Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the differences between frames using OpenCV?

Tags:

opencv

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

like image 332
Ilan Reuven Avatar asked Jun 12 '26 03:06

Ilan Reuven


1 Answers

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();
}
like image 161
user937284 Avatar answered Jun 15 '26 16:06

user937284