Search code examples
c++computer-visionvideo-capture

How to detect if two successive video frames are identical or not?


I'm working on a computer vision project on a video. I want to detect if any two successive frames are identical or not. This is my code.

capture >> currentFrame;
previousFrame = currentFrame;
do{
capture >> currentFrame;
cvtColor( currentFrame, g1, CV_BGR2GRAY );
cvtColor( previousFrame, g2, CV_BGR2GRAY );
cv::absdiff(g1,g2,diff);
int eq = cv::countNonZero(diff);
if(eq ==0)
   cout<<"equal \n";
else 
   cout<<"not equal \n";
if (currentFrame.empty()){
shouldQuit = true; continue;
}
previousFrame = currentFrame;
} while (!shouldQuit);

The problem is that the result is always equal for all the video frames. I don't know where is the error. Could you help me ? Thanks in advance.


Solution

  • After

    previousFrame = currentFrame;
    

    both previousFrame and currentFrame refer to the same array of values.

    You need to create a new cv::Mat object within your loop so that each captured frame does not overwrite the one before.

    So instead of :

    do {
    capture >> currentFrame;
    

    Try:

    do {
    currentFrame = cv::Mat(width, height);
    capture >> currentFrame;