赞
踩
sift特征检测,返回内点个数和透视变换矩阵:
- int siftmatch(Mat img1, Mat img2, Mat* H)
- {
- //一、检测特征点
- Ptr<xfeatures2d::SIFT>feature = xfeatures2d::SIFT::create();//创建SIFT特征类
- vector<KeyPoint>keypoints1, keypoints2;
- feature->detect(img1, keypoints1);//检测特征点,检测信息保存在keypoint中
- feature->detect(img2, keypoints2);
-
- //二、计算描述矩阵并匹配
-
- Mat description1, description2;//初始化描述矩阵
-
- feature->compute(img1, keypoints1, description1);//计算描述矩阵,保存在description中
- feature->compute(img2, keypoints2, description2);
-
- vector<DMatch>matches; //匹配矩阵
- BFMatcher matcher;
- matcher.match(description1, description2, matches);
-
- //Mat image_match2;
- //drawMatches(img1, keypoints1, img2, keypoints2, matches, image_match2);
- //imshow("匹配后的图片2", image_match2);
-
- //三、采用findHomography函数进行RANSAC筛选
-
- std::vector<Point2f>obj, scene;
-
- for (size_t i = 0; i<matches.size(); i++)
- {
- //-- Get the keypoints from the good matches
- obj.push_back(keypoints1[matches[i].queryIdx].pt);
- scene.push_back(keypoints2[matches[i].trainIdx].pt);
- }
-
- vector<uchar>inliersMask(obj.size());
- *H = findHomography(scene, obj, CV_FM_RANSAC, 3.0, inliersMask, 100);
-
- vector<DMatch>inliers;
- for (size_t i = 0; i<inliersMask.size(); i++) {
- if (inliersMask[i])
- inliers.push_back(matches[i]);
- }
- matches.swap(inliers);
-
- //画线
- //cout << "内点数为:" << matches.size() << endl;
- //Mat image_match2 = Mat(img1.rows, img1.cols + img2.cols, CV_8UC1, Scalar(0));
- //for (int i = 0; i < img1.rows; i++)
- //{
- // const uchar* img1ptr = img1.ptr<uchar>(i);
- // const uchar* img2ptr = img1.ptr<uchar>(i);
- // uchar* outdata = image_match2.ptr<uchar>(i);
- // for (int j = 0; j < img1.cols; j++)//将图1和图2拼在一起
- // {
- // outdata[j] = img1ptr[j];
- // outdata[j + img1.cols] = img2ptr[j];
- // }
- //}
- //Point pt1, pt2;//连线的两个端点
- //for (size_t i = 0; i<matches.size(); i++)
- //{
- // //-- Get the keypoints from the good matches
- // pt1 = keypoints1[matches[i].queryIdx].pt;
- // pt2 = keypoints2[matches[i].trainIdx].pt;
- // pt2.x += img1.cols;
- // line(image_match2, pt1, pt2, CV_RGB(255, 0, 255));
- //}
- //imshow("匹配后的图片2", image_match2);
-
- return matches.size();
- }
findHomography函数:
_points1和_points2为初步计算的匹配点,用来通过RANSAC算法筛选
method为筛选方法,这里为CV_FM_RANSAC
- cv::Mat cv::findHomography( InputArray _points1, InputArray _points2,
- int method, double ransacReprojThreshold, OutputArray _mask,
- const int maxIters, const double confidence)
- {
- CV_INSTRUMENT_REGION()//应该是OpenCV相关算法表现性能测试框架,测量函数执行时间,在函数内部追踪函数执行状况
-
- const double defaultRANSACReprojThreshold = 3;//默认拒绝阈值
- bool result = false;
-
- Mat points1 = _points1.getMat(), points2 = _points2.getMat();//用矩阵保存
- Mat src, dst, H, tempMask;
- int npoints = -1;
-
- for( int i = 1; i <= 2; i++ )
- {
- Mat& p = i == 1 ? points1 : points2;
- Mat& m = i == 1 ? src : dst;
- npoints = p.checkVector(2, -1, false);
- if( npoints < 0 )
- {
- npoints = p.checkVector(3, -1, false);
- if( npoints < 0 )
- CV_Error(Error::StsBadArg, "The input arrays should be 2D or 3D point sets");
- if( npoints == 0 )
- return Mat();
- convertPointsFromHomogeneous(p, p);
- }
- p.reshape(2, npoints).convertTo(m, CV_32F);
- }
-
- CV_Assert( src.checkVector(2) == dst.checkVector(2) );
-
- if( ransacReprojThreshold <= 0 )
- ransacReprojThreshold = defaultRANSACReprojThreshold;
-
- Ptr<PointSetRegistrator::Callback> cb = makePtr<HomographyEstimatorCallback>();
-
- if( method == 0 || npoints == 4 )
- {
- tempMask = Mat::ones(npoints, 1, CV_8U);
- result = cb->runKernel(src, dst, H) > 0;
- }
- else if( method == RANSAC )
- result = createRANSACPointSetRegistrator(cb, 4, ransacReprojThreshold, confidence, maxIters)->run(src, dst, H, tempMask);
- else if( method == LMEDS )
- result = createLMeDSPointSetRegistrator(cb, 4, confidence, maxIters)->run(src, dst, H, tempMask);
- else if( method == RHO )
- result = createAndRunRHORegistrator(confidence, maxIters, ransacReprojThreshold, npoints, src, dst, H, tempMask);
- else
- CV_Error(Error::StsBadArg, "Unknown estimation method");
-
- if( result && npoints > 4 && method != RHO)
- {
- compressElems( src.ptr<Point2f>(), tempMask.ptr<uchar>(), 1, npoints );
- npoints = compressElems( dst.ptr<Point2f>(), tempMask.ptr<uchar>(), 1, npoints );
- if( npoints > 0 )
- {
- Mat src1 = src.rowRange(0, npoints);
- Mat dst1 = dst.rowRange(0, npoints);
- src = src1;
- dst = dst1;
- if( method == RANSAC || method == LMEDS )
- cb->runKernel( src, dst, H );
- Mat H8(8, 1, CV_64F, H.ptr<double>());
- createLMSolver(makePtr<HomographyRefineCallback>(src, dst), 10)->run(H8);
- }
- }
-
- if( result )
- {
- if( _mask.needed() )
- tempMask.copyTo(_mask);
- }
- else
- {
- H.release();
- if(_mask.needed() ) {
- tempMask = Mat::zeros(npoints >= 0 ? npoints : 0, 1, CV_8U);
- tempMask.copyTo(_mask);
- }
- }
-
- return H;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。