[PYTHON] Road to real-time background / foreground separation (1)

Since meanshift tracking is often caught in background noise, I would like to separate the foreground and background in real time and apply meanshift tracking only to the foreground.

The API used is as follows.

absdiff (Japanese reference)

So I tried it.

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)

If there is a change in the pixels compared to the previous frame, the larger the difference, the whiter the image.

IMAGE ALT TEXT HERE

That's all for today.


I'm blogging: http://weed.nagoya

Recommended Posts

Road to real-time background / foreground separation (1)
The road to Pythonista
The road to Djangoist
Road to LPIC-1 acquisition
A road to intermediate Python
The road to download Matplotlib