Python
OpenCV

OpenCVでリアルタイムエッジ検出する

1. opencvのインストール

Raspberry Pi

python開発環境構築を参照

Windows

anacondaでtensorflowとopencv導入

mac

systemにいれる場合(opencv2)
$ brew install gcc
$ brew install cmake
$ brew install tesseract
$ brew tap homebrew/science
$ brew install opencv
$ ln -s /usr/local/Cellar/opencv/2.4.13.2/lib/python2.7/site-packages/cv2.so /Users/riki/.pyenv/versions/ML-2.7.13/lib/python2.7/site-packages/
anacondaを使って入れる場合(opencv3)
$ pyenv install anaconda3-4.1.1
$ cd <workspace with opencv3>
$ pyenv local anaconda3-4.1.1
$ conda install -c https://conda.anaconda.org/menpo opencv3
$ conda update hdf5

2. スクリプトの作成

opencvのcanny edge detectionを使う。

edge-detection.py
import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    frame =  cv2.Canny(frame, threshold1 = 100, threshold2=300)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

スクリーンショット 2017-08-15 16.58.07.png