1/13/2014

(OpenCV GPU) Finding largest subset images that is only adjacent(subsequnce) images, (OpenCV, SurfFeaturesFinder, BestOf2NearestMatcher, leaveBiggestComponent funcions example souce code)


This is GPU version of this page ->http://feelmare.blogspot.kr/2013/12/finding-largest-subset-images-that-is.html
Please refer detail description on that page.

The gpu mode is showed  about 10 times faster than gpu processing.



I think GPU programing is very ensential in computer vision, if you don't have contraint on the performance of equipment.

...
#include < stdio.h >     
#include < opencv2\opencv.hpp >     
#include < opencv2\features2d\features2d.hpp >   
#include < opencv2\nonfree\features2d.hpp >   
#include < opencv2\stitching\detail\matchers.hpp >   
#include < opencv2\stitching\stitcher.hpp >   


#ifdef _DEBUG     
#pragma comment(lib, "opencv_core247d.lib")      
//#pragma comment(lib, "opencv_imgproc247d.lib")   //MAT processing     
//#pragma comment(lib, "opencv_objdetect247d.lib")      
#pragma comment(lib, "opencv_gpu247d.lib")     
#pragma comment(lib, "opencv_features2d247d.lib")     
#pragma comment(lib, "opencv_highgui247d.lib")     
//#pragma comment(lib, "opencv_ml247d.lib")   
#pragma comment(lib, "opencv_stitching247d.lib");   
#pragma comment(lib, "opencv_nonfree247d.lib");   

#else     
#pragma comment(lib, "opencv_core247.lib")     
//#pragma comment(lib, "opencv_imgproc247.lib")     
//#pragma comment(lib, "opencv_objdetect247.lib")     
#pragma comment(lib, "opencv_gpu247.lib")     
#pragma comment(lib, "opencv_features2d247.lib")     
#pragma comment(lib, "opencv_highgui247.lib")     
//#pragma comment(lib, "opencv_ml247.lib")     
#pragma comment(lib, "opencv_stitching247.lib");   
#pragma comment(lib, "opencv_nonfree247.lib");   
#endif     

using namespace cv;     
using namespace std;   


void main()     
{   

 //processign tiem measurement   
 unsigned long AAtime=0, BBtime=0;   
 //processing time measure   
 AAtime = getTickCount();

 vector< Mat > vImg;   
 Mat rImg;

 vImg.push_back( imread("./stitching_img/m7.jpg",0) );   
 vImg.push_back( imread("./stitching_img/S1.jpg",0) );   
 vImg.push_back( imread("./stitching_img/m9.jpg",0) );   
 vImg.push_back( imread("./stitching_img/m6.jpg",0) );   
 vImg.push_back( imread("./stitching_img/S6.jpg",0) );   
 vImg.push_back( imread("./stitching_img/m8.jpg",0) );   


 //feature extract   
 gpu::SURF_GPU FeatureFinder_gpu(400);
 gpu::GpuMat inImg_g;   
 gpu::GpuMat src_keypoints_gpu, src_descriptors_gpu;

 vector< cv::KeyPoint> src_keypoints;   
 vector< float> src_descriptors;

 vector< detail::ImageFeatures> features;   

 for(int i=0; i< vImg.size(); ++i)   
 {     
  detail::ImageFeatures F;
  inImg_g.upload(vImg[i]);
  FeatureFinder_gpu(inImg_g, gpu::GpuMat(), src_keypoints_gpu, src_descriptors_gpu, false);
  
  //descriptor down   
  FeatureFinder_gpu.downloadKeypoints(src_keypoints_gpu, src_keypoints);
  FeatureFinder_gpu.downloadDescriptors(src_descriptors_gpu, src_descriptors);  

  //make ImageFeatures
  F.img_idx=i;
  F.img_size = Size(vImg[i].cols, vImg[i].rows);
  F.keypoints = src_keypoints;
  Mat M = Mat(src_descriptors.size()/64.0, 64, CV_32FC1);
  F.descriptors = M;
  memcpy(M.data, src_descriptors.data(), src_descriptors.size()*sizeof(float));  

  //Add vector
  features.push_back(F);

  //data confirm
  printf("%d - key:%d \n", features[i].img_idx, features[i].keypoints.size() );
  printf("    des:cols:%d, rows:%d \n", features[i].descriptors.cols, features[i].descriptors.rows);
  
 }

 

 
 //match
 vector< int> indices_;   
 double conf_thresh_ = 1.0;   
 Mat matching_mask;   
 vector< detail::MatchesInfo> pairwise_matches;   
 detail::BestOf2NearestMatcher Matcher(true);
 Matcher(features, pairwise_matches, matching_mask);   
 Matcher.collectGarbage();


 //grouping
 indices_ = detail::leaveBiggestComponent(features, pairwise_matches, (float)conf_thresh_);   
 Matcher.collectGarbage();   

 for (size_t i = 0; i < indices_.size(); ++i)   
 {   
  printf("%d \n", indices_[i] );   
 }   

 //Processing time measurement   
 BBtime = getTickCount();
 printf("Processing time = %.2lf(sec) \n",  (BBtime - AAtime)/getTickFrequency() );   

 
}