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