进一步匹配(寻找源图与目标图像之间的透视变换)

来源:互联网 发布:linux init3 编辑:程序博客网 时间:2024/06/07 06:48
bool refineMatchesWithHomography(const std::vector<cv::KeyPoint>& queryKeypoints,const std::vector<cv::KeyPoint>& trainKeypoints,  float reprojectionThreshold,std::vector<cv::DMatch>& matches,cv::Mat& homography)  {      const int minNumberMatchesAllowed = 8;      if (matches.size() < minNumberMatchesAllowed)          return false;      // Prepare data for cv::findHomography        std::vector<cv::Point2f> srcPoints(matches.size());      std::vector<cv::Point2f> dstPoints(matches.size());      for (size_t i = 0; i < matches.size(); i++)      {          srcPoints[i] = trainKeypoints[matches[i].trainIdx].pt;          dstPoints[i] = queryKeypoints[matches[i].queryIdx].pt;          //srcPoints[i] = trainKeypoints[i].pt;          //dstPoints[i] = queryKeypoints[i].pt;      }      // Find homography matrix and get inliers mask        std::vector<unsigned char> inliersMask(srcPoints.size());      homography = cv::findHomography(srcPoints,dstPoints,CV_FM_RANSAC,reprojectionThreshold,inliersMask);      std::vector<cv::DMatch> inliers;      for (size_t i = 0; i<inliersMask.size(); i++)      {          if (inliersMask[i])              inliers.push_back(matches[i]);      }      matches.swap(inliers);      return matches.size() > minNumberMatchesAllowed;  }  

参考:

Opencv图像识别从零到精通(23)----轮廓        

opencv3.1.0 特征点检测与图像匹配(features2d、xfeatures2d)

Opencv图像识别从零到精通(30)---重映射,仿射变换

0 0
原创粉丝点击