LoginSignup
7
9

More than 5 years have passed since last update.

リアルタイム背景・前景の分離への道(1)

Last updated at Posted at 2015-06-29

どうもmeanshift追跡が背景ノイズに捕まってしまうことが多いので、リアルタイムに前景と背景を分離し、前景に対してのみmeanshift追跡を適用してみたいと思います。

使うAPIは以下のもの。

absdiff(日本語リファレンス)

で、やってみました。

import cv2

cam = cv2.VideoCapture(0)
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)

img_past = None
img_now  = None

while True:
    img_past = img_now
    img_now  = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
    if img_now is not None and img_past is not None:
        img_diff = cv2.absdiff(img_now, img_past)
        cv2.imshow(winName, img_diff)
    key = cv2.waitKey(10)

直前のフレームと比較して画素に変化があれば、その差分の大きさが大きいほど白くなります。

IMAGE ALT TEXT HERE

今日はここまで。


ブログやっています:http://weed.nagoya

7
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
7
9