SIFT输出特征匹配C++代码
时间: 2023-12-18 16:03:44 浏览: 93
以下是SIFT输出特征匹配的C++代码示例:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img1 = imread("img1.jpg", IMREAD_GRAYSCALE);
Mat img2 = imread("img2.jpg", IMREAD_GRAYSCALE);
//检测SIFT特征点
Ptr<Feature2D> detector = SIFT::create();
vector<KeyPoint> keypoints1, keypoints2;
detector->detect(img1, keypoints1);
detector->detect(img2, keypoints2);
//计算SIFT描述子
Mat descriptors1, descriptors2;
detector->compute(img1, keypoints1, descriptors1);
detector->compute(img2, keypoints2, descriptors2);
//进行特征点匹配
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create(DescriptorMatcher::FLANNBASED);
vector<DMatch> matches;
matcher->match(descriptors1, descriptors2, matches);
//绘制匹配结果
Mat img_matches;
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
imshow("Matches", img_matches);
waitKey(0);
return 0;
}
```
以上代码首先读取了两张图片,然后使用SIFT算法检测特征点和计算描述子。接着使用FLANN算法对特征点进行匹配,并绘制匹配结果。最后显示匹配结果图像,并等待按键退出程序。
阅读全文