Share this post

Submit to DeliciousSubmit to DiggSubmit to FacebookSubmit to Google BookmarksSubmit to StumbleuponSubmit to TechnoratiSubmit to TwitterSubmit to LinkedIn

In this project I used OpenCV library and C++ to detect the position of a moving object using a usb camera.

OpenCV is an opensource library for computer vision in C++. If you don't have it you can get the latest version here.

You can also find any information you need about installing and using this library through the above link.

Ok let's get to the code:

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <stdio.h>

using namespace cv;


int _tmain(int argc, _TCHAR* argv[])

     Mat mtx(480,640,CV_8UC1);
     Mat mtx1(mtx.rows,mtx.cols,CV_8UC1);
     Mat mtx2(mtx.rows,mtx.cols,CV_8UC1);
     Point p;
     int f=0;

As you can see in this part I only included cv.h and used cv namespace and declared the variables.

next part:

CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if ( !capture ) {
      fprintf( stderr, "ERROR: capture is NULL \n" );
      return -1;

This part of code finds a camera installed on the system and attaches the pointer "capture" to it.

next part:


while ( 1 )
     // Get one frame
     IplImage* frame = cvQueryFrame( capture );
     if ( !frame ) {
           fprintf( stderr, "ERROR: frame is null...\n" );

This part of code gets a frame from the camera and saves it as the IplImage variable "frame".



if (f==1) //Save first frame


if (f==2) //Save next frame

As you can see I've used a variable called "f" to save two alternative frames. First frame is saved as the matrix "mtx" and second one as "mtx1".

After we saved these frames we set "f" to zero.



absdiff(mtx,mtx1,mtx2); //Subtracts two alternative frames to detect motion

I've subtracted the two alternative frames and saved the result in "mtx2". 



This function saves the address of the maximum value of "mtx2" in the pointer "p".



if ((p.x<630)&&(p.y<470)) //The whole mask should be in the frame
      Rect mask(p.x,p.y,10,10);
      Mat roi(frame,mask);

This part checks if the moving object doesn't go out of boundaries and draws a white rectangle on the moving object. If we have many moving objects this mark goes on the object which moves faster because in the previous section we only saved the maximum changing value in "p".


imshow("mywindow", frame);

//If ESC key pressed
if ( (cvWaitKey(10) & 255) == 27 ) break;

// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );

return 0;

Ok this part marks the end of our program. It shows the modified frame and releases the resources.

Now this program is completely realtime for a 640*480 frame and if anything moves in front of the camera it'll be marked and detected.

The complete code is here:



Strict Standards: Only variables should be assigned by reference in /home/geekbrot/public_html/modules/mod_ilike_social_media_optimization/tmpl/default.php on line 13

Strict Standards: Only variables should be assigned by reference in /home/geekbrot/public_html/modules/mod_ilike_social_media_optimization/tmpl/horizontal1.php on line 13
© geek brothers