Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ OpenCV
i have code :
 
 
#include "stdafx.h"
using namespace cv;
using namespace std;
 
int main( int argc, char** argv )
{
	/*for (int i = 0; i <= 1; i++ )
	{
		char* tom[] = {"photo1.jpg","photo2.jpg"};*/
Mat img_A = imread( "photo1.jpg", CV_LOAD_IMAGE_COLOR );
Mat img_B = imread( "photo.png", CV_LOAD_IMAGE_COLOR );
if( !img_A.data || !img_B.data )
{ std::cout<< " --(!) Error reading images " << std::endl; return -1; }
//-- Step 1: Detect the keypoints using SURF Detector
int minHessian = 400;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_A, keypoints_B;
detector.detect( img_A, keypoints_A );
detector.detect( img_B, keypoints_B );
//-- Step 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors_A, descriptors_B;
extractor.compute( img_A, keypoints_A, descriptors_A );
extractor.compute( img_B, keypoints_B, descriptors_B );
//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector< DMatch > matches;                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
matcher.match( descriptors_A, descriptors_B, matches );
double max_dist = 0; double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_A.rows; i++ )
{ double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
if ( min_dist <= 0.05)
{
	cout << " ti`m chinh xac " << endl;
}
else
{
	cout << " ti`m chua chinh xac " << endl;
}
printf("-- Max dist : %f \n", max_dist );
printf("-- Min dist : %f \n", min_dist );
	
//-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors_A.rows; i++ )
{ if( matches[i].distance < 3*min_dist )
{ good_matches.push_back( matches[i]); }
}
Mat img_matches;
drawMatches(img_A, keypoints_A, img_B, keypoints_B,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
//-- Localize the object
std::vector<Point2f> A;
std::vector<Point2f> B;
for( int i = 0; i < good_matches.size(); i++ )
{
//-- Get the keypoints from the good matches
A.push_back( keypoints_A[ good_matches[i].queryIdx ].pt );
B.push_back( keypoints_B[ good_matches[i].trainIdx ].pt );
}
Mat H = findHomography( A, B, CV_RANSAC );
//-- Get the corners from the image_1 ( the object to be "detected" )
std::vector<Point2f> A_corners(4);
A_corners[0] = cvPoint(0,0); 
A_corners[1] = cvPoint( img_A.cols, 0 );
A_corners[2] = cvPoint( img_A.cols, img_A.rows );
A_corners[3] = cvPoint( 0, img_A.rows );
std::vector<Point2f> B_corners(4);
perspectiveTransform( A_corners, B_corners, H);
//-- Draw lines between the corners (the mapped Image A in Image B )
line( img_matches, B_corners[0] + Point2f( img_A.cols, 0), B_corners[1] + Point2f( img_A.cols, 0),Scalar(0,0,255)); 
line( img_matches, B_corners[1] + Point2f( img_A.cols, 0), B_corners[2] + Point2f( img_A.cols, 0),Scalar(0,0,255)); 
line( img_matches, B_corners[2] + Point2f( img_A.cols, 0), B_corners[3] + Point2f( img_A.cols, 0),Scalar(0,0,255));
line( img_matches, B_corners[3] + Point2f( img_A.cols, 0), B_corners[0] + Point2f( img_A.cols, 0),Scalar(0,0,255));
 //-- Show detected matches
imshow( "Good Matches & Object detection", img_matches );
waitKey(0);
return 0;
}
 

 
But when i try with a smaller image ( 10 x 11 pixel or 14 x 15 pixel ) , the code didn't run!
 
How to apply that code with image 10x11 pixel ?
Posted 20-Apr-12 7:35am
Comments
BupeChombaDerrick at 20-Apr-12 13:28pm
   
I think the 10x11 is too small for an algorithm like SURF, for such small images. you can just use normalized template matching.
Lee Shine at 20-Apr-12 13:33pm
   
oh no , :( ... have another way to resolve it ? don't use SURF ??????
BupeChombaDerrick at 20-Apr-12 13:52pm
   
Maybe try to enlarge the image to a reasonable size then use the same SURF code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 20 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100