import video\r
from common import nothing, clock, draw_str\r
\r
-MHI_DURATION = 1.0\r
-DEFAULT_THRESHOLD = 16\r
-MAX_TIME_DELTA = 0.5\r
+MHI_DURATION = 0.5\r
+DEFAULT_THRESHOLD = 32\r
+MAX_TIME_DELTA = 0.25\r
MIN_TIME_DELTA = 0.05\r
\r
+def draw_motion_comp(vis, (x, y, w, h), angle, color):\r
+ cv2.rectangle(vis, (x, y), (x+w, y+h), (0, 255, 0))\r
+ r = min(w/2, h/2)\r
+ cx, cy = x+w/2, y+h/2\r
+ angle = np.deg2rad(angle)\r
+ cv2.circle(vis, (cx, cy), r, color, 3)\r
+ cv2.line(vis, (cx, cy), (int(cx+np.cos(angle)*r), int(cy+np.sin(angle)*r)), color, 3)\r
\r
if __name__ == '__main__':\r
import sys\r
frame_diff = cv2.absdiff(frame, prev_frame)\r
gray_diff = cv2.cvtColor(frame_diff, cv.CV_BGR2GRAY)\r
thrs = cv2.getTrackbarPos('threshold', 'motempl')\r
- ret, motion_mask = cv2.threshold(gray_diff, thrs, 255, cv2.THRESH_BINARY)\r
+ ret, motion_mask = cv2.threshold(gray_diff, thrs, 1, cv2.THRESH_BINARY)\r
timestamp = clock()\r
cv2.updateMotionHistory(motion_mask, motion_history, timestamp, MHI_DURATION)\r
- mg_mask, mg_orient = cv2.calcMotionGradient( motion_history, MAX_TIME_DELTA, MIN_TIME_DELTA, apertureSize=5 );\r
+ mg_mask, mg_orient = cv2.calcMotionGradient( motion_history, MAX_TIME_DELTA, MIN_TIME_DELTA, apertureSize=5 )\r
+ seg_mask, seg_bounds = cv2.segmentMotion(motion_history, timestamp, MAX_TIME_DELTA)\r
\r
visual_name = visuals[cv2.getTrackbarPos('visual', 'motempl')]\r
if visual_name == 'input':\r
hsv[:,:,0] = mg_orient/2\r
hsv[:,:,2] = mg_mask*255\r
vis = cv2.cvtColor(hsv, cv.CV_HSV2BGR)\r
+\r
+ for i, rect in enumerate([(0, 0, w, h)] + list(seg_bounds)):\r
+ x, y, rw, rh = rect\r
+ area = rw*rh\r
+ if area < 64**2:\r
+ continue\r
+ silh_roi = motion_mask [y:y+rh,x:x+rw]\r
+ orient_roi = mg_orient [y:y+rh,x:x+rw]\r
+ mask_roi = mg_mask [y:y+rh,x:x+rw]\r
+ mhi_roi = motion_history[y:y+rh,x:x+rw]\r
+ if cv2.norm(silh_roi, cv2.NORM_L1) < area*0.05:\r
+ continue\r
+ angle = cv2.calcGlobalOrientation(orient_roi, mask_roi, mhi_roi, timestamp, MHI_DURATION)\r
+ color = ((255, 0, 0), (0, 0, 255))[i == 0]\r
+ draw_motion_comp(vis, rect, angle, color)\r
+\r
draw_str(vis, (20, 20), visual_name)\r
cv2.imshow('motempl', vis)\r
\r