12/13/2013

How to use mask parameter for SURF feature detector (OpenCV)

The function of mask parameter is we can extract features only desired position(Rect).

This is mask.
 
This is output.
 
We can see features is extracted only white rect.
This is example source code.
 
 
 
 

/////
#include < stdio.h >  
#include < opencv2\opencv.hpp >
#include < opencv2\features2d\features2d.hpp >
#include < opencv2\nonfree\features2d.hpp >

#ifdef _DEBUG  
#pragma comment(lib, "opencv_core246d.lib")   
//#pragma comment(lib, "opencv_imgproc246d.lib")   //MAT processing  
//#pragma comment(lib, "opencv_objdetect246d.lib")   
//#pragma comment(lib, "opencv_gpu246d.lib")  
#pragma comment(lib, "opencv_features2d246d.lib")  
#pragma comment(lib, "opencv_highgui246d.lib")  
//#pragma comment(lib, "opencv_ml246d.lib")
//#pragma comment(lib, "opencv_stitching246d.lib");
#pragma comment(lib, "opencv_nonfree246d.lib");

#else  
#pragma comment(lib, "opencv_core246.lib")  
//#pragma comment(lib, "opencv_imgproc246.lib")  
//#pragma comment(lib, "opencv_objdetect246.lib")  
//#pragma comment(lib, "opencv_gpu246.lib")  
#pragma comment(lib, "opencv_features2d246.lib")  
#pragma comment(lib, "opencv_highgui246.lib")  
//#pragma comment(lib, "opencv_ml246.lib")  
//#pragma comment(lib, "opencv_stitching246.lib");
#pragma comment(lib, "opencv_nonfree246.lib");
#endif  

using namespace cv;  
using namespace std;


void main()  
{
 
 //SURF_GPU example source code
 Mat inImg,outImg;
 vector< cv::KeyPoint > src_keypoints;
 vector< float > src_descriptors;
 
 //image load
 inImg = imread("ship.png",0);

 //FeatureFinder 
 SurfFeatureDetector FeatureFinder(400);

 //make mask
 Mat mask = Mat::zeros(inImg.size(), CV_8U);  
 Mat roi(mask, Rect(400,400,400,400) );
 roi = Scalar(255, 255, 255);  

 /*
 //The mode to set multiple masks
 vector< Rect > mask_rect;
 mask_rect.push_back(Rect(0,0,200,200) );
 mask_rect.push_back(Rect(200,200,200,200) );
 mask_rect.push_back(Rect(400,400,200,200) );
 mask_rect.push_back(Rect(600,600,200,200) );

 Mat mask = Mat::zeros(inImg.size(), CV_8U);  
 for(int i=0; i< mask_rect.size(); ++i)
 {
  Mat roi(mask, mask_rect[i] ); 
  roi = Scalar(255, 255, 255);  
 }
 */

 //Feature Extraction
 FeatureFinder.detect( inImg, src_keypoints , mask);

 //Features Draw
 drawKeypoints( inImg, src_keypoints, outImg, Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );

 imshow("Show", outImg); 
 imshow("mask", mask); 
 imshow("roi", roi); 
 waitKey(0);

 //save to file
 imwrite("output.jpg", outImg);
 imwrite("mask.jpg", mask);
 imwrite("roi.jpg", roi);

}

/////


If you want to set one more masks, see the comment section.
This source code is advance on this page(http://feelmare.blogspot.kr/2013/12/surffeaturedetector-exmaple-source-code.html).

No comments:

Post a Comment