#include <iostream>
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat obj = imread("F:\\Picture\\obj.jpg"); //载入目标图像
Mat scene = imread("F:\\Picture\\scene.jpg"); //载入场景图像
if (obj.empty() || scene.empty())
{
cout << "Can't open the picture!\n";
return 0;
}
vector<KeyPoint> obj_keypoints, scene_keypoints;
Mat obj_descriptors, scene_descriptors;
ORB detector; //采用ORB算法提取特征点
detector.detect(obj, obj_keypoints);
detector.detect(scene, scene_keypoints);
detector.compute(obj, obj_keypoints, obj_descriptors);
detector.compute(scene, scene_keypoints, scene_descriptors);
BFMatcher matcher(NORM_HAMMING, true); //汉明距离做为相似度度量
vector<DMatch> matches;
matcher.match(obj_descriptors, scene_descriptors, matches);
Mat match_img;
drawMatches(obj, obj_keypoints, scene, scene_keypoints, matches, match_img);
imshow("滤除误匹配前", match_img);
//保存匹配对序号
vector<int> queryIdxs(matches.size()), trainIdxs(matches.size());
for (size_t i = 0; i < matches.size(); i++)
{
queryIdxs[i] = matches[i].queryIdx;
trainIdxs[i] = matches[i].trainIdx;
}
Mat H12; //变换矩阵
vector<Point2f> points1; KeyPoint::convert(obj_keypoints, points1, queryIdxs);
vector<Point2f> points2; KeyPoint::convert(scene_keypoints, points2, trainIdxs);
int ransacReprojThreshold = 5; //拒绝阈值
H12 = findHomography(Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold);
vector<char> matchesMask(matches.size(), 0);
Mat points1t;
perspectiveTransform(Mat(points1), points1t, H12);
for (size_t i1 = 0; i1 < points1.size(); i1++) //保存‘内点’
{
if (norm(points2[i1] - points1t.at<Point2f>((int)i1, 0)) <= ransacReprojThreshold) //给内点做标记
{
matchesMask[i1] = 1;
}
}
Mat match_img2; //滤除‘外点’后
drawMatches(obj, obj_keypoints, scene, scene_keypoints, matches, match_img2, Scalar(0, 0, 255), Scalar::all(-1), matchesMask);
//画出目标位置
std::vector<Point2f> obj_corners(4);
obj_corners[0] = cvPoint(0, 0); obj_corners[1] = cvPoint(obj.cols, 0);
obj_corners[2] = cvPoint(obj.cols, obj.rows); obj_corners[3] = cvPoint(0, obj.rows);
std::vector<Point2f> scene_corners(4);
perspectiveTransform(obj_corners, scene_corners, H12);
line(match_img2, scene_corners[0] + Point2f(static_cast<float>(obj.cols), 0),
scene_corners[1] + Point2f(static_cast<float>(obj.cols), 0), Scalar(0, 0, 255), 2);
line(match_img2, scene_corners[1] + Point2f(static_cast<float>(obj.cols), 0),
scene_corners[2] + Point2f(static_cast<float>(obj.cols), 0), Scalar(0, 0, 255), 2);
line(match_img2, scene_corners[2] + Point2f(static_cast<float>(obj.cols), 0),
scene_corners[3] + Point2f(static_cast<float>(obj.cols), 0), Scalar(0, 0, 255), 2);
line(match_img2, scene_corners[3] + Point2f(static_cast<float>(obj.cols), 0),
scene_corners[0] + Point2f(static_cast<float>(obj.cols), 0), Scalar(0, 0, 255), 2);
imshow("滤除误匹配后", match_img2);
waitKey(0);
return 0;
}
RANSAC随机采样一致性筛选特征点
最新推荐文章于 2024-08-26 13:51:08 发布
本文介绍了一种基于ORB算法的目标检测方法。该方法通过提取并匹配两幅图像中的ORB特征点来确定目标的位置,并使用RANSAC算法过滤错误匹配,提高定位准确性。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
ACE-Step
音乐合成
ACE-Step
ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言
6944

被折叠的 条评论
为什么被折叠?



