主要用于使用 features2d 和 calib3d 模块来检测场景中的已知平面物体。
步骤:
1、读入两幅图像;
<span style="font-size:14px;">Mat img1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);</span>
2、检测两幅图像的关键点(尺度旋转都不发生变化的关键点);
<span style="font-size:14px;">// 对第一幅图像进行关键点检测
FastFeatureDetector detector(15);
vector<KeyPoint> keypoints1;
detector.detect(img1, keypoints1);
... // 对第二幅图像进行关键点检测</span>
3、计算每个关键点的描述向量(Descriptor);
<span style="font-size:14px;">// 计算描述向量
SurfDescriptorExtractor extractor;
Mat descriptors1;
extractor.compute(img1, keypoints1, descriptors1);
... // 计算第二幅图像中的关键点对应的描述向量</span>
4、计算两幅图像中的关键点对应的描述向量距离,寻找两图像中距离最近的描述向量对应的关键点,即为两图像中匹配上的关键点;
// 关键点描述向量匹配
<span style="font-size:14px;">BruteForceMatcher<L2<float> > matcher;
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);</span>
<span style="font-size:14px;">
</span>
<span style="font-size:14px;">5、可视化结果;</span>
// 绘制出结果
namedWindow("matches", 1);
Mat img_matches;
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
imshow("matches", img_matches);
waitKey(0);
6、寻找两个点集合中的单映射变换(homography transformation);
<span style="font-size:14px;">vector<Point2f> points1, points2;
// 用点填充形成矩阵(array)
....
Mat H = findHomography(Mat(points1), Mat(points2), CV_RANSAC, ransacReprojThreshold);</span>
7、创建内匹配点集合同时绘制出匹配上的点。用perspectiveTransform函数来通过单映射来映射点;
<span style="font-size:14px;">Mat points1Projected; </span>
<span style="font-size:14px;">perspectiveTransform(Mat(points1), points1Projected, H);</span>
8、用 drawMatches 来绘制内匹配点;
<span style="font-size:14px;">drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );</span>