<em id= "__mceDel" >#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <iostream>
using
namespace cv;
using
namespace std;
int main(){
cv::initModule_nonfree();
Mat img1_mat = imread( "1.bmp" ,CV_LOAD_IMAGE_GRAYSCALE);
Mat img2_mat = imread( "2.bmp" ,CV_LOAD_IMAGE_GRAYSCALE);
std::vector<KeyPoint> img1_keypoint,img2_keypoint;<br>
/*特征点检测*/
// FeatureDetector *sift_detector = FeatureDetector::create("SIFT");
Ptr<FeatureDetector> sift_detector = FeatureDetector::create( "SIFT" );
sift_detector->detect(img1_mat,img1_keypoint);
sift_detector->detect(img2_mat,img2_keypoint);<br>
/*在图片上显示特征点*/
Mat img_kp_1,img_kp_2;
drawKeypoints(img1_mat,img1_keypoint,img_kp_1,Scalar::all(-1),DrawMatchesFlags::DEFAULT);
drawKeypoints(img2_mat,img2_keypoint,img_kp_2,Scalar::all(-1),DrawMatchesFlags::DEFAULT);
imshow( "sift_keypoint_image 1" ,img_kp_1);
imshow( "sift_keypoint_image 2" ,img_kp_2);<br>
/*特征向量提取*/
Mat img1_descriptor,img2_descriptor;
Ptr<DescriptorExtractor> sift_descriptor_extractor = DescriptorExtractor::create( "SIFT" );
sift_descriptor_extractor->compute(img1_mat,img1_keypoint,img1_descriptor);
sift_descriptor_extractor->compute(img2_mat,img2_keypoint,img2_descriptor);
/*特征点匹配*/
std::vector<DMatch> img_match;
Ptr<DescriptorMatcher> bruteforce_matcher = DescriptorMatcher::create( "BruteForce" );
bruteforce_matcher->match(img1_descriptor,img2_descriptor,img_match);
/*在图片上显示匹配结果*/
Mat match_img;
drawMatches(img1_mat,img1_keypoint,img2_mat,img2_keypoint,accurate_match,match_img);
imshow( "match image" ,match_img);
waitKey(0);
system ( "pause" );
return
0;
}
</em>
|