2016-03-11 42 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