找到2张图片之间的差异

时间:2013-09-19 20:06:24

标签: c++ opencv image-processing computer-vision feature-detection

想象一下,我有2张图片。两者看起来相同但有差异。例如,image1图片中有一本小册子,但image2没有,但image2中的其他内容与image1相同。

这是我的代码:

#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/nonfree/features2d.hpp>

using namespace cv;

void readme();


int main( int argc, char** argv )
{


  Mat img_object = imread( "C:/Users/Yohan/Pictures/ironManSpecial2.jpg",CV_LOAD_IMAGE_GRAYSCALE);
  Mat img_scene = imread("C:/Users/Yohan/Pictures/noIronMan.jpg",CV_LOAD_IMAGE_GRAYSCALE);

  if( !img_object.data || !img_scene.data )
  { std::cout<< " --(!) Error reading images " << std::endl; return -1; }

  //-- Step 1: Detect the keypoints using SURF Detector
  int minHessian = 400;

  SurfFeatureDetector detector( minHessian );

  std::vector<KeyPoint> keypoints_object, keypoints_scene;

  detector.detect( img_object, keypoints_object );
  detector.detect( img_scene, keypoints_scene );

  //-- Step 2: Calculate descriptors (feature vectors)
  SurfDescriptorExtractor extractor;

  Mat descriptors_object, descriptors_scene;

  extractor.compute( img_object, keypoints_object, descriptors_object );
  extractor.compute( img_scene, keypoints_scene, descriptors_scene );

  //-- Step 3: Matching descriptor vectors using FLANN matcher
  FlannBasedMatcher matcher;
  std::vector< DMatch > matches;
  matcher.match( descriptors_object, descriptors_scene, matches );

  double max_dist = 0; double min_dist = 100;

  //-- Quick calculation of max and min distances between keypoints
  for( int i = 0; i < descriptors_object.rows; i++ )
  { double dist = matches[i].distance;
    if( dist < min_dist ) min_dist = dist;
    if( dist > max_dist ) max_dist = dist;
  }

  printf("-- Max dist : %f \n", max_dist );
  printf("-- Min dist : %f \n", min_dist );

  //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
  std::vector< DMatch > good_matches;

  for( int i = 0; i < descriptors_object.rows; i++ )
  { if( matches[i].distance < 3*min_dist )
    { good_matches.push_back( matches[i]); }
  }  

  Mat img_matches;
  drawMatches( img_object, keypoints_object, img_scene, keypoints_scene, 
               good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), 
               vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS ); 


  //-- Localize the object from img_1 in img_2 
  std::vector<Point2f> obj;
  std::vector<Point2f> scene;

  for( int i = 0; i < good_matches.size(); i++ )
  {
    //-- Get the keypoints from the good matches
    obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
    scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt ); 
  }

  Mat H = findHomography( obj, scene, CV_RANSAC );

  //-- Get the corners from the image_1 ( the object to be "detected" )
  std::vector<Point2f> obj_corners(4);
  obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_object.cols, 0 );
  obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.rows );
  std::vector<Point2f> scene_corners(4);

  perspectiveTransform( obj_corners, scene_corners, H);


  //-- Draw lines between the corners (the mapped object in the scene - image_2 )
  line( img_matches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
  line( img_matches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
  line( img_matches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
  line( img_matches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );

  //-- Show detected matches
  namedWindow( "Good Matches & Object detection", CV_WINDOW_NORMAL );
  imshow( "Good Matches & Object detection", img_matches );

  waitKey(0);

  return 0;
}

以上代码用于查找&#34;匹配&#34;。这是它的结果

enter image description here

你可以看到有一个&#34;钢铁侠3手册&#34;在image1(左侧图像)中但不在image2(右侧图像)中。现在,我需要找到&#34;的东西&#34;在image2中缺少并在控制台中打印消息。

应该考虑的是,这个&#34;钢铁侠3手册&#34;只是一个例子,在实际情况中,我不知道会发生什么。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

您可以尝试使用找到的关键点对齐图像,然后找出像素差异,或应用模板匹配算法。

答案 1 :(得分:2)

您将提前获得有关此图像的哪些信息?根据存在的某些物体,方向,特征等,它们在外观上是否相似?如果您有此信息,则可以使用直接点匹配算法(即:计算奇异值分解等)。

如果没有,最直接的方法是使用一些常用方法来收集一些有用的统计数据。

首先,考虑直方图均衡或直方图归一化,以及一些简单的去噪滤波器(即:你有高斯噪声,需要应用简单的平滑滤波器,或脉冲/椒盐噪声,需要应用一个中位数/模式过滤器?)。

现在,获取一些有关图像的基本统计信息。获得平均强度值,以及每个的方差/标准偏差。如果两个图像之间只是一个小的变化,两个图像的这些值应该非常相似。如果没有,您可能需要重新审视过滤和均衡阶段。

最后,考虑主成分分析(即:计算协方差矩阵,计算其特征值,确定2个最大值,并计算与这些值相关的特征向量)。由此,您可以找到图像最暗或最亮的特征,以及它的对齐和位置(即:质心)。你基本上可以将两个图像对齐,并且方向相同。

有了这个,您可以裁剪/修剪两个图像的边缘,并且您将拥有两个看起来非常相似的对齐图像。现在,应用阈值滤镜会将图像缩小为黑白,并且可以从另一个图像中减去一个图像。通过应用于两个图像的先前操作,两者之间的差异将仅是最不同的特征,并且实际上是以图形方式定义它的掩模。

希望这会有所帮助。如果您探索这种方法,如果您发现它正朝着正确的方向前进,我可以提供更多信息。

祝你好运!

相关问题