0

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?

Rella
  • 65,003
  • 109
  • 363
  • 636

3 Answers3

2

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.

ArtemStorozhuk
  • 8,715
  • 4
  • 35
  • 53
  • I'm upvoting, because I didn't noticed that him is using a cv::capture to get the frames, wich, of course will never pass on my answer. – Ian Medeiros Jul 17 '12 at 19:08
0

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'.

Ian Medeiros
  • 1,746
  • 9
  • 24
  • NO!!!! NOT AT ALL!. cv::norm will use the distance between the pixels to return the SUM OF DIFFERENCES. So, difference in one pixel can be compensated by differences in other ones. For example, imagine that norm returns 0. All the pixels are identical, excluding the pixels (50,50) and (51,51). The difference in (50,50) is -1, and on (51,51) is 1. This case still returns 0 with norm, but will not pass in my loop. – Ian Medeiros Jul 17 '12 at 19:06
  • Emm... take a look at doc: http://opencv.willowgarage.com/documentation/cpp/core_operations_on_arrays.html#cv-norm difference can't be smaller than zero (with any normType)... – ArtemStorozhuk Jul 17 '12 at 19:16
  • Norm return's the sum of differences of ALL pixels, witch doesn't account for differences in INDIVIDUAL pixels. Which part of documentation disqualify my comment? – Ian Medeiros Jul 17 '12 at 21:04
  • Oh! I get it. It's possible to get the max difference between all pixels. Yeah, if him use the flag NORM_INF, using the norm will actually do the same as my loop. – Ian Medeiros Jul 17 '12 at 21:08
0

Google "opencv difference" =>

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I'd love not to use open frameworks and stick to native for OpenCV 2.2 C++ api. also opencv.absDiff () does not return bool which I asked for. – Rella Jul 09 '11 at 23:02