开发者

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

开发者 https://www.devze.com 2023-03-18 07:53 出处:网络
So we try code like: cv::Mat m1, m2; cv::VideoCapture cap(0); do { cap >> m1; cap >> m2; }while(cv::norm(m1,m2)=开发者_Python百科=0);

So we try code like:

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

do {
    cap >> m1;
    cap >> m2;
}   while(cv::norm(m1,m2)=开发者_Python百科=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?


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.


Google "opencv difference" =>

  • http://andybest.net/2009/02/processing-opencv-tutorial-2-bubbles/

  • http://ubaa.net/shared/processing/opencv/opencv_absdiff.html

  • http://createdigitalmotion.com/2009/02/processing-tutorials-getting-started-with-video-processing-via-opencv/

    opencv.absDiff (), etc


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号