当前位置:   article > 正文

opencv3中SIFT图像拼接代码详解(findHomography)_the input arrays should be 2d or 3d point sets in

the input arrays should be 2d or 3d point sets in function 'cv::findhomograp

sift特征检测,返回内点个数和透视变换矩阵:

  1. int siftmatch(Mat img1, Mat img2, Mat* H)
  2. {
  3. //一、检测特征点
  4. Ptr<xfeatures2d::SIFT>feature = xfeatures2d::SIFT::create();//创建SIFT特征类
  5. vector<KeyPoint>keypoints1, keypoints2;
  6. feature->detect(img1, keypoints1);//检测特征点,检测信息保存在keypoint中
  7. feature->detect(img2, keypoints2);
  8. //二、计算描述矩阵并匹配
  9. Mat description1, description2;//初始化描述矩阵
  10. feature->compute(img1, keypoints1, description1);//计算描述矩阵,保存在description中
  11. feature->compute(img2, keypoints2, description2);
  12. vector<DMatch>matches; //匹配矩阵
  13. BFMatcher matcher;
  14. matcher.match(description1, description2, matches);
  15. //Mat image_match2;
  16. //drawMatches(img1, keypoints1, img2, keypoints2, matches, image_match2);
  17. //imshow("匹配后的图片2", image_match2);
  18. //三、采用findHomography函数进行RANSAC筛选
  19. std::vector<Point2f>obj, scene;
  20. for (size_t i = 0; i<matches.size(); i++)
  21. {
  22. //-- Get the keypoints from the good matches
  23. obj.push_back(keypoints1[matches[i].queryIdx].pt);
  24. scene.push_back(keypoints2[matches[i].trainIdx].pt);
  25. }
  26. vector<uchar>inliersMask(obj.size());
  27. *H = findHomography(scene, obj, CV_FM_RANSAC, 3.0, inliersMask, 100);
  28. vector<DMatch>inliers;
  29. for (size_t i = 0; i<inliersMask.size(); i++) {
  30. if (inliersMask[i])
  31. inliers.push_back(matches[i]);
  32. }
  33. matches.swap(inliers);
  34. //画线
  35. //cout << "内点数为:" << matches.size() << endl;
  36. //Mat image_match2 = Mat(img1.rows, img1.cols + img2.cols, CV_8UC1, Scalar(0));
  37. //for (int i = 0; i < img1.rows; i++)
  38. //{
  39. // const uchar* img1ptr = img1.ptr<uchar>(i);
  40. // const uchar* img2ptr = img1.ptr<uchar>(i);
  41. // uchar* outdata = image_match2.ptr<uchar>(i);
  42. // for (int j = 0; j < img1.cols; j++)//将图1和图2拼在一起
  43. // {
  44. // outdata[j] = img1ptr[j];
  45. // outdata[j + img1.cols] = img2ptr[j];
  46. // }
  47. //}
  48. //Point pt1, pt2;//连线的两个端点
  49. //for (size_t i = 0; i<matches.size(); i++)
  50. //{
  51. // //-- Get the keypoints from the good matches
  52. // pt1 = keypoints1[matches[i].queryIdx].pt;
  53. // pt2 = keypoints2[matches[i].trainIdx].pt;
  54. // pt2.x += img1.cols;
  55. // line(image_match2, pt1, pt2, CV_RGB(255, 0, 255));
  56. //}
  57. //imshow("匹配后的图片2", image_match2);
  58. return matches.size();
  59. }

findHomography函数:

_points1和_points2为初步计算的匹配点,用来通过RANSAC算法筛选
method为筛选方法,这里为CV_FM_RANSAC

  1. cv::Mat cv::findHomography( InputArray _points1, InputArray _points2,
  2. int method, double ransacReprojThreshold, OutputArray _mask,
  3. const int maxIters, const double confidence)
  4. {
  5. CV_INSTRUMENT_REGION()//应该是OpenCV相关算法表现性能测试框架,测量函数执行时间,在函数内部追踪函数执行状况
  6. const double defaultRANSACReprojThreshold = 3;//默认拒绝阈值
  7. bool result = false;
  8. Mat points1 = _points1.getMat(), points2 = _points2.getMat();//用矩阵保存
  9. Mat src, dst, H, tempMask;
  10. int npoints = -1;
  11. for( int i = 1; i <= 2; i++ )
  12. {
  13. Mat& p = i == 1 ? points1 : points2;
  14. Mat& m = i == 1 ? src : dst;
  15. npoints = p.checkVector(2, -1, false);
  16. if( npoints < 0 )
  17. {
  18. npoints = p.checkVector(3, -1, false);
  19. if( npoints < 0 )
  20. CV_Error(Error::StsBadArg, "The input arrays should be 2D or 3D point sets");
  21. if( npoints == 0 )
  22. return Mat();
  23. convertPointsFromHomogeneous(p, p);
  24. }
  25. p.reshape(2, npoints).convertTo(m, CV_32F);
  26. }
  27. CV_Assert( src.checkVector(2) == dst.checkVector(2) );
  28. if( ransacReprojThreshold <= 0 )
  29. ransacReprojThreshold = defaultRANSACReprojThreshold;
  30. Ptr<PointSetRegistrator::Callback> cb = makePtr<HomographyEstimatorCallback>();
  31. if( method == 0 || npoints == 4 )
  32. {
  33. tempMask = Mat::ones(npoints, 1, CV_8U);
  34. result = cb->runKernel(src, dst, H) > 0;
  35. }
  36. else if( method == RANSAC )
  37. result = createRANSACPointSetRegistrator(cb, 4, ransacReprojThreshold, confidence, maxIters)->run(src, dst, H, tempMask);
  38. else if( method == LMEDS )
  39. result = createLMeDSPointSetRegistrator(cb, 4, confidence, maxIters)->run(src, dst, H, tempMask);
  40. else if( method == RHO )
  41. result = createAndRunRHORegistrator(confidence, maxIters, ransacReprojThreshold, npoints, src, dst, H, tempMask);
  42. else
  43. CV_Error(Error::StsBadArg, "Unknown estimation method");
  44. if( result && npoints > 4 && method != RHO)
  45. {
  46. compressElems( src.ptr<Point2f>(), tempMask.ptr<uchar>(), 1, npoints );
  47. npoints = compressElems( dst.ptr<Point2f>(), tempMask.ptr<uchar>(), 1, npoints );
  48. if( npoints > 0 )
  49. {
  50. Mat src1 = src.rowRange(0, npoints);
  51. Mat dst1 = dst.rowRange(0, npoints);
  52. src = src1;
  53. dst = dst1;
  54. if( method == RANSAC || method == LMEDS )
  55. cb->runKernel( src, dst, H );
  56. Mat H8(8, 1, CV_64F, H.ptr<double>());
  57. createLMSolver(makePtr<HomographyRefineCallback>(src, dst), 10)->run(H8);
  58. }
  59. }
  60. if( result )
  61. {
  62. if( _mask.needed() )
  63. tempMask.copyTo(_mask);
  64. }
  65. else
  66. {
  67. H.release();
  68. if(_mask.needed() ) {
  69. tempMask = Mat::zeros(npoints >= 0 ? npoints : 0, 1, CV_8U);
  70. tempMask.copyTo(_mask);
  71. }
  72. }
  73. return H;
  74. }





声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/153425?site
推荐阅读
相关标签
  

闽ICP备14008679号