6/02/2015

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();

}


6/01/2015

opencv 3.0 rc1, background subtraction mog2 example code

This is mog2 example code in opencv 3.0 rc1 version.

It will be help when you convert opencv 2.x code to 3.x opencv version.
Thank you.

This is mog2 example of 2.4.9 version.
http://study.marearts.com/2015/05/basic-mog2-example-souce-using-opencv.html

...
#include < iostream>  
#include < opencv2\opencv.hpp>  
#include < opencv2\videoio.hpp>  
#include < opencv2\highgui.hpp>  
//#include < opencv2\core\cuda.hpp>
#include < opencv2\core.hpp>
//#include < opencv2\bgsegm.hpp>
#include < opencv2\cudabgsegm.hpp>

#ifdef _DEBUG             
#pragma comment(lib, "opencv_core300d.lib")     
#pragma comment(lib, "opencv_highgui300d.lib")  
#pragma comment(lib, "opencv_videoio300d.lib")  
#pragma comment(lib, "opencv_cudabgsegm300d.lib")  

#else     
#pragma comment(lib, "opencv_core300.lib")     
#pragma comment(lib, "opencv_highgui300.lib")  
#pragma comment(lib, "opencv_videoio300.lib")  
#pragma comment(lib, "opencv_cudabgsegm300.lib")  
#endif      

using namespace std;
using namespace cv;
int main()
{

 //
 
 VideoCapture cap("M:\\____videoSample____\\blog\\video20.wmv");
 //VideoCapture cap("M:\\____videoSample____\\posco\\cartype1.avi");
 
 //
 Ptr< cuda::BackgroundSubtractorMOG2 > MOG2_g = cuda::createBackgroundSubtractorMOG2(3000, 64); 
 Mat Mog_Mask;
 cuda::GpuMat Mog_Mask_g;

 //
 Mat o_frame;
 cuda::GpuMat o_frame_gpu;
 

 cap >> o_frame;
 if (o_frame.empty())
  return 0;

 /////////////////////////////////////////////////////////////////////////    


 unsigned long AAtime = 0, BBtime = 0;

 //Mat rFrame;    
 Mat showMat_r_blur;
 Mat showMat_r;

 namedWindow("origin");
 namedWindow("mog_mask");

 while (1)
 {
  /////////////////////////////////////////////////////////////////////////    
  cap >> o_frame;
  if (o_frame.empty())
   return 0;


  o_frame_gpu.upload(o_frame);
  AAtime = getTickCount();

  //
  MOG2_g->apply(o_frame_gpu, Mog_Mask_g, -1);
  //pMOG2_g.operator()(o_frame_gpu, Mog_Mask_g, -1);
  //  
  Mog_Mask_g.download(Mog_Mask);

  BBtime = getTickCount();
  float pt = (BBtime - AAtime) / getTickFrequency();
  float fpt = 1 / pt;
  printf("gpu %.4lf / %.4lf \n", pt, fpt);



  o_frame_gpu.download(showMat_r);
  imshow("origin", showMat_r);
  imshow("mog_mask", Mog_Mask);


  /////////////////////////////////////////////////////////////////////////    

  if (waitKey(10) > 0)
   break;
 }

 MOG2_g.release();

 return 0;
}



...