opencv 3.0 rc1, example source code for surf and matching (gpu version)

This code is SURF and Matching test in opencv 3.0 rc1.

Especially, for using surf class, we have to add extra library when build opencv 3.0 rc1.
The extra lib is opened in github named opencv_contrib.

refer to this page.
http://study.marearts.com/2015/01/mil-boosting-tracker-test-in-opencv-30.html

surf(gpu version) is included in xfeatures2d/cuda.hpp
and cpu version is xfeatures2d/nonfree.hpp

As we know, surf, sift are still nonfree. very not good!..-.-

necessary dlls

result


Now, refer this example code.
This cod is converted from http://study.marearts.com/2014/07/opencv-study-surf-gpu-and-matching.html.


#include < stdio.h>  
#include < iostream>  


#include < opencv2\opencv.hpp>  
#include < opencv2\core.hpp>
#include < opencv2\highgui.hpp> 
#include < opencv2\cudaarithm.hpp>
#include < opencv2\xfeatures2d\cuda.hpp>
#include < opencv2\cudafeatures2d.hpp>
//#include < opencv2\xfeatures2d\nonfree.hpp>



#ifdef _DEBUG          
#pragma comment(lib, "opencv_core300d.lib")
#pragma comment(lib, "opencv_highgui300d.lib")  
#pragma comment(lib, "opencv_imgcodecs300d.lib")
#pragma comment(lib, "opencv_cudafeatures2d300d.lib")
#pragma comment(lib, "opencv_xfeatures2d300d.lib")
#pragma comment(lib, "opencv_features2d300d.lib")
#else  
#pragma comment(lib, "opencv_core300.lib")
#pragma comment(lib, "opencv_highgui300.lib")
#pragma comment(lib, "opencv_imgcodecs300.lib")
#pragma comment(lib, "opencv_cudafeatures2d300.lib")
#pragma comment(lib, "opencv_xfeatures2d300.lib")
#pragma comment(lib, "opencv_features2d300.lib")
#endif   


using namespace cv;
using namespace std;

void main()
{


 
 cuda::GpuMat img1(imread("M:\\____videoSample____\\Image\\A2.jpg", CV_LOAD_IMAGE_GRAYSCALE));
 cuda::GpuMat img2(imread("M:\\____videoSample____\\Image\\A3.jpg", CV_LOAD_IMAGE_GRAYSCALE));

 
 /////////////////////////////////////////////////////////////////////////////////////////  
 unsigned long t_AAtime = 0, t_BBtime = 0;
 float t_pt;
 float t_fpt;
 t_AAtime = getTickCount();
 /////////////////////////////////////////////////////////////////////////////////////////  

 
 cuda::SURF_CUDA surf(400); 
 // detecting keypoints & computing descriptors   
 cuda::GpuMat keypoints1GPU, keypoints2GPU;
 cuda::GpuMat descriptors1GPU, descriptors2GPU;
 surf(img1, cuda::GpuMat(), keypoints1GPU, descriptors1GPU);
 surf(img2, cuda::GpuMat(), keypoints2GPU, descriptors2GPU);

 cout << "FOUND " << keypoints1GPU.cols << " keypoints on first image" << endl;
 cout << "FOUND " << keypoints2GPU.cols << " keypoints on second image" << endl;

 // matching descriptors   
 vector< vector< DMatch> > matches;
 Ptr< cuda::DescriptorMatcher > matcher = cuda::DescriptorMatcher::createBFMatcher();
 matcher->knnMatch(descriptors1GPU, descriptors2GPU, matches, 2);

 // downloading results  Gpu -> Cpu  
 vector< KeyPoint> keypoints1, keypoints2;
 vector< float> descriptors1, descriptors2;
 surf.downloadKeypoints(keypoints1GPU, keypoints1);
 surf.downloadKeypoints(keypoints2GPU, keypoints2);
 //surf.downloadDescriptors(descriptors1GPU, descriptors1);   
 //surf.downloadDescriptors(descriptors2GPU, descriptors2);  

 vector< KeyPoint> matchingKey1, matchingKey2;
 std::vector< DMatch > good_matches;
 for (int k = 0; k < min(descriptors1GPU.rows - 1, (int)matches.size()); k++)
 {
  if ((matches[k][0].distance < 0.6*(matches[k][1].distance)) && ((int)matches[k].size() <= 2 && (int)matches[k].size()>0))
  {
   good_matches.push_back(matches[k][0]);

  }
 }

 t_BBtime = getTickCount();
 t_pt = (t_BBtime - t_AAtime) / getTickFrequency();
 t_fpt = 1 / t_pt;
 printf("feature extraction = %.4lf / %.4lf \n", t_pt, t_fpt);

 // drawing the results   
 Mat img_matches;
 Mat img11, img22;
 img1.download(img11);
 img2.download(img22);

 //drawMatches(img11, matchingKey1, img22, matchingKey2, good_matches, img_matches);   
 drawMatches(img11, keypoints1, img22, keypoints2, good_matches, img_matches);

 namedWindow("matches", 0);
 imshow("matches", img_matches);
 waitKey(0);
 
 matcher.release();

}


Comments

Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Optical Flow sample source code using OpenCV

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

(C, C++) TinyXML , xml read & write

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )