5 #include "opencv2/core/core.hpp"
6 #include "opencv2/imgproc/imgproc.hpp"
7 #include "opencv2/gpu/gpu.hpp"
8 #include "opencv2/highgui/highgui.hpp"
9 #include "opencv2/contrib/contrib.hpp"
13 using namespace cv::gpu;
15 static Mat loadImage(const string& name)
17 Mat image = imread(name, IMREAD_GRAYSCALE);
20 cerr << "Can't load image - " << name << endl;
26 int main(int argc, const char* argv[])
28 CommandLineParser cmd(argc, argv,
29 "{ image i | pic1.png | input image }"
30 "{ template t | templ.png | template image }"
31 "{ scale s | | estimate scale }"
32 "{ rotation r | | estimate rotation }"
33 "{ gpu | | use gpu version }"
34 "{ minDist | 100 | minimum distance between the centers of the detected objects }"
35 "{ levels | 360 | R-Table levels }"
36 "{ votesThreshold | 30 | the accumulator threshold for the template centers at the detection stage. The smaller it is, the more false positions may be detected }"
37 "{ angleThresh | 10000 | angle votes treshold }"
38 "{ scaleThresh | 1000 | scale votes treshold }"
39 "{ posThresh | 100 | position votes threshold }"
40 "{ dp | 2 | inverse ratio of the accumulator resolution to the image resolution }"
41 "{ minScale | 0.5 | minimal scale to detect }"
42 "{ maxScale | 2 | maximal scale to detect }"
43 "{ scaleStep | 0.05 | scale step }"
44 "{ minAngle | 0 | minimal rotation angle to detect in degrees }"
45 "{ maxAngle | 360 | maximal rotation angle to detect in degrees }"
46 "{ angleStep | 1 | angle step in degrees }"
47 "{ maxSize | 1000 | maximal size of inner buffers }"
48 "{ help h ? | | print help message }"
51 cmd.about("This program demonstrates arbitary object finding with the Generalized Hough transform.");
59 const string templName = cmd.get<string>("template");
60 const string imageName = cmd.get<string>("image");
61 const bool estimateScale = cmd.has("scale");
62 const bool estimateRotation = cmd.has("rotation");
63 const bool useGpu = cmd.has("gpu");
64 const double minDist = cmd.get<double>("minDist");
65 const int levels = cmd.get<int>("levels");
66 const int votesThreshold = cmd.get<int>("votesThreshold");
67 const int angleThresh = cmd.get<int>("angleThresh");
68 const int scaleThresh = cmd.get<int>("scaleThresh");
69 const int posThresh = cmd.get<int>("posThresh");
70 const double dp = cmd.get<double>("dp");
71 const double minScale = cmd.get<double>("minScale");
72 const double maxScale = cmd.get<double>("maxScale");
73 const double scaleStep = cmd.get<double>("scaleStep");
74 const double minAngle = cmd.get<double>("minAngle");
75 const double maxAngle = cmd.get<double>("maxAngle");
76 const double angleStep = cmd.get<double>("angleStep");
77 const int maxSize = cmd.get<int>("maxSize");
85 Mat templ = loadImage(templName);
86 Mat image = loadImage(imageName);
88 int method = GHT_POSITION;
92 method += GHT_ROTATION;
94 vector<Vec4f> position;
99 GpuMat d_templ(templ);
100 GpuMat d_image(image);
103 Ptr<GeneralizedHough_GPU> d_hough = GeneralizedHough_GPU::create(method);
104 d_hough->set("minDist", minDist);
105 d_hough->set("levels", levels);
106 d_hough->set("dp", dp);
107 d_hough->set("maxSize", maxSize);
108 if (estimateScale && estimateRotation)
110 d_hough->set("angleThresh", angleThresh);
111 d_hough->set("scaleThresh", scaleThresh);
112 d_hough->set("posThresh", posThresh);
116 d_hough->set("votesThreshold", votesThreshold);
120 d_hough->set("minScale", minScale);
121 d_hough->set("maxScale", maxScale);
122 d_hough->set("scaleStep", scaleStep);
124 if (estimateRotation)
126 d_hough->set("minAngle", minAngle);
127 d_hough->set("maxAngle", maxAngle);
128 d_hough->set("angleStep", angleStep);
131 d_hough->setTemplate(d_templ);
135 d_hough->detect(d_image, d_position);
136 d_hough->download(d_position, position);
142 Ptr<GeneralizedHough> hough = GeneralizedHough::create(method);
143 hough->set("minDist", minDist);
144 hough->set("levels", levels);
145 hough->set("dp", dp);
146 if (estimateScale && estimateRotation)
148 hough->set("angleThresh", angleThresh);
149 hough->set("scaleThresh", scaleThresh);
150 hough->set("posThresh", posThresh);
151 hough->set("maxSize", maxSize);
155 hough->set("votesThreshold", votesThreshold);
159 hough->set("minScale", minScale);
160 hough->set("maxScale", maxScale);
161 hough->set("scaleStep", scaleStep);
163 if (estimateRotation)
165 hough->set("minAngle", minAngle);
166 hough->set("maxAngle", maxAngle);
167 hough->set("angleStep", angleStep);
170 hough->setTemplate(templ);
174 hough->detect(image, position);
179 cout << "Found : " << position.size() << " objects" << endl;
180 cout << "Detection time : " << tm.getTimeMilli() << " ms" << endl;
183 cvtColor(image, out, COLOR_GRAY2BGR);
185 for (size_t i = 0; i < position.size(); ++i)
187 Point2f pos(position[i][0], position[i][1]);
188 float scale = position[i][2];
189 float angle = position[i][3];
193 rect.size = Size2f(templ.cols * scale, templ.rows * scale);
199 line(out, pts[0], pts[1], Scalar(0, 0, 255), 3);
200 line(out, pts[1], pts[2], Scalar(0, 0, 255), 3);
201 line(out, pts[2], pts[3], Scalar(0, 0, 255), 3);
202 line(out, pts[3], pts[0], Scalar(0, 0, 255), 3);