【问题标题】:How to use opencv feature matching for detecting copy-move forgery如何使用 opencv 特征匹配来检测复制移动伪造
【发布时间】:2016-06-27 08:47:11
【问题描述】:

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

P.S1:我得到了图像的筛选关键点和描述符,并坚持使用特征匹配类。

P.S2:特征匹配的类型对我来说并不重要。

提前致谢。

更新:

这些图片是我需要的一个例子

并且有一个代码匹配两个图像的特征并在两个图像(不是一个)上做类似的事情,android原生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);

【问题讨论】:

  • 显示您当前的代码和您正在使用的图像样本肯定会有所帮助。
  • @user3896254 感谢您的建议,我编辑我的帖子并添加示例和代码
  • @all 你知道如何使用python实现图像中字体(字符/数字)的复制移动伪造吗?
  • @MohamadMohamadPoor 你有没有为这个问题找到满意的答案?

标签: c++ opencv sift flann


【解决方案1】:

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

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

  • 找到一组关键点(SURF、SIFT 或任何您想要的),
  • 使用蛮力匹配器 (cv::BFMatcher) 的 knnMatch 函数计算与所有其他关键点的匹配分数,
  • 保持不同点之间的匹配,即距离大于零(或阈值)的点。

    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);
    

【讨论】:

  • @Evil 这是我会遵循的建议。如果您有需要检测的图像,则使用模板匹配。否则,请使用 Gwen 显示的示例。
  • @Gwen 这周我太忙了,我会试试你的解决方案,让你知道发生了什么,顺便说一句,谢谢你的回答,谢谢你的替代解决方案,但我需要使用关键点。
  • @Gwen 我试过你的示例代码,但最后它没有得到我需要的结果,它给了我很多匹配并且没有显示在单个图像中! !! (显示在两个相同的图像上......),有没有进一步的帮助?提前致谢。
  • @Evil 我更正了距离计算(我使用公制距离而不是像素距离......)。代码未经测试,但想法就在这里。但是,正如我所说,我不能保证这种方法会奏效。
  • @Gwen 感谢您的回复,它不符合我的需要,但感谢您的想法。
猜你喜欢
  • 2014-04-04
  • 1970-01-01
  • 2014-03-19
  • 2015-04-01
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
相关资源
最近更新 更多