//RANSAC算法实现
vector<DMatch> ransac(vector<DMatch> matches,vector<KeyPoint> queryKeyPoint,vector<KeyPoint> trainKeyPoint)
{
//定义保存匹配点对坐标
vector<Point2f> srcPoints(matches.size()),dstPoints(matches.size());
//保存从关键点中提取到的匹配点对的坐标
for(int i=0;i<matches.size();i++)
{
srcPoints[i]=queryKeyPoint[matches[i].queryIdx].pt;
dstPoints[i]=trainKeyPoint[matches[i].trainIdx].pt;
}
//保存计算的单应性矩阵
Mat homography;
//保存点对是否保留的标志
vector<unsigned char> inliersMask(srcPoints.size());
//匹配点对进行RANSAC过滤
homography = findHomography(srcPoints,dstPoints,CV_RANSAC,5,inliersMask);
//RANSAC过滤后的点对匹配信息
vector<DMatch> matches_ransac;
//手动的保留RANSAC过滤后的匹配点对
for(int i=0;i<inliersMask.size();i++)
{
if(inliersMask[i])
{
matches_ransac.push_back(matches[i]);
//cout<<"第"<<i<<"对匹配:"<<endl;
//cout<<"queryIdx:"<<matches[i].queryIdx<<"\ttrainIdx:"<<matches[i].trainIdx<<endl;
//cout<<"imgIdx:"<<matches[i].imgIdx<<"\tdistance:"<<matches[i].distance<<endl;
}
}
//返回RANSAC过滤后的点对匹配信息
return matches_ransac;
}
用Opencv实现RANSAC过滤关键点匹配对
最新推荐文章于 2025-05-10 09:30:19 发布