Motion History Image

asked 2019-07-04 11:04:24 -0600

updated 2019-07-04 14:27:46 -0600

berak gravatar image

I would like to create motion history image with Python and OpenCV. I use following function :

cv2.motempl.updateMotionHistory

My video has 6 seconds and 144 frames for examples. I would like to see all motion history in one image for 6 seconds and 144 frames. But somehow, it shows for example, 1 seconds and 24 frames in one image motion history. And then, first ones for motion history disappear and new 1-2 seconds 24-40 frames motion history shown. I cannot see all motion history in one image for 6 seconds and 144 frames.

Is it possible to show all motion history for 6 seconds and 144 frames in one image with updateMotionHistory ? Any sample ?

……
        cv2.motempl.updateMotionHistory(motion_mask, motion_history, timestamp, MHI_DURATION)
        mg_mask, mg_orient = cv2.motempl.calcMotionGradient( motion_history, MAX_TIME_DELTA, MIN_TIME_DELTA, apertureSize=7)#5 )
        seg_mask, seg_bounds = cv2.motempl.segmentMotion(motion_history, timestamp, MAX_TIME_DELTA)

        visual_name = visuals[cv2.getTrackbarPos('visual', 'motempl')]
        if visual_name == 'input':
            vis = frame.copy()
        elif visual_name == 'frame_diff':
            vis = frame_diff.copy()
        elif visual_name == 'motion_hist':
            vis = np.uint8(np.clip((motion_history-(timestamp-MHI_DURATION)) / MHI_DURATION, 0, 1)*255)
            vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)
        elif visual_name == 'grad_orient':
            hsv[:,:,0] = mg_orient/2
            hsv[:,:,2] = mg_mask*255
            vis = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
….
edit retag flag offensive close merge delete

Comments

hmm, it would be much better, if you could come up with a minimal, complete, working example, your snippet is incomplete, so ppl can't try.

there's a sample here: https://github.com/opencv/opencv_cont...

berak gravatar imageberak ( 2019-07-05 04:00:50 -0600 )edit

Find exact code here:

#!/usr/bin/env python

import numpy as np
import cv2
import cv2 as cv2_2
import os
from common import nothing, clock, draw_str

MHI_DURATION = 0.5
DEFAULT_THRESHOLD = 32
MAX_TIME_DELTA = 0.25
MIN_TIME_DELTA = 0.05
v_start_frame= 68
v_end_frame=155

def draw_motion_comp(vis, rec_x, rec_y, rec_w, rec_h, angle, color):
    cv2.rectangle(vis, (int(rec_x), int(rec_y)), (int(rec_x+rec_w), int(rec_y+rec_h)), (0, 255, 0))
    r = min(rec_w/2, rec_h/2)
    cx, cy = rec_x+rec_w/2, rec_y+rec_h/2
    angle = angle*np.pi/180
    cv2.circle(vis, (int(cx), int(cy)), int(r), color, 3)
    cv2.line(vis, (int(cx), int(cy)), (int(cx+np.cos(angle)*r), int(cy+np.sin(angle)*r)), color, 3)


currentFrame = 0

FILE_OUTPUT = '/home/test/out_test_motion_history.mp4'
if os.path.isfile(FILE_OU
papatya3 gravatar imagepapatya3 ( 2019-07-05 04:38:09 -0600 )edit