Created
June 12, 2014 01:53
-
-
Save allanlei/fb4a46484f3505fedf1b to your computer and use it in GitHub Desktop.
Motion detection using OpenCV with Differential Images technique. Source http://www.steinm.com/blog/motion-detection-webcam-python-opencv-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
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment