Static camera detects moving objects

Source: Internet
Author: User

Recently, I have learned a small example of video surveillance, using a static camera to detect moving objects, and have a simple background difference method and simple frame difference method of the two methods of OPENCV implementation, considered as an extension of the OpenCV understanding.

The first simple background difference method is simple in principle. Background difference is that the image is divided into foreground and background, for a fixed scene, first make a background template, and then for each frame new image, calculate the difference between the current frame and the background image, get the foreground, the foreground corresponds to the moving object or person. But the simple background difference method has a problem, it assumes the background is static, if there is a table in the scene, we think of it as the background and add to the background image, but if there is a person into the scene, met the table, then people can be detected as the foreground, But this table is also considered a background because it is not the same as the previous table. In other words, the background difference, the image used as the background can not be absolutely static, or the background changes will be considered "intruder."

The second is the frame difference method, the frame difference value is the current frame minus the previous frame, the resulting difference is the motion of the image. In order to reduce the error, it is generally the next frame minus the current frame, the current frame minus the previous frame, and then the two difference images do bitwise AND operation. The method can adapt to the change of background, each time it is dynamic to identify the current change, equivalent to a relative change, but can not measure the current scene and fixed scene absolute change, so the two can be said to be the application of the same. The code and running result of the frame difference method are pasted.


#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/video/ background_segm.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream> #include <sstream
> Using namespace CV;

using namespace Std;

	Mat Framediff (Mat Prevframe, Mat Curframe, Mat nextframe) {Mat diffFrames1, diffFrames2, output;
	Absdiff (NextFrame, Curframe, diffFrames1);

	Absdiff (Curframe, Prevframe, diffFrames2);

	Bitwise_and (diffFrames1, diffFrames2, Output);
return output;
	} Mat GetFrame (videocapture cap, float scalingfactor) {mat frame, output;
	Cap >> frame;
	Resize (frame, frame, Size (), Scalingfactor, Scalingfactor, Inter_area);
	Cvtcolor (frame, output, cv_bgr2gray);
return output;
	} int main (int argc, char* argv[]) {Mat frame, prevframe, Curframe, NextFrame;
	Char ch;
	Videocapture Cap (0);
	if (!cap.isopened ()) return-1;
	Namedwindow ("Frame");

	float scalingfactor = 0.75;
	Prevframe = GetFrame (Cap, scalingfactor);Curframe = GetFrame (Cap, scalingfactor);
	NextFrame = GetFrame (Cap, scalingfactor);
		while (true) {imshow ("Object movement", Framediff (Prevframe, Curframe, NextFrame));
		Prevframe = Curframe;
		Curframe = NextFrame;
		NextFrame = GetFrame (Cap, scalingfactor);
		ch = waitkey (30);
		if (ch = =) {break;
	}} cap.release ();

	Destroyallwindows ();
return 1; }






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.