一、代码示例
#include
#include
using namespace cv;
using namespace std;
RNG rng(12345);
void drawKeypoints_test(const vector& keypoints, Mat& outImage)
{
for (size_t i = 0; i < keypoints.size(); i++)
{
Point2f point = keypoints[i].pt;
//printf("x = %f, y = %f\n", point.x, point.y);
circle(outImage, Point(point.x, point.y), 2, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), FILLED, LINE_AA);
}
}
void showPoints()
{
Mat sceneImage = imread("curry_dlt.jpg");
Ptr detector = BRISK::create();
vector keypoints;
detector->detect(sceneImage, keypoints, Mat());
Mat keypointImage = sceneImage.clone();
//drawKeypoints_test(sceneImage, keypoints, keypointImage, Scalar::all(-1));
drawKeypoints_test(keypoints, keypointImage);
imshow("keypointImage", keypointImage);
}
void showMatches()
{
Mat sceneImage = imread("curry_dlt.jpg");
Mat objImage = imread("curry1.jpg");
Ptr detector = BRISK::create();
vector keypoints_obj;
vector keypoints_scene;
Mat descriptor_obj, descriptor_scene;
detector->detectAndCompute(objImage, Mat(), keypoints_obj, descriptor_obj);
detector->detectAndCompute(sceneImage, Mat(), keypoints_scene, descriptor_scene);
// matching
BFMatcher matcher(NORM_L2);
vector matches;
matcher.match(descriptor_obj, descriptor_scene, matches);
// draw matching
Mat outImage;
drawMatches(objImage, keypoints_obj, sceneImage, keypoints_scene, matches, outImage);
imshow("outImage", outImage);
// good matching
vector goodMatches;
double minDist = 0, maxDist = 0;
for (size_t i = 0; i < matches.size(); i++)
{
double dist = matches[i].distance;
if (dist < minDist)
minDist = dist;
if (dist > maxDist)
maxDist = dist;
}
printf("maxDist = %f, minDist = %f\n", maxDist, minDist);
for (size_t i = 0; i < matches.size(); i++)
{
double dist = matches[i].distance;
if (dist < max(0.02, 3 * minDist))
printf("i = %d\n", i);
goodMatches.push_back( matches[i]);
}
Mat goodMatchesImage;
drawMatches(objImage, keypoints_obj, sceneImage, keypoints_scene, goodMatches, goodMatchesImage);
imshow("goodMatchesImage", goodMatchesImage);
}
int main()
{
showMatches();
waitKey(0);
return 0;
}
二、结果展示

