MAXSIMJC 2016-01-21 02:28 采纳率: 0%
浏览 4159

单个模板的多目标识别 OPENCV

初学图像处理, 请多多指教!

我现在想解决的是用单个模板去匹配测试图中的多个目标(目标有仿射变换),参照了现成的一些SURF算法提取特征值进行匹配,可是这样只能匹配到一个目标。
后来发现匹配的knnmatch函数可以对一个 query descriptor返回k个最佳匹配,可是效果并不好, 不知有什么建议?十分感谢!

代码如下:

  Mat img1 = imread( "q.jpg", CV_LOAD_IMAGE_GRAYSCALE );
  Mat img2 = imread( "qw.jpg", CV_LOAD_IMAGE_GRAYSCALE );

    //-- Step 1: Detect the keypoints using Sift Detector
  SiftFeatureDetector  detector( hessian );

  vector<KeyPoint> queryKeypoints, trainKeypoints;

  detector.detect( img1, queryKeypoints );
  detector.detect( img2, trainKeypoints );

    //-- Step 2: Extract the keypoints using Sift Extractor
    Mat queryDescriptor,trainDescriptor;// extract keypoints
     SiftDescriptorExtractor extractor;  //Create Descriptor Extractor
    extractor.compute( img1, queryKeypoints, queryDescriptor );
    extractor.compute( img2, trainKeypoints, trainDescriptor );

        //--Step3: Match 
        vector<vector<DMatch>> m_knnMatches;
        vector<DMatch>m_Matches;
        vector<DMatch>n_Matches;
    const float minRatio = 1.f / 1.5f;

        matcher.knnMatch(queryDescriptor,trainDescriptor,m_knnMatches,3);
        for (size_t i=0; i<m_knnMatches.size(); i++)
        {
            const cv::DMatch& bestMatch = m_knnMatches[i][0];
            const cv::DMatch& betterMatch = m_knnMatches[i][1];
            const cv::DMatch& betterrMatch = m_knnMatches[i][2];
            float distanceRatio = bestMatch.distance / betterMatch.distance;
            float distanceRatio1 = betterMatch.distance / betterrMatch.distance;
            if (distanceRatio < minRatio)
        {
                m_Matches.push_back(bestMatch);
            }
            if (distanceRatio1 < minRatio)
            {
                n_Matches.push_back(betterMatch);
            }
        }

        Mat img_matches;
  drawMatches( img1,queryKeypoints,img2,trainKeypoints,m_Matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

        Mat img_matches2;
  drawMatches( img1,queryKeypoints,img2,trainKeypoints,n_Matches, img_matches2, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

  • 写回答

3条回答 默认 最新

  • shiter 人工智能领域优质创作者 2016-01-21 02:46
    关注

    我记得是可以匹配多个的哇,你代码发上来瞅瞅啊,opencv那些函数效果都一般,你得自己多调试些参数

    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)