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" );
      getchar();
      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" );
           getchar();
           break;
     }

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

after:

 

f++;
if (f==1) //Save first frame
{
       extractImageCOI(frame,mtx,1);
}

 

if (f==2) //Save next frame
{
       extractImageCOI(frame,mtx1,1);
       f=0;

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.

Next:

 

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

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

after:

minMaxLoc(mtx2,0,0,0,&p);

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

 after:

 

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);
      roi=Scalar(0);
}

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:

Enjoy! 

   
  
© geek brothers