]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/samples/c/find_obj_ferns.cpp
2d3c48d5147724956187a910b81296952b1bcd32
[opencv.git] / opencv / samples / c / find_obj_ferns.cpp
1 #include <cv.h>
2 #include <cvaux.h>
3 #include <highgui.h>
4
5 #include <algorithm>
6 #include <iostream>
7 #include <vector>
8
9 using namespace cv;
10
11 int main(int argc, char** argv)
12 {
13     const char* object_filename = argc > 1 ? argv[1] : "box.png";
14     const char* scene_filename = argc > 2 ? argv[2] : "box_in_scene.png";
15     int i;
16     
17     cvNamedWindow("Object", 1);
18     cvNamedWindow("Image", 1);
19     cvNamedWindow("Object Correspondence", 1);
20     
21     Mat object = imread( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
22     Mat image;
23     
24     double imgscale = 1;
25 #if 1
26     Mat _image = imread( scene_filename, CV_LOAD_IMAGE_GRAYSCALE );
27     resize(_image, image, Size(), 1./imgscale, 1./imgscale, INTER_CUBIC);
28 #else
29     image.create(cvRound(object.rows*2), cvRound(object.cols*2), CV_8U);
30     Mat M = getRotationMatrix2D(Point2f(object.cols*0.5f, object.rows*0.5f), -30, imgscale);
31     ((double*)M.data)[2] += (image.cols - object.cols)*0.5f;
32     ((double*)M.data)[5] += (image.rows - object.rows)*0.5f;
33     warpAffine(object, image, M, image.size(), INTER_LINEAR, BORDER_TRANSPARENT);
34 #endif
35
36     if( !object.data || !image.data )
37     {
38         fprintf( stderr, "Can not load %s and/or %s\n"
39                 "Usage: find_obj [<object_filename> <scene_filename>]\n",
40                 object_filename, scene_filename );
41         exit(-1);
42     }
43
44     Size patchSize(32, 32);
45     LDetector ldetector(7, 20, 2, 2000, patchSize.width, 2);
46     ldetector.setVerbose(true);
47     PlanarObjectDetector detector;
48     
49     vector<Mat> objpyr, imgpyr;
50     int blurKSize = 3;
51     double sigma = 0;
52     GaussianBlur(object, object, Size(blurKSize, blurKSize), sigma, sigma);
53     GaussianBlur(image, image, Size(blurKSize, blurKSize), sigma, sigma);
54     buildPyramid(object, objpyr, ldetector.nOctaves-1);
55     buildPyramid(image, imgpyr, ldetector.nOctaves-1);
56     
57     vector<KeyPoint> objKeypoints, imgKeypoints;
58    // PatchGenerator gen(0,256,15,true,0.6,1.5,-CV_PI,CV_PI,-CV_PI,CV_PI);
59         PatchGenerator gen(0,256,5,true,0.8,1.2,-CV_PI/2,CV_PI/2,-CV_PI/2,CV_PI/2);
60     
61     FileStorage fs("outlet_model.xml", FileStorage::READ);
62     if( fs.isOpened() )
63     {
64         detector.read(fs.getFirstTopLevelNode());
65         FileStorage fs2("outlet_model_copy.xml", FileStorage::WRITE);
66         detector.write(fs2, "outlet-detector");
67     }
68     else
69     {
70         ldetector.setVerbose(true);
71         ldetector.getMostStable2D(object, objKeypoints, 100, gen);
72         detector.setVerbose(true);
73     
74         detector.train(objpyr, objKeypoints, patchSize.width, 100, 11, 10000, ldetector, gen);
75         if( fs.open("outlet_model.xml", FileStorage::WRITE) )
76             detector.write(fs, "outlet-detector");
77     }
78     fs.release();
79         
80     vector<Point2f> dst_corners;
81     Mat correspond( object.rows + image.rows, std::max(object.cols, image.cols), CV_8UC3);
82     correspond = Scalar(0.);
83     Mat part(correspond, Rect(0, 0, object.cols, object.rows));
84     cvtColor(object, part, CV_GRAY2BGR);
85     part = Mat(correspond, Rect(0, object.rows, image.cols, image.rows));
86     cvtColor(image, part, CV_GRAY2BGR);
87
88 #if 1    
89     vector<int> pairs;
90     Mat H;
91     
92     double t = (double)getTickCount();
93     objKeypoints = detector.getModelPoints();
94 //      ldetector(imgpyr, imgKeypoints, 3000);
95     ldetector(imgpyr, imgKeypoints, 300);
96     
97     std::cout << "Object keypoints: " << objKeypoints.size() << "\n";
98     std::cout << "Image keypoints: " << imgKeypoints.size() << "\n";
99     bool found = detector(imgpyr, imgKeypoints, H, dst_corners, &pairs);
100     t = (double)getTickCount() - t;
101     printf("%gms\n", t*1000/getTickFrequency());
102     
103     if( found )
104     {
105         for( i = 0; i < 4; i++ )
106         {
107             Point r1 = dst_corners[i%4];
108             Point r2 = dst_corners[(i+1)%4];
109             line( correspond, Point(r1.x, r1.y+object.rows),
110                  Point(r2.x, r2.y+object.rows), Scalar(0,0,255) );
111         }
112     }
113     
114     for( i = 0; i < (int)pairs.size(); i += 2 )
115     {
116         line( correspond, objKeypoints[pairs[i]].pt,
117              imgKeypoints[pairs[i+1]].pt + Point2f(0,object.rows),
118              Scalar(0,255,0) );
119     }
120 #endif
121     
122     imshow( "Object Correspondence", correspond );
123     Mat objectColor;
124     cvtColor(object, objectColor, CV_GRAY2BGR);
125     for( i = 0; i < (int)objKeypoints.size(); i++ )
126     {
127         circle( objectColor, objKeypoints[i].pt, 2, Scalar(0,0,255), -1 );
128         circle( objectColor, objKeypoints[i].pt, (1 << objKeypoints[i].octave)*15, Scalar(0,255,0), 1 );
129     }
130     Mat imageColor;
131     cvtColor(image, imageColor, CV_GRAY2BGR);
132     for( i = 0; i < (int)imgKeypoints.size(); i++ )
133     {
134         circle( imageColor, imgKeypoints[i].pt, 2, Scalar(0,0,255), -1 );
135         circle( imageColor, imgKeypoints[i].pt, (1 << imgKeypoints[i].octave)*15, Scalar(0,255,0), 1 );
136     }
137     imwrite("correspond.png", correspond );
138     imshow( "Object", objectColor );
139     imshow( "Image", imageColor );
140     
141     waitKey(0);
142     return 0;
143 }