如何在 C++ 的 OpenCV 4.2.0 中使用 SIFT?
How do I use SIFT in OpenCV 4.2.0 with C++?
我用的是visual studio2017,安装opencv和opencv verison of 4.2.0,用cmake生成文件。 xfeatured2d420.lib 与编译器链接。还有#include "opencv2/xfeatures2d.hpp"
#include "opencv2/xfeatures2d/nonfree.hpp" 包含在内。使用 xfeatures2d::Sift 提取特征给我内存错误。我需要从两个图像计算筛选关键点。
Mat img_1 = imread("C:/Users/Dan/Desktop/0.jpg", 1);
Mat img_2 = imread("C:/Users/Dan/Desktop/0.jpg", 1);
cv::Ptr<Feature2D> f2d = xfeatures2d::SIFT::create();
std::vector<KeyPoint> keypoints_1, keypoints_2;
f2d->detect(img_1, keypoints_1);
f2d->detect(img_2, keypoints_2);
Mat descriptors_1, descriptors_2;
f2d->compute(img_1, keypoints_1, descriptors_1);
f2d->compute(img_2, keypoints_2, descriptors_2);
BFMatcher matcher;
std::vector< DMatch > matches; matcher.match(descriptors_1,
descriptors_2, matches);
vector<cv::DMatch> good_matches;
for (int i = 0; i < matches.size(); ++i)
{
const float ratio = 0.8;
if (matches[i][0].distance < ratio * matches[i]
[1].distance)
{
good_matches.push_back(matches[i][0]);
}
}
vector<Point2f> points1, points2;
for (int i = 0; i < good_matches.size(); i++)
{
//-- Get the keypoints from the good matches
points1.push_back(keypoints_1[good_matches[i].queryIdx].pt);
points2.push_back(keypoints_2[good_matches[i].trainIdx].pt);
}
/* Find Homography */
Mat H = findHomography(Mat(points2), Mat(points1), RANSAC);
我用的是visual studio2017,安装opencv和opencv verison of 4.2.0,用cmake生成文件。 xfeatured2d420.lib 与编译器链接。还有#include "opencv2/xfeatures2d.hpp" #include "opencv2/xfeatures2d/nonfree.hpp" 包含在内。使用 xfeatures2d::Sift 提取特征给我内存错误。我需要从两个图像计算筛选关键点。
Mat img_1 = imread("C:/Users/Dan/Desktop/0.jpg", 1);
Mat img_2 = imread("C:/Users/Dan/Desktop/0.jpg", 1);
cv::Ptr<Feature2D> f2d = xfeatures2d::SIFT::create();
std::vector<KeyPoint> keypoints_1, keypoints_2;
f2d->detect(img_1, keypoints_1);
f2d->detect(img_2, keypoints_2);
Mat descriptors_1, descriptors_2;
f2d->compute(img_1, keypoints_1, descriptors_1);
f2d->compute(img_2, keypoints_2, descriptors_2);
BFMatcher matcher;
std::vector< DMatch > matches; matcher.match(descriptors_1,
descriptors_2, matches);
vector<cv::DMatch> good_matches;
for (int i = 0; i < matches.size(); ++i)
{
const float ratio = 0.8;
if (matches[i][0].distance < ratio * matches[i]
[1].distance)
{
good_matches.push_back(matches[i][0]);
}
}
vector<Point2f> points1, points2;
for (int i = 0; i < good_matches.size(); i++)
{
//-- Get the keypoints from the good matches
points1.push_back(keypoints_1[good_matches[i].queryIdx].pt);
points2.push_back(keypoints_2[good_matches[i].trainIdx].pt);
}
/* Find Homography */
Mat H = findHomography(Mat(points2), Mat(points1), RANSAC);