How do I find the differences between frames using OpenCV?

How do I find the differences between frames when running a video on OpenCV? I need to make a loop that checks for changes from frame to frame and displays the result in another window? Can I do this in the loop I am attaching here? Or is there another way to do this?

while( key != 'x' )  
{  
   frame = cvQueryFrame( capture );
   cvCvtColor(frame, gray, CV_RGB2GRAY);

   //gray_frame = cvQueryFrame( capture );

   //cvCvtColor(frame, gray_frame, CV_BGR2GRAY);

   if(key==27)
        break;

   cvShowImage( "video",frame );
   cvShowImage( "grayvideo",gray );

   key = cvWaitKey( 1000 / fps );  
}  
cvDestroyWindow( "video" );  
cvDestroyWindow( "grayvideo" ); 
cvReleaseCapture( &capture );  

return 0;

      


I am getting this error in the command window: the compiler did not align the stack variables. Libavcodec has been compromised and can be very slow or crash. This is not a bug in libavcodec, but in the compiler. You can try recompiling using gcc> = 4.2. Don't report crashes to the FFmpeg developers. OpenCV error: assertion failed (src1.size () == dst.size () && src1.type () == dst. Type ()) in unknown function, file ........ \ ocv \ opencv \ src \ cxcore \ cxarithm.cpp, line 1563

what is wrong, mabs are mabs the size of depth? how can i fix this? or is there something wrong with the code? Many thanks for your help

+3


source to share


3 answers


You can subtract two Mata objects / pointers.



Mat prev_frame;
cap.read(prev_frame);

while (1)
{
    Mat frame;
    cap.read(frame);

    Mat dif = frame - prev_frame;
    imshow("difference", dif);

    // you can also use absdiff
    //absdiff(frame, prev_frame, dif);

    prev_frame = frame.clone();
}

      

+3


source


there are simple approaches to frame difference, but I would recommend you check out the sample motion pattern from OpenCV. It is located in OpenCVversion \ samples \ c \ motempl.exe. This is an advanced way to make a difference, and that might be what interests you.

If you want to do this with subtractive frames, you must create another IplImage to hold the last frame and subtract the current frame from that. Also make sure you create another window to view the result.

You can look at this post for information on subtraction.

Your code should look like the following pseudocode:



allocate space for frame, oldFrame and destinationFrame, all the same size/type.

while(1){      
  copy current frame to oldFrame
  grab new frame in frame

  cvSub(frame,oldFrame,dest)

  cvShow(dest)

      

}

I hope this helps.

Regards, Daniel

+1


source


This is the new code from your advice:

 #include "stdafx.h"
 #include <stdio.h> // For printf
 #include <cv.h>  
 #include <cxcore.h>  
 #include <highgui.h>      

int main()  
{  


    int key = 0; 




     CvCapture* capture = cvCaptureFromAVI( "macroblock.mpg" ); 
     IplImage* frame = cvQueryFrame( capture );
     IplImage* currframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
     IplImage* destframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);

        if ( !capture ) 

    {  
        fprintf( stderr, "Cannot open AVI!\n" );  
        return 1;  
        }

      int fps = ( int )cvGetCaptureProperty( capture, CV_CAP_PROP_FPS );

      cvNamedWindow( "dest", CV_WINDOW_AUTOSIZE );

      while( key != 'x' )
          {
              frame = cvQueryFrame( capture );
        currframe = cvCloneImage( frame );
         frame = cvQueryFrame( capture );



              cvSub(frame,currframe,destframe);

              if(key==27 )break;
              cvShowImage( "dest",destframe);
               key = cvWaitKey( 1000 / fps );
               }  
           cvDestroyWindow( "dest" );
           cvReleaseCapture( &capture );
           return 0;
}

      

0


source







All Articles