reverted samples with new command argument parser. will be continued after OpenCV...
[profile/ivi/opencv.git] / samples / c / find_obj_ferns.cpp
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
7 #include <algorithm>
8 #include <iostream>
9 #include <vector>
10
11 using namespace cv;
12 void help()
13 {
14     printf( "This program shows the use of the \"fern\" plannar PlanarObjectDetector point\n"
15                 "descriptor classifier"
16                 "Usage:\n"
17                 "./find_obj_ferns [<object_filename default: box.png> <scene_filename default:box_in_scene.png>]\n"
18                 "\n");
19 }
20 int main(int argc, char** argv)
21 {
22     const char* object_filename = argc > 1 ? argv[1] : "box.png";
23     const char* scene_filename = argc > 2 ? argv[2] : "box_in_scene.png";
24     int i;
25     help();
26     cvNamedWindow("Object", 1);
27     cvNamedWindow("Image", 1);
28     cvNamedWindow("Object Correspondence", 1);
29     
30     Mat object = imread( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
31     Mat image;
32     
33     double imgscale = 1;
34
35     Mat _image = imread( scene_filename, CV_LOAD_IMAGE_GRAYSCALE );
36     resize(_image, image, Size(), 1./imgscale, 1./imgscale, INTER_CUBIC);
37
38
39     if( !object.data || !image.data )
40     {
41         fprintf( stderr, "Can not load %s and/or %s\n"
42                 "Usage: find_obj_ferns [<object_filename> <scene_filename>]\n",
43                 object_filename, scene_filename );
44         exit(-1);
45     }
46
47     Size patchSize(32, 32);
48     LDetector ldetector(7, 20, 2, 2000, patchSize.width, 2);
49     ldetector.setVerbose(true);
50     PlanarObjectDetector detector;
51     
52     vector<Mat> objpyr, imgpyr;
53     int blurKSize = 3;
54     double sigma = 0;
55     GaussianBlur(object, object, Size(blurKSize, blurKSize), sigma, sigma);
56     GaussianBlur(image, image, Size(blurKSize, blurKSize), sigma, sigma);
57     buildPyramid(object, objpyr, ldetector.nOctaves-1);
58     buildPyramid(image, imgpyr, ldetector.nOctaves-1);
59     
60     vector<KeyPoint> objKeypoints, imgKeypoints;
61         PatchGenerator gen(0,256,5,true,0.8,1.2,-CV_PI/2,CV_PI/2,-CV_PI/2,CV_PI/2);
62     
63     string model_filename = format("%s_model.xml.gz", object_filename);
64     printf("Trying to load %s ...\n", model_filename.c_str());
65     FileStorage fs(model_filename, FileStorage::READ);
66     if( fs.isOpened() )
67     {
68         detector.read(fs.getFirstTopLevelNode());
69         printf("Successfully loaded %s.\n", model_filename.c_str());
70     }
71     else
72     {
73         printf("The file not found and can not be read. Let's train the model.\n");
74         printf("Step 1. Finding the robust keypoints ...\n");
75         ldetector.setVerbose(true);
76         ldetector.getMostStable2D(object, objKeypoints, 100, gen);
77         printf("Done.\nStep 2. Training ferns-based planar object detector ...\n");
78         detector.setVerbose(true);
79     
80         detector.train(objpyr, objKeypoints, patchSize.width, 100, 11, 10000, ldetector, gen);
81         printf("Done.\nStep 3. Saving the model to %s ...\n", model_filename.c_str());
82         if( fs.open(model_filename, FileStorage::WRITE) )
83             detector.write(fs, "ferns_model");
84     }
85     printf("Now find the keypoints in the image, try recognize them and compute the homography matrix\n");
86     fs.release();
87         
88     vector<Point2f> dst_corners;
89     Mat correspond( object.rows + image.rows, std::max(object.cols, image.cols), CV_8UC3);
90     correspond = Scalar(0.);
91     Mat part(correspond, Rect(0, 0, object.cols, object.rows));
92     cvtColor(object, part, CV_GRAY2BGR);
93     part = Mat(correspond, Rect(0, object.rows, image.cols, image.rows));
94     cvtColor(image, part, CV_GRAY2BGR);
95  
96     vector<int> pairs;
97     Mat H;
98     
99     double t = (double)getTickCount();
100     objKeypoints = detector.getModelPoints();
101     ldetector(imgpyr, imgKeypoints, 300);
102     
103     std::cout << "Object keypoints: " << objKeypoints.size() << "\n";
104     std::cout << "Image keypoints: " << imgKeypoints.size() << "\n";
105     bool found = detector(imgpyr, imgKeypoints, H, dst_corners, &pairs);
106     t = (double)getTickCount() - t;
107     printf("%gms\n", t*1000/getTickFrequency());
108     
109     if( found )
110     {
111         for( i = 0; i < 4; i++ )
112         {
113             Point r1 = dst_corners[i%4];
114             Point r2 = dst_corners[(i+1)%4];
115             line( correspond, Point(r1.x, r1.y+object.rows),
116                  Point(r2.x, r2.y+object.rows), Scalar(0,0,255) );
117         }
118     }
119     
120     for( i = 0; i < (int)pairs.size(); i += 2 )
121     {
122         line( correspond, objKeypoints[pairs[i]].pt,
123              imgKeypoints[pairs[i+1]].pt + Point2f(0,(float)object.rows),
124              Scalar(0,255,0) );
125     }
126     
127     imshow( "Object Correspondence", correspond );
128     Mat objectColor;
129     cvtColor(object, objectColor, CV_GRAY2BGR);
130     for( i = 0; i < (int)objKeypoints.size(); i++ )
131     {
132         circle( objectColor, objKeypoints[i].pt, 2, Scalar(0,0,255), -1 );
133         circle( objectColor, objKeypoints[i].pt, (1 << objKeypoints[i].octave)*15, Scalar(0,255,0), 1 );
134     }
135     Mat imageColor;
136     cvtColor(image, imageColor, CV_GRAY2BGR);
137     for( i = 0; i < (int)imgKeypoints.size(); i++ )
138     {
139         circle( imageColor, imgKeypoints[i].pt, 2, Scalar(0,0,255), -1 );
140         circle( imageColor, imgKeypoints[i].pt, (1 << imgKeypoints[i].octave)*15, Scalar(0,255,0), 1 );
141     }
142     imwrite("correspond.png", correspond );
143     imshow( "Object", objectColor );
144     imshow( "Image", imageColor );
145     
146     waitKey(0);
147     return 0;
148 }