5 #include "opencv2/core.hpp"
6 #include "opencv2/core/utility.hpp"
7 #include "opencv2/imgproc.hpp"
8 #include "opencv2/cudaimgproc.hpp"
9 #include "opencv2/highgui.hpp"
14 static Mat loadImage(const string& name)
16 Mat image = imread(name, IMREAD_GRAYSCALE);
19 cerr << "Can't load image - " << name << endl;
25 int main(int argc, const char* argv[])
27 CommandLineParser cmd(argc, argv,
28 "{ image i | ../data/pic1.png | input image }"
29 "{ template t | templ.png | template image }"
30 "{ full | | estimate scale and rotation }"
31 "{ gpu | | use gpu version }"
32 "{ minDist | 100 | minimum distance between the centers of the detected objects }"
33 "{ levels | 360 | R-Table levels }"
34 "{ votesThreshold | 30 | the accumulator threshold for the template centers at the detection stage. The smaller it is, the more false positions may be detected }"
35 "{ angleThresh | 10000 | angle votes treshold }"
36 "{ scaleThresh | 1000 | scale votes treshold }"
37 "{ posThresh | 100 | position votes threshold }"
38 "{ dp | 2 | inverse ratio of the accumulator resolution to the image resolution }"
39 "{ minScale | 0.5 | minimal scale to detect }"
40 "{ maxScale | 2 | maximal scale to detect }"
41 "{ scaleStep | 0.05 | scale step }"
42 "{ minAngle | 0 | minimal rotation angle to detect in degrees }"
43 "{ maxAngle | 360 | maximal rotation angle to detect in degrees }"
44 "{ angleStep | 1 | angle step in degrees }"
45 "{ maxBufSize | 1000 | maximal size of inner buffers }"
46 "{ help h ? | | print help message }"
49 cmd.about("This program demonstrates arbitary object finding with the Generalized Hough transform.");
57 const string templName = cmd.get<string>("template");
58 const string imageName = cmd.get<string>("image");
59 const bool full = cmd.has("full");
60 const bool useGpu = cmd.has("gpu");
61 const double minDist = cmd.get<double>("minDist");
62 const int levels = cmd.get<int>("levels");
63 const int votesThreshold = cmd.get<int>("votesThreshold");
64 const int angleThresh = cmd.get<int>("angleThresh");
65 const int scaleThresh = cmd.get<int>("scaleThresh");
66 const int posThresh = cmd.get<int>("posThresh");
67 const double dp = cmd.get<double>("dp");
68 const double minScale = cmd.get<double>("minScale");
69 const double maxScale = cmd.get<double>("maxScale");
70 const double scaleStep = cmd.get<double>("scaleStep");
71 const double minAngle = cmd.get<double>("minAngle");
72 const double maxAngle = cmd.get<double>("maxAngle");
73 const double angleStep = cmd.get<double>("angleStep");
74 const int maxBufSize = cmd.get<int>("maxBufSize");
82 Mat templ = loadImage(templName);
83 Mat image = loadImage(imageName);
85 Ptr<GeneralizedHough> alg;
89 Ptr<GeneralizedHoughBallard> ballard = useGpu ? cuda::createGeneralizedHoughBallard() : createGeneralizedHoughBallard();
91 ballard->setMinDist(minDist);
92 ballard->setLevels(levels);
94 ballard->setMaxBufferSize(maxBufSize);
95 ballard->setVotesThreshold(votesThreshold);
101 Ptr<GeneralizedHoughGuil> guil = useGpu ? cuda::createGeneralizedHoughGuil() : createGeneralizedHoughGuil();
103 guil->setMinDist(minDist);
104 guil->setLevels(levels);
106 guil->setMaxBufferSize(maxBufSize);
108 guil->setMinAngle(minAngle);
109 guil->setMaxAngle(maxAngle);
110 guil->setAngleStep(angleStep);
111 guil->setAngleThresh(angleThresh);
113 guil->setMinScale(minScale);
114 guil->setMaxScale(maxScale);
115 guil->setScaleStep(scaleStep);
116 guil->setScaleThresh(scaleThresh);
118 guil->setPosThresh(posThresh);
123 vector<Vec4f> position;
128 cuda::GpuMat d_templ(templ);
129 cuda::GpuMat d_image(image);
130 cuda::GpuMat d_position;
132 alg->setTemplate(d_templ);
136 alg->detect(d_image, d_position);
137 d_position.download(position);
143 alg->setTemplate(templ);
147 alg->detect(image, position);
152 cout << "Found : " << position.size() << " objects" << endl;
153 cout << "Detection time : " << tm.getTimeMilli() << " ms" << endl;
156 cv::cvtColor(image, out, COLOR_GRAY2BGR);
158 for (size_t i = 0; i < position.size(); ++i)
160 Point2f pos(position[i][0], position[i][1]);
161 float scale = position[i][2];
162 float angle = position[i][3];
166 rect.size = Size2f(templ.cols * scale, templ.rows * scale);
172 line(out, pts[0], pts[1], Scalar(0, 0, 255), 3);
173 line(out, pts[1], pts[2], Scalar(0, 0, 255), 3);
174 line(out, pts[2], pts[3], Scalar(0, 0, 255), 3);
175 line(out, pts[3], pts[0], Scalar(0, 0, 255), 3);