3

How can I find the differences between frames when I'm running video on OpenCV? I need to do a loop that checks the changes from frame to frame and displays the result in another window? Can I do it in the loop that i attach here? Or is there another way to do it?

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 get this error on the command window:Compiler did not align stack variables. Libavcodec has been miscompiled and may be very slow or crash. This is not a bug in libavcodec, but in the compiler. You may try recompiling using gcc >= 4.2. Do not report crashes to 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 maby the maby the size of depth? how can i fix it? or maby something wrong with the code? thanks a lot for your help

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Ilan Reuven
  • 61
  • 2
  • 3
  • 6
  • Take the time to review your other questions and accept the answers that solved them! There's a little checkbox near each answer, you can click on it to select the official answer. This way you are helping the community too. – karlphillip Apr 03 '12 at 21:25
  • Do you mean the degree of difference between similar images (objects scenes) or just you want to calculate the difference image like Diff = frame[N] - frame[N+1]? could you clarify your question – Y.AL Oct 06 '13 at 17:52

3 Answers3

3

You can subtract the two Mat 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();
}
user937284
  • 2,454
  • 6
  • 25
  • 29
1

there are simple approaches to frame difference, but I'd recommend you checking the Motion Template sample from OpenCV. It is located at OpenCVversion\samples\c\motempl.exe. This is some advanced way of doing the difference and that might be what you are interested.

In case you want to accomplish that with a frame subtraction, you should create another IplImage to store the last frame and subtract the current frame from that one. Also make sure you create another window for seeing the result.

You should take a 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 that helps.

Kind regards, Daniel

Community
  • 1
  • 1
dannyxyz22
  • 978
  • 9
  • 18
0

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;
}
Ilan Reuven
  • 61
  • 2
  • 3
  • 6