LoginSignup
13
22

More than 5 years have passed since last update.

ステーキ定食の画像から物体を選別できないかやってみた - ①物体検出編

Last updated at Posted at 2017-06-08

はじめに

以前、OpenCVを使って物体検出を行いましたが、Selective Searchという名のライブラリがあるということで、どれくらい精度に違いがあるのか比較してみました。

また、以前あまり理解していなかった環境構築周りも見直しがてらに別の方法で行ってみました。なのでちょこっと違います。

なお、試してみたところSelective SearchはPython3だと関数が異なるため正常に動作しません。

Selective Search

Selective Search

パクリ元

R-CNNとしてSelective search を使ってみた

環境

# OS/ソフトウェア/ライブラリ バージョン
1 Mac OS X EI Capitan
2 Python 2.7系
3 OpenCV 3.2系
4 Selective Search
5 matplotlib 2.0系

構築

以前と一緒

最新にアップデート

brew update

tap

brew tap homebrew/python
brew tap homebrew/science

Pythonのインストール

brew install python

パスの確認

which python
/usr/local/bin/python ※1

※1 /usr/bin/pythonの場合、PATHを通してあげてください。
※ 反映されない場合ははコンソールの再起動をかけてみてください。

PATHの設定

.zshrc
if [ -d $(brew --prefix)/lib/python2.7/site-packages ];then
  export PYTHONPATH=$(brew --prefix)/lib/python2.7/site-packages:$PYTHONPAT
fi

バージョンの確認

python
Python 2.7.13 (default, Apr  4 2017, 08:46:44) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

ここから異なります

Applicationsにリンクを貼る

brew linkapps

pipをインストール

easy_install pip

setuptoolsを最新版に更新

pip install --upgrade setuptools

pipを最新版に更新

pip install --upgrade pip

OpenCV3をインストール

pip install opencv-python

NumPyをインストール

pip install numpy

MatplotLibをインストール

pip install matplotlib

Selective Searchをインストール

pip install selectivesearch

比較

画像

今回はこのステーキ定食を使います。
物体検出の比較元ソースコードは以前を使います。
※OpenCVのバージョンの関係でfindContoursの戻り値の数が異なるのでご注意を。(http://amazarashi.me/archives/752)

steak.jpg

Selective Searchのソースコード

R-CNNとしてSelective search を使ってみたのまま使ってたりします。

grouping_image.py
import cv2
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import selectivesearch

def main():
        # loading lena image
    img = cv2.imread("{画像パス}")

    # 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 smaller than 2000 pixels
        if r['size'] < 2000:
            continue
        # distorted rects
        x, y, w, h = r['rect']
        if w / h > 1.2 or h / w > 1.2:
            continue
        candidates.add(r['rect'])

    # draw rectangles on the original image
    fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
    ax.imshow(img)
    for x, y, w, h in candidates:
        print(x, y, w, h)
        rect = mpatches.Rectangle(
            (x, y), w, h, fill=False, edgecolor='red', linewidth=1)
        ax.add_patch(rect)

        cv2.imwrite('{ディレクトリパス}' + str(x) + '.jpg', img[y:y + h, x:x + w])

    plt.show()

if __name__ == "__main__":
    main()

結果

いくつの料理や食べ物や調味料を輪郭抽出できるかを比較します。
レベルとしては器や皿1つに対して抽出できてかなので割りと厳し目にしてます。

通常の場合

2つ

56.jpg

223.jpg

Selective Searchの場合

5つ

57.jpg

227.jpg

379.jpg

405.jpg

273.jpg

感想

  • Selective Searchのほうが精度が良かった。
  • Selective Searchでもステーキ定食なのにお肉は綺麗に検出されなかった。

329.jpg

238.jpg

  • 色々処理をしているのかSelective Searchは速度は多少(10秒くらい?)かかった。
  • PythonやOpenCVのバージョンの違いによって関数結構違いすぎて色々面倒くさい。
  • 色々やってるわりにPythonやOpenCVを1から独力で書ける気がしない。

全ページリンク

13
22
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
13
22