1 #include "opencv2/highgui/highgui.hpp"
2 #include "opencv2/core/core.hpp"
3 #include "opencv2/imgproc/imgproc.hpp"
4 #include "opencv2/features2d/features2d.hpp"
5 #include "opencv2/objdetect/objdetect.hpp"
6 #include "opencv2/legacy/legacy.hpp"
16 printf( "This program shows the use of the \"fern\" plannar PlanarObjectDetector point\n"
17 "descriptor classifier\n"
19 "./find_obj_ferns <object_filename> <scene_filename>, default: box.png and box_in_scene.png\n\n");
24 int main(int argc, char** argv)
28 const char* object_filename = argc > 1 ? argv[1] : "box.png";
29 const char* scene_filename = argc > 2 ? argv[2] : "box_in_scene.png";
33 Mat object = imread( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
34 Mat scene = imread( scene_filename, CV_LOAD_IMAGE_GRAYSCALE );
36 if( !object.data || !scene.data )
38 fprintf( stderr, "Can not load %s and/or %s\n",
39 object_filename, scene_filename );
46 resize(scene, image, Size(), 1./imgscale, 1./imgscale, INTER_CUBIC);
48 cvNamedWindow("Object", 1);
49 cvNamedWindow("Image", 1);
50 cvNamedWindow("Object Correspondence", 1);
52 Size patchSize(32, 32);
53 LDetector ldetector(7, 20, 2, 2000, patchSize.width, 2);
54 ldetector.setVerbose(true);
55 PlanarObjectDetector detector;
57 vector<Mat> objpyr, imgpyr;
60 GaussianBlur(object, object, Size(blurKSize, blurKSize), sigma, sigma);
61 GaussianBlur(image, image, Size(blurKSize, blurKSize), sigma, sigma);
62 buildPyramid(object, objpyr, ldetector.nOctaves-1);
63 buildPyramid(image, imgpyr, ldetector.nOctaves-1);
65 vector<KeyPoint> objKeypoints, imgKeypoints;
66 PatchGenerator gen(0,256,5,true,0.8,1.2,-CV_PI/2,CV_PI/2,-CV_PI/2,CV_PI/2);
68 string model_filename = format("%s_model.xml.gz", object_filename);
69 printf("Trying to load %s ...\n", model_filename.c_str());
70 FileStorage fs(model_filename, FileStorage::READ);
73 detector.read(fs.getFirstTopLevelNode());
74 printf("Successfully loaded %s.\n", model_filename.c_str());
78 printf("The file not found and can not be read. Let's train the model.\n");
79 printf("Step 1. Finding the robust keypoints ...\n");
80 ldetector.setVerbose(true);
81 ldetector.getMostStable2D(object, objKeypoints, 100, gen);
82 printf("Done.\nStep 2. Training ferns-based planar object detector ...\n");
83 detector.setVerbose(true);
85 detector.train(objpyr, objKeypoints, patchSize.width, 100, 11, 10000, ldetector, gen);
86 printf("Done.\nStep 3. Saving the model to %s ...\n", model_filename.c_str());
87 if( fs.open(model_filename, FileStorage::WRITE) )
88 detector.write(fs, "ferns_model");
90 printf("Now find the keypoints in the image, try recognize them and compute the homography matrix\n");
93 vector<Point2f> dst_corners;
94 Mat correspond( object.rows + image.rows, std::max(object.cols, image.cols), CV_8UC3);
95 correspond = Scalar(0.);
96 Mat part(correspond, Rect(0, 0, object.cols, object.rows));
97 cvtColor(object, part, CV_GRAY2BGR);
98 part = Mat(correspond, Rect(0, object.rows, image.cols, image.rows));
99 cvtColor(image, part, CV_GRAY2BGR);
104 double t = (double)getTickCount();
105 objKeypoints = detector.getModelPoints();
106 ldetector(imgpyr, imgKeypoints, 300);
108 std::cout << "Object keypoints: " << objKeypoints.size() << "\n";
109 std::cout << "Image keypoints: " << imgKeypoints.size() << "\n";
110 bool found = detector(imgpyr, imgKeypoints, H, dst_corners, &pairs);
111 t = (double)getTickCount() - t;
112 printf("%gms\n", t*1000/getTickFrequency());
116 for( i = 0; i < 4; i++ )
118 Point r1 = dst_corners[i%4];
119 Point r2 = dst_corners[(i+1)%4];
120 line( correspond, Point(r1.x, r1.y+object.rows),
121 Point(r2.x, r2.y+object.rows), Scalar(0,0,255) );
125 for( i = 0; i < (int)pairs.size(); i += 2 )
127 line( correspond, objKeypoints[pairs[i]].pt,
128 imgKeypoints[pairs[i+1]].pt + Point2f(0,(float)object.rows),
132 imshow( "Object Correspondence", correspond );
134 cvtColor(object, objectColor, CV_GRAY2BGR);
135 for( i = 0; i < (int)objKeypoints.size(); i++ )
137 circle( objectColor, objKeypoints[i].pt, 2, Scalar(0,0,255), -1 );
138 circle( objectColor, objKeypoints[i].pt, (1 << objKeypoints[i].octave)*15, Scalar(0,255,0), 1 );
141 cvtColor(image, imageColor, CV_GRAY2BGR);
142 for( i = 0; i < (int)imgKeypoints.size(); i++ )
144 circle( imageColor, imgKeypoints[i].pt, 2, Scalar(0,0,255), -1 );
145 circle( imageColor, imgKeypoints[i].pt, (1 << imgKeypoints[i].octave)*15, Scalar(0,255,0), 1 );
148 imwrite("correspond.png", correspond );
149 imshow( "Object", objectColor );
150 imshow( "Image", imageColor );