Common Methods for moving object detection include optical flow, background subtraction, and frame difference ). The Background Subtraction Method and the interframe difference method are suitable for static cameras, while the optical flow method is used for camera motion, but the calculation is relatively large.
The following describes how to use the cvupdatemotionhistory function in opencv to detect motion targets. Cvupdatemotionhistory is used to update motion history images. Its prototype is:
Void cvupdatemotionhistory (
Const cvarr * silhouette,
Cvarr * MHI,
Double timestamp,
Double duration );
The parameters are described as follows,
Silhouette, a motion contour image obtained by difference between frames.
MHI, short for motion histoty image, indicates the motion history image.
Timestamp.
Duration, the maximum time that pixels that have been moved can be maintained.
The relationship between the output motion history image MHI and the input motion contour image silhouette can be expressed as follows:
(1) MHI (x, y) = timestamp if silhouette (x, y )! = 0. That is, when the motion contour image is not zero at the coordinate (x, y), the time mark timestamp of MHI at (x, y.
(2) MHI (x, y) = 0 if silhouette (x, y) = 0 and MHI (x, y) <(timestamp-duration ). because timestamp is constantly increasing over time, the maximum duration of pixels that have been moved is duration. when the duration of MHI (X, Y) is longer than duration, MHI (X, Y) is zero if no new motion occurs at (x, y.
(3) MHI (x, y) = MHI (x, y) otherwise. as long as the duration of the moving pixel does not exceed duration, the MHI (x, y) size is maintained at the time point of the motion.
The following provides the code based on opencv2.0. The code references the routines in the opencv installation directory and makes some changes to facilitate other projects to directly call functions for moving target detection.
# Include <cv. h> # include
The program runs as follows: