For using SURF detector we must add this headers additionaly;
#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>
int main( int argc, char** argv )
{
Mat frame;
Mat templ;// marker
Mat descriptors1;
Mat descriptors2;
Mat result;
Mat img_matches;
std::vector< DMatch > good_matches;
VideoCapture cam1("/home/maygun/Desktop/source.mp4");//our video
templ = imread("/home/maygun/Desktop/matrix.png"); // our object that we want to find in an image
cvtColor( templ,templ,cv::COLOR_BGR2GRAY );// transform color to gray image
cv::GaussianBlur(templ,templ,Size(5,5),0.5,0.5,0);
cv::FastFeatureDetector detector(21); // parameter as a min hessian
vector<KeyPoint> keypoints_frame; // keypoints for matching
vector<KeyPoint> keypoints_matrix;
cv::SurfDescriptorExtractor extractor;
std::vector<Point2f> obj;
std::vector<Point2f> scene;
BFMatcher matcher(NORM_L2);
vector< DMatch > matches;
detector.detect(templ, keypoints_matrix); // detect keypoint in object
extractor.compute(templ,keypoints_matrix,descriptors1); // create descriptors
namedWindow( "result", WINDOW_NORMAL );
while(1)
{
char b1 = cvWaitKey(33);
if (b1 == 27) // if esc exit
break;
cam1 >> frame;
cv::GaussianBlur(frame,frame,Size(5,5),0.5,0.5,0);
detector.detect(frame,keypoints_frame); // detector for each frame
extractor.compute(frame,keypoints_frame,descriptors2);
matcher.match(descriptors1,descriptors2,matches); // match images
cv::drawMatches(templ,keypoints_matrix,frame,keypoints_frame,matches,result,Scalar::all(-1),Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow("result",result);
}
waitKey(0);
return 0;
}
Here result ;
References ; http://docs.opencv.org/doc/tutorials/features2d/feature_detection/feature_detection.html