Ask Your Question

Revision history [back]

I have written it in C++, You can have a try, because they are simular.

initModule_nonfree();//if use SIFT or SURF Ptr<featuredetector> detector = FeatureDetector::create("SIFT"); Ptr<descriptorextractor> descriptor_extractor = DescriptorExtractor::create("SIFT"); Ptr<descriptormatcher> descriptor_matcher = DescriptorMatcher::create("BruteForce"); //if( detector.empty() || descriptor_extractor.empty() ) // MessageBox(_T("aaaaa")); //throw runtime_error("fail to create detector!");

Mat img1 = imread("1.jpg");
Mat img2 = imread("2.jpg");
//imshow("Mathc",img1);
//detect keypoints;
vector<KeyPoint> keypoints1,keypoints2;
detector->detect( img1, keypoints1 );
detector->detect( img2, keypoints2 );
//cout <<"img1:"<< keypoints1.size() << " points  img2:" <<keypoints2.size() 
//  << " points" << endl << ">" << endl;

//compute descriptors for keypoints;
//cout << "< Computing descriptors for keypoints from images..." << endl;
Mat descriptors1,descriptors2;
descriptor_extractor->compute( img1, keypoints1, descriptors1 );
descriptor_extractor->compute( img2, keypoints2, descriptors2 );

//cout<<endl<<"Descriptors Size: "<<descriptors2.size()<<" >"<<endl;
//cout<<endl<<"Descriptor's Column: "<<descriptors2.cols<<endl
//  <<"Descriptor's Row: "<<descriptors2.rows<<endl;
//cout << ">" << endl;

//Draw And Match img1,img2 keypoints
Mat img_keypoints1,img_keypoints2;
drawKeypoints(img1,keypoints1,img_keypoints1,Scalar::all(-1),0);
drawKeypoints(img2,keypoints2,img_keypoints2,Scalar::all(-1),0);
imshow("Box_in_scene keyPoints",img_keypoints1);
imshow("Box keyPoints",img_keypoints2);

descriptor_extractor->compute( img1, keypoints1, descriptors1 );  
vector<DMatch> matches;
descriptor_matcher->match( descriptors1, descriptors2, matches );

Mat img_matches;
drawMatches(img1,keypoints1,img2,keypoints2,matches,img_matches,Scalar::all(-1),CV_RGB(255,255,255),Mat(),4);

imshow("Mathc",img_matches);
waitKey(10000);