Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why are the two images equal?

in my program, main calls a function which takes two pictures with a USB camera and writes them into a vector passed by reference to it. Due to an error I'm unable to find (for 2 days now...) the two images written into the vector are the same after I return to main, even if they should not be the same, as they were taken with a wait time in between, and I'm waving in front of the camera. Can some of you code angels spot an error I did?

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/videoio/videoio.hpp>

using namespace std;


//---------FUNCTION DECLARATIONS----------------
void       takePictures(vector<cv::Mat>&);
bool       equal(const cv::Mat&, const cv::Mat&);




//---------MAIN---------------------------------
int main(int argc, char* argv[])
{

  // Take pictures
  vector<cv::Mat> images;
  takePictures( images);

  bool sameImg = equal(images[0],images[1]);

  return 0;
}


//----------IMAGE TAKING FUNCTION---------------
void takePictures(vector<cv::Mat>& images)
{
  // open cameras
  cv::VideoCapture cap1(0);
  cv::Mat img1;

  // Picture taking procedure
  for ( int i = 1; i <= 2; i++ )
  {
    cap1 >> img1;
    images.push_back(img1);
    cv::waitKey(1000);
  }
}




bool equal(const cv::Mat & a, const cv::Mat & b)
{
    if ( (a.rows != b.rows) || (a.cols != b.cols) )
        return false;
    cv::Scalar s = sum( a - b );
    return (s[0]==0) && (s[1]==0) && (s[2]==0);
}

Thank you a lot in advance for your help.

Why are the two images equal?

in my program, main calls a function which takes two pictures with a USB camera and writes them into a vector passed by reference to it. Due to an error I'm unable to find (for 2 days now...) the two images written into the vector are the same identical after I return to main, main (I test this with the function "equal(...)"), even if they should not be the same, as they were taken with a wait time in between, and I'm waving in front of the camera. Can some of you code angels spot an error I did?did? The equal() function is not the problem, I have tested it thoroughly.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/videoio/videoio.hpp>

using namespace std;


//---------FUNCTION DECLARATIONS----------------
void       takePictures(vector<cv::Mat>&);
bool       equal(const cv::Mat&, const cv::Mat&);




//---------MAIN---------------------------------
int main(int argc, char* argv[])
{

  // Take pictures
  vector<cv::Mat> images;
  takePictures( images);

  bool sameImg = equal(images[0],images[1]);

  return 0;
}


//----------IMAGE TAKING FUNCTION---------------
void takePictures(vector<cv::Mat>& images)
{
  // open cameras
  cv::VideoCapture cap1(0);
  cv::Mat img1;

  // Picture taking procedure
  for ( int i = 1; i <= 2; i++ )
  {
    cap1 >> img1;
    images.push_back(img1);
    cv::waitKey(1000);
  }
}




bool equal(const cv::Mat & a, const cv::Mat & b)
{
    if ( (a.rows != b.rows) || (a.cols != b.cols) )
        return false;
    cv::Scalar s = sum( a - b );
    return (s[0]==0) && (s[1]==0) && (s[2]==0);
}

Thank you a lot in advance for your help.