level 4
饮梦者014
楼主
关于图像匹配的问题,找到特征点匹配点后后,怎么对齐?
程序如下:
#include<iostream>
#include <opencv2/opencv.hpp>
#include<string.h>
#include <stdio.h>
#include <opencv2/nonfree/features2d.hpp>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
/*Mat img1=imread("Image01.jpg");*/
Mat img1=imread("Image01_副本.jpg");
/*Mat img2=imread("Image02.jpg");*/
Mat img2=imread("Image02_副本.jpg");
//cvtColor(img1,img1,CV_RGB2GRAY);
//cvtColor(img2,img2,CV_RGB2GRAY);
//-- Step 1: Detect the keypoints using SURF Detector
int minHessian=400;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_1, keypoints_2;//
detector.detect( img1, keypoints_1 );
detector.detect( img2, keypoints_2 );
//-- Step 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors_1, descriptors_2;//两个矩阵
extractor.compute( img1, keypoints_1, descriptors_1 );
extractor.compute( img2, keypoints_2, descriptors_2 );
//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
// BruteForceMatcher<L2<float>> matcher;
std::vector< DMatch > matches;//
matcher.match( descriptors_1, descriptors_2, matches );/
Mat imgMatches;
drawMatches(img1, keypoints_1, img2, keypoints_2, matches, imgMatches);
namedWindow("Matches");
imshow("Matches", imgMatches);///
double max_dist = 0;
double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_1.rows; i++ )//rows是行
{
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 2*min_dist )
//-- PS.- radiusMatch can also be used here.
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors_1.rows; i++ )
{
if( matches[i].distance < 2*min_dist )
{
good_matches.push_back( matches[i]);
}
}
//-- Draw only "good" matches仅仅绘制
Mat img_matches;
drawMatches( img1, keypoints_1, img2, keypoints_2,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
imshow( "Good Matches & Object detection", img_matches );
waitKey();
return 0;
}
求助!!!是个图像处理的新手,可以看懂程序,但是就是想不到自己要怎么写下去才行。
2016年04月27日 08点04分
1
程序如下:
#include<iostream>
#include <opencv2/opencv.hpp>
#include<string.h>
#include <stdio.h>
#include <opencv2/nonfree/features2d.hpp>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
/*Mat img1=imread("Image01.jpg");*/
Mat img1=imread("Image01_副本.jpg");
/*Mat img2=imread("Image02.jpg");*/
Mat img2=imread("Image02_副本.jpg");
//cvtColor(img1,img1,CV_RGB2GRAY);
//cvtColor(img2,img2,CV_RGB2GRAY);
//-- Step 1: Detect the keypoints using SURF Detector
int minHessian=400;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_1, keypoints_2;//
detector.detect( img1, keypoints_1 );
detector.detect( img2, keypoints_2 );
//-- Step 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors_1, descriptors_2;//两个矩阵
extractor.compute( img1, keypoints_1, descriptors_1 );
extractor.compute( img2, keypoints_2, descriptors_2 );
//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
// BruteForceMatcher<L2<float>> matcher;
std::vector< DMatch > matches;//
matcher.match( descriptors_1, descriptors_2, matches );/
Mat imgMatches;
drawMatches(img1, keypoints_1, img2, keypoints_2, matches, imgMatches);
namedWindow("Matches");
imshow("Matches", imgMatches);///
double max_dist = 0;
double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_1.rows; i++ )//rows是行
{
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 2*min_dist )
//-- PS.- radiusMatch can also be used here.
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors_1.rows; i++ )
{
if( matches[i].distance < 2*min_dist )
{
good_matches.push_back( matches[i]);
}
}
//-- Draw only "good" matches仅仅绘制
Mat img_matches;
drawMatches( img1, keypoints_1, img2, keypoints_2,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
imshow( "Good Matches & Object detection", img_matches );
waitKey();
return 0;
}
求助!!!是个图像处理的新手,可以看懂程序,但是就是想不到自己要怎么写下去才行。