need help for Detecting motion of moving object

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

need help for Detecting motion of moving object

umeshnarayanan
hai

friend  i am a post graduate student in IT,new to this Opencv
environment, currently i am doing a project for Traffic controlling as
my master project

vision based vehicle identification (

i need help for a programm to detect moving object in a video and draw
a rectangle around it and count the moving object
(also needed the parameters about the object
,size,shape,speed,movingdirection etc)

here i have some programm


1)this programm will take a .avi video as input and extract the frame
, the process these frames canny edge detection ,counter detection is
done over the image then the resultant imge sequences are stored in to
output videos


#include "cv.h" // includes OpenCV definitions
#include "highgui.h" // includes highGUI definitions
#include <stdio.h>// includes C standard input/output definitions
#include <stdlib.h>
#include <iostream>


   
int main()   //(int argc, char **argv)
   
   {
      CvCapture *capture = 0;
      IplImage *frame = 0;
      IplImage *sourse = 0;
      IplImage *grayImg = 0; // gray image
      IplImage *cannyImg = 0; // gray image for the canny edge detection
      IplImage *cannyImgclone = 0;
      int key = 0;
     
      CvFont font; //initialize the font
      cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX,1.0, 1.0, 0, 1, CV_AA);
      CvPoint pt1 = cvPoint(20,190);
      CvPoint pt2 = cvPoint(90,220);
      CvPoint pt3 = cvPoint(40,190);
      CvPoint pt4 = cvPoint(50,220);
      capture = cvCaptureFromFile("one.avi"); //capture video from file
     
     IplImage* contourImg = NULL;
 
  //parameters for the contour detection
 
  CvMemStorage * storage = cvCreateMemStorage(0);
  CvSeq * contour = 0;
 
  int mode = CV_RETR_EXTERNAL;
 
  mode = CV_RETR_CCOMP; //detect both outside and inside contour
     
     
     
     
     
      /*==================== writing the video=========================*/
CvVideoWriter *writer;
//CvVideoWriter *cannyout;
int isColor = 1;
int fps     = 20;  // or 30
int frameW  = 500; // 744 for firewire cameras
int frameH  = 500; // 480 for firewire cameras

writer=cvCreateVideoWriter("out.avi",CV_FOURCC('D','I','V','X'),fps,cvSize(frameW,frameH),isColor);

//cannyout=cvCreateVideoWriter("canny.avi",CV_FOURCC('D','I','V','X'),fps,cvSize(frameW,frameH),isColor);

 
  /*==================== writing the video=========================*/  
   
           
   
      /* always check */
      if (!capture)
        {
      fprintf (stderr, "Cannot open Avi file!\n");
      return(1);
    }
 
      cvNamedWindow("result", CV_WINDOW_AUTOSIZE); // create a window
for the result
      cvMoveWindow("result", 350, 0);
      cvNamedWindow("src", CV_WINDOW_AUTOSIZE); // create a window for
sourse
      cvNamedWindow("canny",1);
      cvMoveWindow("canny", 750, 0);
      cvNamedWindow("counter",1);
       cvMoveWindow("counter", 750, 300);      
             
    for (;;)
   
    //for( int fr = 1;frame; frame = cvQueryFrame(capture), fr++ )
    {
   
        frame = cvQueryFrame(capture);// to retrive the frames
             sourse = cvQueryFrame(capture);
       
       
       if (!cvGrabFrame(capture))
      {
      break;
                }
               
    //create a single channel 1 byte image (i.e. gray-level image)
    grayImg = cvCreateImage( cvSize(sourse->width, sourse->height),
IPL_DEPTH_8U, 1 );
   
    //convert original color image (3 channel rgb color image) to
gray-level image
    cvCvtColor( sourse, grayImg, CV_BGR2GRAY );
    cannyImg = cvCreateImage(cvGetSize(sourse), IPL_DEPTH_8U, 1);
   
    // canny edge detection
    cvCanny(grayImg, cannyImg, 50, 150, 3);
   
     
      cvShowImage("src", sourse);
      char string[] = "Captured frame";
      char* t(&string[0]);
     
      cvPutText(frame,t, pt1, &font,CV_RGB(255, 255, 255));
      cvPutText(frame,"frame # ", pt2, &font,CV_RGB(255, 255, 255));
      cvShowImage("result", frame); /* display current frame */
     
       
      cvPutText(cannyImg,"Canny edge ", pt3, &font,CV_RGB(255, 255, 255));
      //cvPutText(cannyImg," detection ", pt4, &font,CV_RGB(255, 255,
255));
      cvShowImage( "canny", cannyImg );
      cannyImgclone = cvCloneImage(cannyImg);
// ==========================

//convert original color image (3 channel rgb color image) to
gray-level image
  cvCvtColor( sourse, grayImg, CV_BGR2GRAY );
  cvShowImage( "counter", sourse );
 //make a copy of the original image to draw the detected contour
  contourImg = cvCreateImage(cvGetSize(sourse), IPL_DEPTH_8U, 3);
  contourImg = cvCloneImage( sourse );
 
  //find the contour
  cvFindContours(grayImg, storage, &contour, sizeof(CvContour), mode,
CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
 
  //draw the contour
  cvDrawContours(contourImg, contour, CV_RGB(0, 255, 0), CV_RGB(255,
0, 0), 2, 2, 8);
 

  cvShowImage( "counter", contourImg );
 
 
 
 
 
 
 
// ===============================    
 
       key=cvWaitKey(150);
     // key=cvWaitKey(30);
     
     //for saving the frames
         char can[] =  "cannyedge.jpg";
             char* c( &can[0] );
         
             cvSaveImage(c ,cannyImgclone);
             
             char result[] =  "resultant.jpg";
             char* r( &result[0] );
         
             cvSaveImage(r ,frame);
             
             
             cvWriteFrame(writer,frame);
           //cvWriteToAVI(cannyout,cannyImgclone);
             
             
/* always check */
             
 if (!frame)
     {
      break;
      }
     
/* exit if user press a key */    
 
if ((key = cvWaitKey(1)) > 0)
      {
      goto done;
      }
     
       } // end of for loop
     
      done:
   
      cvWaitKey(0);
      cvDestroyWindow("result");
      cvDestroyWindow("src");
     
       cvDestroyWindow( "canny" );
   
   
      cvReleaseImage( &grayImg );
      cvReleaseImage( &cannyImg );
      cvReleaseImage( &frame );
      cvReleaseImage( &sourse );
    cvReleaseImage( &contourImg );
     
      cvReleaseCapture(&capture);
      //cvReleaseVideoWriter(&writer);
     // cvReleaseVideoWriter(&cannyout);
      return(0);
 
      }


2) this is a another interesting programm which take an .avi video as
input then extract the for ground and background
and show them seperately

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

//this is a sample for foreground detection functions
int main(int argc, char** argv)
{
IplImage* tmp_frame = NULL;
IplImage* sourse = NULL;
CvCapture* capt = NULL;

      CvFont font; //initialize the font
      cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX,1.0, 1.0, 0, 1, CV_AA);
      CvPoint pt1 = cvPoint(20,190);
     




//if( argc < 2 )
//{
// printf("please specify video file name \n");
// exit(0);
// }

//cap = cvCaptureFromFile(argv[1]);


capt = cvCaptureFromFile("yellow.avi"); // capture frme form file
tmp_frame = cvQueryFrame(capt);
sourse = cvQueryFrame(capt);

if(!tmp_frame)
{
printf("bad video \n");
exit(0);
}
cvNamedWindow("Sourse", 1);
cvNamedWindow("BackGround", 1);
cvMoveWindow("BackGround",350, 0);
cvNamedWindow("ForGround", 1);
cvMoveWindow("ForGround", 750, 0);


// this portion is bit complcated ,need more understanding


//create BG model
CvBGStatModel* bg_model;

bg_model =cvCreateGaussianBGModel( tmp_frame );

for( int fr = 1;tmp_frame; tmp_frame = cvQueryFrame(capt), fr++ )
{

double t = (double)cvGetTickCount();

cvUpdateBGStatModel( tmp_frame, bg_model );

t = (double)cvGetTickCount() - t;


printf( "%.1f\n", t/(cvGetTickFrequency()*1000.) );




cvPutText(sourse,"sourse Video", pt1, &font,CV_RGB(255, 255, 255));
cvShowImage("Sourse",sourse );
cvPutText(bg_model->background,"Background", pt1, &font,CV_RGB(255,
255, 255));
cvShowImage("BackGround", bg_model->background);

cvPutText(bg_model->foreground,"ForeGround", pt1, &font,CV_RGB(255,
255, 255));  

cvShowImage("ForGround", bg_model->foreground);

//int k = cvWaitKey(20);

cvWaitKey(20);
//if( k == 27 ) break;
//printf("frame# %d \r", fr);
}


cvReleaseBGStatModel( &bg_model );
cvReleaseCapture(&capt);

return 0;
}


my next task is to make a programm which take a video file and
extaract background and detect the moving object in the video file
(i have try some example but not working properly)

if any one of you know about this please come forward
help me