Skip to content

Instantly share code, notes, and snippets.

@macrat
Created March 19, 2016 12:16
Show Gist options
  • Save macrat/393a3ed8885cb7c62db5 to your computer and use it in GitHub Desktop.
Save macrat/393a3ed8885cb7c62db5 to your computer and use it in GitHub Desktop.
微妙性能な動体検知。
import cv2
import numpy
cam = cv2.VideoCapture(0)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
mog = cv2.createBackgroundSubtractorMOG2()
while cv2.waitKey(1):
img = cam.read()[1]
out = numpy.array(cv2.GaussianBlur(img, (5, 5), 10))
mask = cv2.GaussianBlur(mog.apply(img), (9, 9), 10)
canny = cv2.GaussianBlur(cv2.Canny(mask, 10, 50), (19, 19), 7)
cnts = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[1]
for cnt in cnts:
rect = cv2.boundingRect(cnt)
cv2.rectangle(out, rect[:2], (rect[0]+rect[2], rect[1]+rect[3]), (255, 0, 0))
cv2.imshow('moving', out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment