sift特征检测,返回内点个数和透视变换矩阵:
int siftmatch(Mat img1, Mat img2, Mat* H)
{
//一、检测特征点
Ptrfeature = xfeatures2d::SIFT::create();//创建SIFT特征类
vectorkeypoints1, keypoints2;
feature->detect(img1, keypoints1);//检测特征点,检测信息保存在keypoint中
feature->detect(img2, keypoints2);
//二、计算描述矩阵并匹配
Mat description1, description2;//初始化描述矩阵
feature->compute(img1, keypoints1, description1);//计算描述矩阵,保存在description中
feature->compute(img2, keypoints2, description2);
vectormatches; //匹配矩阵
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::vectorobj, scene;
for (size_t i = 0; iinliersMask(obj.size());
*H = findHomography(scene, obj, CV_FM_RANSAC, 3.0, inliersMask, 100);
vectorinliers;
for (size_t i = 0; i(i);
// const uchar* img2ptr = img1.ptr(i);
// uchar* outdata = image_match2.ptr(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
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 cb = makePtr();
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(), tempMask.ptr(), 1, npoints );
npoints = compressElems( dst.ptr(), tempMask.ptr(), 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());
createLMSolver(makePtr(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;
}
你可能感兴趣的:(opencv)