- Read Two Images
- Resize to half of its size
- Detectect Keypoints using SURF
- Calculate Feature Descriptor
- Matching Descriptor using Brute Force Matcher
Original Image
SURF Keypoints
SURF Matches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <vector> | |
#include "opencv2\opencv.hpp" | |
#include <opencv2\nonfree\features2d.hpp> | |
using namespace std; | |
using namespace cv; | |
int main() | |
{ | |
Mat img_1, img_2; | |
img_1= imread("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE ); | |
img_2= imread("lena.jpg", CV_LOAD_IMAGE_GRAYSCALE ); | |
if(img_1.empty()||img_2.empty()) | |
{ | |
cout << "Could not open or find the image" << std::endl ; | |
return -1; | |
} | |
/// Resize | |
resize(img_1,img_1,Size(0,0),0.5,0.5,INTER_LINEAR); | |
resize(img_2,img_2,Size(0,0),0.5,0.5,INTER_LINEAR); | |
imshow("Image 1", img_1); | |
imshow("Image 2", img_2); | |
// Step -1, Detect keypoints using SURF detector | |
int minHessian = 400; | |
SurfFeatureDetector detector(minHessian); | |
vector<KeyPoint> keypoints_1, keypoints_2; | |
detector.detect(img_1, keypoints_1); | |
detector.detect(img_2, keypoints_2); | |
// Step -2, Calculate descriptors (feature vector) | |
SurfDescriptorExtractor extractor; | |
Mat descriptor_1, descriptor_2; | |
extractor.compute(img_1,keypoints_1,descriptor_1); | |
extractor.compute(img_2,keypoints_2,descriptor_2); | |
//step - 3, Matching descriptor vectors with a brute force mathcher | |
BFMatcher matcher(NORM_L2); | |
vector<DMatch> matches; | |
matcher.match(descriptor_1, descriptor_2,matches); | |
//--Draw Matches | |
Mat img_matches; | |
drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches); | |
//-- Show Detected Matches | |
imshow("Matches",img_matches); | |
waitKey(0); | |
return 0; | |
} |
No comments:
Post a Comment