LoginSignup
8
9

More than 5 years have passed since last update.

pythonで物体の自動検出→トリミング→リサイズまで

Last updated at Posted at 2018-05-19

やったこと

静止画から物体を自動検出するために、selective searchというpythonのライブラリを使ってみました。
実行環境:Windows7-32bit, python3.6, Anaconda
selective searchは以下のコマンドでインストールできます。

pip install selectivesearch

実行結果

元の画像
fifth_corner.jpg

実行結果
image.png
image.png
image.png
image.png
image.png
image.png

何も無いところ(路面の傷かな?)が検出されましたが、ライダーとバイクもちゃんと抽出できています。

物体検出される領域のサイズやアスペクト比を変更すると、ヘルメット、タイヤ、グローブ等も検出されました。これらのパラメータは検出したい物によって、調整が必要みたいです。

image.png
image.png
image.png
image.png

他にも色々と検出されましたが、数が多いので割愛しました。

コード

selective_search.py
# -*- coding: utf-8 -*-
import skimage.data
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import selectivesearch

def main():

    # loading image
    img = skimage.data.imread('fifth_corner.jpg') # 画像ファイルを指定します。
    print("image shape " + str(img.shape))

    # perform selective search
    img_lbl, regions = selectivesearch.selective_search(img, scale=500, sigma=0.9, min_size=10)

    candidates = set()
    for r in regions:
        # excluding same rectangle (with different segments)
        if r['rect'] in candidates:
            continue

        # excluding regions small pixels
        if r['size'] < 30000: # 物体検出される領域の大きさを指定します。
            continue

        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 2.0 or h / w > 2.0: # 物体検出される領域のアスペクト比を指定します。
            continue

        candidates.add(r['rect'])

    print("number of object " + str(len(candidates)))

    for x, y, w, h in candidates:

        # draw original image
        fig, ax = plt.subplots(ncols=3, nrows=1, figsize=(10, 10))
        ax[0].imshow(img)

        # draw rectangles on the original image
        rect = mpatches.Rectangle(
            (x, y), w, h, fill=False, edgecolor='red', linewidth=1)
        ax[0].add_patch(rect)

        # trim image and draw
        img2 = img[y:y+h, x:x+w]
        ax[1].imshow(img2)

        # reshape trimed image and draw
        img3 = skimage.transform.resize(img2, (64, 64))
        ax[2].imshow(img3)

    plt.show()

if __name__ == "__main__":
    main()
8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9