2016-03-11 44 views
6

在我的OpenCV的项目,我想检测的图像复制伪造的举动。我知道如何使用opencv FLANN在2个不同的图像中进行特征匹配,但是我对如何使用FLANN检测图像中的复制移动伪造变得非常困惑。如何使用OpenCV的特征匹配检测复制伪造移动

P.S1:我得到的筛关键点和形象的描述,并卡在使用特征匹配类。

P.S2:特征匹配的类型不是对我很重要。

在此先感谢。

更新:

这些图片是什么,我需要

Input Image

Result

一个例子,有一个相匹配两幅图像的特征并做一些喜欢它的代码两个图像(没有一个),在机器人的OpenCV本地格式的代码是象下面这样:

vector<KeyPoint> keypoints; 
     Mat descriptors; 

     // Create a SIFT keypoint detector. 
     SiftFeatureDetector detector; 
     detector.detect(image_gray, keypoints); 
     LOGI("Detected %d Keypoints ...", (int) keypoints.size()); 

     // Compute feature description. 
     detector.compute(image, keypoints, descriptors); 
     LOGI("Compute Feature ..."); 


     FlannBasedMatcher matcher; 
     std::vector<DMatch> matches; 
     matcher.match(descriptors, descriptors, matches); 

     double max_dist = 0; double min_dist = 100; 

     //-- Quick calculation of max and min distances between keypoints 
      for(int i = 0; i < descriptors.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 2*min_dist, 
      //-- or a small arbitary value (0.02) in the event that min_dist is very 
      //-- small) 
      //-- PS.- radiusMatch can also be used here. 
      std::vector<DMatch> good_matches; 

      for(int i = 0; i < descriptors.rows; i++) 
      { if(matches[i].distance <= max(2*min_dist, 0.02)) 
      { good_matches.push_back(matches[i]); } 
      } 

      //-- Draw only "good" matches 
      Mat img_matches; 
      drawMatches(image, keypoints, image, keypoints, 
         good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), 
         vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); 

      //-- Show detected matches 
//   imshow("Good Matches", img_matches); 
      imwrite(imgOutFile, img_matches); 
+1

显示你当前的代码和您正在使用图像的样本肯定是有帮助的。 – alexisrozhkov

+0

@ user3896254谢谢你的建议,我编辑自己的帖子,并添加例子和代码 – Evil

回答

2

我不知道这是否是使用关键点这个问题的好办法。我宁愿测试template matching(使用图像上的滑动窗口作为补丁)。与关键点相比,这种方法的缺点是对旋转和缩放比较敏感。

如果你想使用的关键点,您可以:

  • 找出一组关键点(SURF,过筛,或任何你想要的)的,
  • 计算匹配分数与其他所有关键点,与knnMatch功能的蛮力部队匹配(cv::BFMatcher),
  • 保持区别点之间的匹配,即距离大于零(或阈值)的点。

    int nknn = 10; // max number of matches for each keypoint 
    double minDist = 0.5; // distance threshold 
    
    // Match each keypoint with every other keypoints 
    cv::BFMatcher matcher(cv::NORM_L2, false); 
    std::vector< std::vector<cv::DMatch> > matches; 
    matcher.knnMatch(descriptors, descriptors, matches, nknn); 
    
    double max_dist = 0; double min_dist = 100; 
    
    //-- Quick calculation of max and min distances between keypoints 
    for(int i = 0; i < descriptors.rows; i++) 
    { 
        double dist = matches[i].distance; 
        if(dist < min_dist) min_dist = dist; 
        if(dist > max_dist) max_dist = dist; 
    } 
    
    // Compute distance and store distant matches 
    std::vector<cv::DMatch> good_matches; 
    for (int i = 0; i < matches.size(); i++) 
    { 
        for (int j = 0; j < matches[i].size(); j++) 
        { 
         // The METRIC distance 
         if(matches[i][j].distance> max(2*min_dist, 0.02)) 
          continue; 
    
         // The PIXELIC distance 
         Point2f pt1 = keypoints[queryIdx].pt; 
         Point2f pt2 = keypoints[trainIdx].pt; 
    
         double dist = cv::norm(pt1 - pt2); 
         if (dist > minDist) 
          good_matches.push_back(matches[i][j]); 
        } 
    } 
    
    Mat img_matches; 
    drawMatches(image_gray, keypoints, image_gray, keypoints, good_matches, img_matches); 
    
+1

@Evil这是我会照做。如果您有需要检测的图像,请使用模板匹配。否则,请按照Gwen所示的示例进行操作。 – John

+0

@Gwen本周我很忙,我会尝试你的解决方案,让你知道发生了什么,顺便说一句,谢谢你的回答,并感谢替代解决方案,但我需要使用关键点。 – Evil

+0

@Gwen我已经试过了你的示例代码,但最后它并没有给我我需要的结果,它给了我很多匹配,并且不显示在单个图像中! (在两个相同的图像旁边显示......),有没有进一步的帮助?提前致谢。 – Evil