X
    Categories: C++ OpenCV Tutorials

OpenCV : Motion Tracking with Recording using WebCam (Easy Way) aka Motion Activated Security Camera

Hello Everybody, Pankaj here !!
Today we are going to be going over another OpenCV Tutotirla using C++, more importantly, we are going to use our new found knowledge of Image Processing to build our very own motion-activated surveillance camera.

Here is a little preview of what the program is going to look like when all is said and done:

You can see that once, there is motion detected in the camera’s field of vision, the program then starts recording and it will save to a video file on the hard drive and you’ll be able to open it up at the end of the day if you run it for the whole day and view all the activity that’s happened on your camera.

Some of the features that you’ll notice is that the camera only records when there is a motion on the screen. Also, we have added a little data and time stamp for your convinience just like the CCTV camera or a closed-circuit camera that’s used for survelliance.

So let’s look at the complete code and pay attention to the comments with each line of code to understand what every line is meant to do.
Let’s get started!
Open up visual studio and creat an empty project and then create a new motion.cpp file and paste the below code in it. Ensure that your build is correctly set and you have linked the opencv libraries and includes.



//http://pythonopencv.com

#include 
#include 
#include 

using namespace std;
using namespace cv;

//our sensitivity value to be used in the absdiff() function
//for higher sensitivity, use a lower value
const static int SENSITIVITY_VALUE = 40;
//size of blur used to smooth the intensity image output from absdiff() function
const static int BLUR_SIZE = 10;
//these two can be toggled by pressing 'd' or 't'
bool debugMode;
bool trackingEnabled;

//int to string helper function
string intToString(int number){

	//this function has a number input and string output
	std::stringstream ss;
	ss  03)
	//we use 'setw(2)' so that we force the string 2 characters wide with a zero in front of it.
	//if the month is '10' then it will remain '10'
	std::stringstream m;
	m > contours;
	vector hierarchy;
	//find contours of filtered image using openCV findContours function
	//findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );// retrieves all contours
	findContours(temp,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE );// retrieves external contours

	//if contours vector is not empty, we have found some objects
	//we can simply say that if the vector is not empty, motion in the video feed has been detected.
	if(contours.size()>0)motionDetected=true;
	else motionDetected = false;

	return motionDetected;

}
int main(){
	//set recording and startNewRecording initially to false.
	bool recording = false;
	bool startNewRecording = false;
	int inc=0;
	bool firstRun = true;
	//if motion is detected in the video feed, we will know to start recording.
	bool motionDetected = false;

	//pause and resume code (if needed)
	bool pause = false;
	//set debug mode and trackingenabled initially to false
	//these can be toggled using 'd' and 't'
	debugMode = false;
	trackingEnabled = true;
	//set up the matrices that we will need
	//the two frames we will be comparing
	Mat frame1,frame2;
	//their grayscale images (needed for absdiff() function)
	Mat grayImage1,grayImage2;
	//resulting difference image
	Mat differenceImage;
	//thresholded difference image (for use in findContours() function)
	Mat thresholdImage;
	//video capture object.
	VideoCapture capture;
	capture.open(0);
	VideoWriter oVideoWriter;//create videoWriter object, not initialized yet
	double dWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
	double dHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
	//set framesize for use with videoWriter
	Size frameSize(static_cast(dWidth), static_cast(dHeight));

	if(!capture.isOpened()){
		cout
Editor :