C:\Windows\System32目录需要添加
需要添加引用三个库:
opencv_xfeatures2d341d.lib
opencv_world341d.lib
opencv_calib3d341d.lib
#include <stdio.h>
#include <iostream>
#include <opencv2/core/core.hpp>
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
void readme();
int main(int argc, char** argv)
{
if (argc != 3)
{
readme();
return -1;
}
Mat img_object = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
Mat img_scene = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
if (img_object.empty() || img_scene.empty())
{
cout << "Image is invalid" << endl;
return -1;
}
int minHessian = 400;
Ptr<SURF> detector = SURF::create(minHessian);
vector<KeyPoint> keypoints_src, keypoints_dst;
Mat descriptor_object, descriptor_scene;
detector->detectAndCompute(img_object, Mat(), keypoints_src, descriptor_object);
detector->detectAndCompute(img_scene, Mat(), keypoints_dst, descriptor_scene);
FlannBasedMatcher matcher;
vector<DMatch> matches;
matcher.match(descriptor_object, descriptor_scene, matches);
// find good matched points
double minDist = 100, maxDist = 0;
for (int i = 0; i < descriptor_object.rows; i++)
{
double dist = matches[i].distance;
if (dist > maxDist)
maxDist = dist;
if (dist < minDist)
minDist = dist;
}
cout << "--Max dist : " << maxDist << endl;
cout << "--Min dist : " << minDist << endl;
vector<DMatch> goodMatches;
for (int i = 0; i < descriptor_object.rows; i++)
{
double dist = matches[i].distance;
if (dist < max(3 * minDist, 0.02))
{
goodMatches.push_back(matches[i]);
}
}
Mat img_matches;
drawMatches(img_object, keypoints_src, img_scene, keypoints_dst, goodMatches, img_matches, Scalar::all(-1),
Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
vector<Point2f> obj;
vector<Point2f> scene;
for (int i = 0; i < goodMatches.size(); i++)
{
obj.push_back(keypoints_src[goodMatches[i].queryIdx].pt);
scene.push_back(keypoints_dst[goodMatches[i].trainIdx].pt);
}
Mat H = findHomography(obj, scene, CV_RANSAC);
vector<Point2f> obj_corners(4);
obj_corners[0] = cvPoint(0, 0);
obj_corners[1] = cvPoint(img_object.cols, 0);
obj_corners[2] = cvPoint(img_object.cols, img_object.rows);
obj_corners[3] = cvPoint(0, img_object.rows);
vector<Point2f> scene_corners(4);
perspectiveTransform(obj_corners, scene_corners, H);
line(img_matches, scene_corners[0] + Point2f(img_object.cols, 0), scene_corners[1] + Point2f(img_object.cols, 0), Scalar(0, 255, 0), 4);
line(img_matches, scene_corners[1] + Point2f(img_object.cols, 0), scene_corners[2] + Point2f(img_object.cols, 0), Scalar(0, 255, 0), 4);
line(img_matches, scene_corners[2] + Point2f(img_object.cols, 0), scene_corners[3] + Point2f(img_object.cols, 0), Scalar(0, 255, 0), 4);
line(img_matches, scene_corners[3] + Point2f(img_object.cols, 0), scene_corners[0] + Point2f(img_object.cols, 0), Scalar(0, 255, 0), 4);
imshow("Good Matches & Object detection", img_matches);
waitKey(0);
return 0;
}
void readme()
{
cout<<"Usage:./SURF_descriptor <img1> <img2>" << std::endl;
}
运行结果: