Created
July 11, 2014 08:01
-
-
Save pklaus/c4fe6a12c6c8e2ff3c16 to your computer and use it in GitHub Desktop.
Motion detection using a webcam, Python, OpenCV and Differential Images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Found on | |
http://www.steinm.com/blog/motion-detection-webcam-python-opencv-differential-images/ | |
""" | |
import cv2 | |
def diffImg(t0, t1, t2): | |
d1 = cv2.absdiff(t2, t1) | |
d2 = cv2.absdiff(t1, t0) | |
return cv2.bitwise_and(d1, d2) | |
cam = cv2.VideoCapture(0) | |
winName = "Movement Indicator" | |
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE) | |
# Read three images first: | |
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
while True: | |
cv2.imshow( winName, diffImg(t_minus, t, t_plus) ) | |
# Read next image | |
t_minus = t | |
t = t_plus | |
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) | |
key = cv2.waitKey(10) | |
if key == 27: | |
cv2.destroyWindow(winName) | |
break | |
print("Goodbye") |
I am having the same error, could you solve it ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /io/opencv/modules/imgproc/src/color.cpp, line 10638
Traceback (most recent call last):
File "MD.py", line 15, in
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
cv2.error: /io/opencv/modules/imgproc/src/color.cpp:10638: error: (-215) scn == 3 || scn == 4 in function cvtColor
it shows the error ? what should i do ?