Skip to content

Instantly share code, notes, and snippets.

@xoxota99
Last active March 3, 2018 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xoxota99/dd5a3c412a7505faa7d2179e8417b517 to your computer and use it in GitHub Desktop.
Save xoxota99/dd5a3c412a7505faa7d2179e8417b517 to your computer and use it in GitHub Desktop.
Working OpenCV absdiff.dilate display
import cv2
# using pure OpenCV.
vs = cv2.VideoCapture(0)
# time.sleep(2.0)
lastFrame = None
while True:
# grab the frame from the VideoCapture
_, frame = vs.read()
# resize
frame = cv2.resize(frame, (800, 600), fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR)
# greyscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# blur
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if lastFrame is None:
lastFrame = gray
continue
# diff
frameDelta = cv2.absdiff(lastFrame, gray)
lastFrame = gray
thresh = cv2.threshold(frameDelta, 20, 255, cv2.THRESH_BINARY)[1]
# show the output frame
cv2.imshow("thresh", thresh)
key = cv2.waitKey(1) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment