Rella
Rella

Reputation: 66935

OpenCV how to get bool true if images are `==`and false if not?

So we try code like:

cv::Mat m1, m2;
cv::VideoCapture cap(0);

do {
    cap >> m1;
    cap >> m2;
}   while(cv::norm(m1,m2)==0);
frames+=2;
     //...

but it seems not to work. So how to get bool if frames data contents captured from camera are same or not?

Upvotes: 0

Views: 2276

Answers (3)

ArtemStorozhuk
ArtemStorozhuk

Reputation: 8725

Your method fails because in real camera videostream (from your code I see that you capture from camera) every two frames are not equal because of noise, changing illumination, small camera motion etc. You can change your check to this:

cv::norm(m1,m2) < epsilon

Where epsilon is some unsigned number which you can find by yourself (it depends on your criteria). This is very fast and simple solution.

Look at karlphillip's link to get more efficient solution.

Upvotes: 2

Ian Medeiros
Ian Medeiros

Reputation: 1776

There is no such a function I'm aware off in OpenCV, but it can easily be implemented. Any function that return's a sum of the elements will be wrong, because differences in one pixel can be compensated by differences in other pixels. The only way to guarantee correctness is by doing a pixel by pixel check. Here is a simple example:

template<typename T>
bool identical(cv::Mat m1, cv::Mat m2)
{
    if(m1.cols != m2.cols || m1.rows != m2.rows)
      return false;

    for(int i=0; i<m1.rows; i++)
    {
       for(int j=0; j<m1.cols; j++)
       {
          if(m1.at<T>(i, j) != m2.at<T>(i, j))
            return false;
       }
    }

    return true;
}

I didn't checked the code, so, be careful just doing a 'ctrl+c'.

Upvotes: 0

Related Questions