Wednesday, May 1, 2013

OpenCV: SURF Feature extractor

Main steps:
  1. Read Two Images
  2. Resize to half of its size
  3. Detectect Keypoints  using SURF
  4. Calculate Feature Descriptor
  5. Matching Descriptor using Brute Force Matcher
Original Image
 SURF Keypoints
SURF Matches
#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;
}
view raw gistfile1.cpp hosted with ❤ by GitHub


No comments:

Post a Comment