简体   繁体   中英

OpenCv C++ record video when motion detected from cam

I am attempting to use a straightforward motion detection code to detect movement from a camera. I'm using the OpenCV library and I have some code that takes the difference between two frames to detect a change.

I have the difference frame working just fine and it's black when no motion is present.

The problem is how now i can detect that blackness to stop recording or no darkness to begin recording frames.

Thank u all.

A very simple thing to do is to sum the entire diff image into an integer. If that sum is above a threshold you have movement. Then you can use a second threshold and when the sum is below that limit you stopped having movement.

You can also make the threshold only change the program state if some elapsed time has occurred since the last threshold. ie after movement is detected you don't check for lack of movement for 10 seconds.

What about storing a black frame internally and using your same comparison code? If your new frame is different (above a threshold) from the all-black frame, start recording.

This seems the most straightforward since you already have the image-processing algorithms down.

Take a look at the code of the free software motion for getting inspiring ideas.

There are quite a few things to keep in mind for reliable motion detection. For example tolerate the slow changes from the sun's rotation. Or accepting momentary image glitches which can come especially from the cheapest cameras.

From a small experience I have had, I think that better than just adding up all differences, it works better to count the number of pixels whose variation exceeds a certain threshold.

Motion also offers masks, which let you for example ignore movements in a nearby road.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM