Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Because images looks very similar you could try matchTemplate. Cross Correlation is quite invariant to brightness variation, using TM_CCORR_NORMED methos you can get a 0..1 score for the matching.

Using this code I get score=90.6% comparing your images and score=79.4% comparing 2 completely different images.

int main()
{
  Mat img1, img2;
  img1=imread("img1.jpg");
  img2=imread("img2.jpg");
  //resize to the smallest
  //resize big images to improve speed
  //this steps can be optimized
  if (img1.size().area() < img2.size().area())
    cv::resize(img2, img2, img1.size());
  else
    cv::resize(img1, img1, img2.size());
  if (img1.size().area() > 800 * 800)
  {
    cv::resize(img1, img1, Size(0, 0), 0.5, 0.5);
    cv::resize(img2, img2, Size(0, 0), 0.5, 0.5);
  }
  //normalization or convert to gray 
  //doesn't improve result so much with TM_CCORR
  /*
  cvtColor(img1, img1, CV_BGR2GRAY);
  cvtColor(img2, img2, CV_BGR2GRAY);
  */
  Mat matchResult(Size(1, 1), CV_32F);
  matchTemplate(img1, img2, matchResult, TM_CCORR_NORMED);
  double score = matchResult.at<float>(0, 0);
  // Sincerely match 2 completely different images gives score~0.8
  if (score > 0.9)
    cout << "Match with score " << 100.0*score << "%" << endl;
  else
    cout << "NOT match. score " << 100.0*score << "%" << endl;
  return 0;
}

Because images looks very similar you could try matchTemplate. Cross Correlation is quite invariant to brightness variation, variation and using TM_CCORR_NORMED methos method you can get a 0..1 score for the matching.

Using this code I get score=90.6%score=91.6% comparing your images and score=79.4% comparing 2 completely different images.

int main()
{
  Mat img1, img2;
  img1=imread("img1.jpg");
  img2=imread("img2.jpg");
  //with TM_CCORR, normalization doesn't improve score so much 
  //but convert to gray improves speed 
  cvtColor(img1, img1, CV_BGR2GRAY);
  cvtColor(img2, img2, CV_BGR2GRAY);

  //resize to the smallest
  //resize big images to improve speed
  //this steps can be optimized
  if (img1.size().area() < img2.size().area())
    cv::resize(img2, img2, img1.size());
  else
    cv::resize(img1, img1, img2.size());
  if (img1.size().area() > 800 * 800)
  {
    cv::resize(img1, img1, Size(0, 0), 0.5, 0.5);
    cv::resize(img2, img2, Size(0, 0), 0.5, 0.5);
  }
  //normalization or convert to gray 
  //doesn't improve result so much with TM_CCORR
  /*
  cvtColor(img1, img1, CV_BGR2GRAY);
  cvtColor(img2, img2, CV_BGR2GRAY);
  */
  Mat matchResult(Size(1, 1), CV_32F);
  matchTemplate(img1, img2, matchResult, TM_CCORR_NORMED);
  double score = matchResult.at<float>(0, 0);
  // Sincerely match 2 completely different images gives score~0.8
  if (score > 0.9)
    cout << "Match with score " << 100.0*score << "%" << endl;
  else
    cout << "NOT match. score " =" << 100.0*score << "%" << endl;
  return 0;
}