我正在尝试运行冲浪代码:
我有以下问题。
1:我希望获得有限的关键点(例如1000),如SIFT实现。是否有任何内置函数或者必须编写自己的函数。
2:我画的很好。它工作正常,但我想绘制 与绿色匹配良好,与红线匹配(匹配 - 匹配) 在同一张图片上('Match_SURF.jpg')。
#include <stdio.h>
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/line_descriptor.hpp"
#include "opencv2\features2d\features2d.hpp"
#include "opencv2/xfeatures2d.hpp"
#include "opencv2\xfeatures2d\nonfree.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
void readme();
/** @function main */
int main(int argc, char** argv)
{
Mat img_object = imread("C:\\VC_examples\\IMG_0030.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat img_scene = imread("C:\\VC_examples\\IMG_0031.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (!img_object.data || !img_scene.data)
{
std::cout << " --(!) Error reading images " << std::endl; return -1;
}
//-- Step 1: Detect the keypoints using SURF Detector
int minHessian = 500;
cv::Ptr<Feature2D> detector = xfeatures2d::SURF::create(minHessian);
std::vector<KeyPoint> keypoints_object, keypoints_object;
std::vector<KeyPoint> keypoints_scene, keypoints_scene;
detector->detect(img_object, keypoints_object);
detector->detect(img_scene, keypoints_scene);
//-- Step 2: Calculate descriptors (feature vectors)
Mat descriptors_object, descriptors_scene;
detector->compute(img_scene, keypoints_scene, descriptors_scene);
detector->compute(img_object, keypoints_object, descriptors_object);
//-- Step 3: Matching descriptor vectors using BFMatcher :
BFMatcher matcher;
std::vector< DMatch > matches;
matcher.match(descriptors_object, descriptors_scene, matches);
double max_dist = 0; double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for (int i = 0; i < descriptors_object.rows; i++)
{
double dist = matches[i].distance;
if (dist < min_dist) min_dist = dist;
if (dist > max_dist) max_dist = dist;
}
printf("-- Max dist : %f \n", max_dist);
printf("-- Min dist : %f \n", min_dist);
绘制“好”匹配和不匹配
//-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
std::vector< DMatch > good_matches;
for (int i = 0; i < descriptors_object.rows; i++)
{
if (matches[i].distance < 3 * min_dist)
{
good_matches.push_back(matches[i]);
}
}
Mat img_matches;
drawMatches(img_object, keypoints_object, img_scene, keypoints_scene,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
std::vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
//-- Show detected matches
imshow("Good Matches & Object detection", img_matches);
保存图片
imwrite("Match_SURF.jpg", img_matches);**
waitKey(0);
return 0;
}
/** @function readme */
void readme()
{
std::cout << " Usage: ./SURF_descriptor <img1> <img2>" << std::endl;
}
有什么想法吗?