1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #include "precomp.hpp"
50 /////////////////////////////////////
53 template <typename T, class A> void releaseVector(vector<T, A>& v)
59 double toRad(double a)
61 return a * CV_PI / 180.0;
66 return fabs(v) > numeric_limits<float>::epsilon();
69 class GHT_Pos : public GeneralizedHough
75 void setTemplateImpl(const Mat& edges, const Mat& dx, const Mat& dy, Point templCenter);
76 void detectImpl(const Mat& edges, const Mat& dx, const Mat& dy, OutputArray positions, OutputArray votes);
79 virtual void processTempl() = 0;
80 virtual void processImage() = 0;
83 void convertTo(OutputArray positions, OutputArray votes);
98 vector<Vec4f> posOutBuf;
99 vector<Vec3i> voteOutBuf;
107 void GHT_Pos::setTemplateImpl(const Mat& edges, const Mat& dx, const Mat& dy, Point templCenter_)
109 templSize = edges.size();
110 templCenter = templCenter_;
111 edges.copyTo(templEdges);
118 void GHT_Pos::detectImpl(const Mat& edges, const Mat& dx, const Mat& dy, OutputArray positions, OutputArray votes)
120 imageSize = edges.size();
121 edges.copyTo(imageEdges);
130 if (!posOutBuf.empty())
134 convertTo(positions, votes);
144 void GHT_Pos::releaseImpl()
147 templCenter = Point(-1, -1);
148 templEdges.release();
153 imageEdges.release();
157 releaseVector(posOutBuf);
158 releaseVector(voteOutBuf);
161 #define votes_cmp_gt(l1, l2) (aux[l1][0] > aux[l2][0])
162 static CV_IMPLEMENT_QSORT_EX( sortIndexies, size_t, votes_cmp_gt, const Vec3i* )
164 void GHT_Pos::filterMinDist()
166 size_t oldSize = posOutBuf.size();
167 const bool hasVotes = !voteOutBuf.empty();
169 CV_Assert(!hasVotes || voteOutBuf.size() == oldSize);
171 vector<Vec4f> oldPosBuf(posOutBuf);
172 vector<Vec3i> oldVoteBuf(voteOutBuf);
174 vector<size_t> indexies(oldSize);
175 for (size_t i = 0; i < oldSize; ++i)
177 sortIndexies(&indexies[0], oldSize, &oldVoteBuf[0]);
182 const int cellSize = cvRound(minDist);
183 const int gridWidth = (imageSize.width + cellSize - 1) / cellSize;
184 const int gridHeight = (imageSize.height + cellSize - 1) / cellSize;
186 vector< vector<Point2f> > grid(gridWidth * gridHeight);
188 const double minDist2 = minDist * minDist;
190 for (size_t i = 0; i < oldSize; ++i)
192 const size_t ind = indexies[i];
194 Point2f p(oldPosBuf[ind][0], oldPosBuf[ind][1]);
198 const int xCell = static_cast<int>(p.x / cellSize);
199 const int yCell = static_cast<int>(p.y / cellSize);
207 x1 = std::max(0, x1);
208 y1 = std::max(0, y1);
209 x2 = std::min(gridWidth - 1, x2);
210 y2 = std::min(gridHeight - 1, y2);
212 for (int yy = y1; yy <= y2; ++yy)
214 for (int xx = x1; xx <= x2; ++xx)
216 const vector<Point2f>& m = grid[yy * gridWidth + xx];
218 for(size_t j = 0; j < m.size(); ++j)
220 const Point2f d = p - m[j];
222 if (d.ddot(d) < minDist2)
235 grid[yCell * gridWidth + xCell].push_back(p);
237 posOutBuf.push_back(oldPosBuf[ind]);
239 voteOutBuf.push_back(oldVoteBuf[ind]);
244 void GHT_Pos::convertTo(OutputArray _positions, OutputArray _votes)
246 const int total = static_cast<int>(posOutBuf.size());
247 const bool hasVotes = !voteOutBuf.empty();
249 CV_Assert(!hasVotes || voteOutBuf.size() == posOutBuf.size());
251 _positions.create(1, total, CV_32FC4);
252 Mat positions = _positions.getMat();
253 Mat(1, total, CV_32FC4, &posOutBuf[0]).copyTo(positions);
261 _votes.create(1, total, CV_32SC3);
262 Mat votes = _votes.getMat();
263 Mat(1, total, CV_32SC3, &voteOutBuf[0]).copyTo(votes);
268 /////////////////////////////////////
271 class GHT_Ballard_Pos : public GHT_Pos
274 AlgorithmInfo* info() const;
284 virtual void calcHist();
285 virtual void findPosInHist();
291 vector< vector<Point> > r_table;
295 CV_INIT_ALGORITHM(GHT_Ballard_Pos, "GeneralizedHough.POSITION",
296 obj.info()->addParam(obj, "minDist", obj.minDist, false, 0, 0,
297 "Minimum distance between the centers of the detected objects.");
298 obj.info()->addParam(obj, "levels", obj.levels, false, 0, 0,
300 obj.info()->addParam(obj, "votesThreshold", obj.votesThreshold, false, 0, 0,
301 "The accumulator threshold for the template centers at the detection stage. The smaller it is, the more false positions may be detected.");
302 obj.info()->addParam(obj, "dp", obj.dp, false, 0, 0,
303 "Inverse ratio of the accumulator resolution to the image resolution."))
305 GHT_Ballard_Pos::GHT_Ballard_Pos()
308 votesThreshold = 100;
312 void GHT_Ballard_Pos::releaseImpl()
314 GHT_Pos::releaseImpl();
316 releaseVector(r_table);
320 void GHT_Ballard_Pos::processTempl()
322 CV_Assert(templEdges.type() == CV_8UC1);
323 CV_Assert(templDx.type() == CV_32FC1 && templDx.size() == templSize);
324 CV_Assert(templDy.type() == templDx.type() && templDy.size() == templSize);
325 CV_Assert(levels > 0);
327 const double thetaScale = levels / 360.0;
329 r_table.resize(levels + 1);
330 for_each(r_table.begin(), r_table.end(), mem_fun_ref(&vector<Point>::clear));
332 for (int y = 0; y < templSize.height; ++y)
334 const uchar* edgesRow = templEdges.ptr(y);
335 const float* dxRow = templDx.ptr<float>(y);
336 const float* dyRow = templDy.ptr<float>(y);
338 for (int x = 0; x < templSize.width; ++x)
342 if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
344 const float theta = fastAtan2(dyRow[x], dxRow[x]);
345 const int n = cvRound(theta * thetaScale);
346 r_table[n].push_back(p - templCenter);
352 void GHT_Ballard_Pos::processImage()
358 void GHT_Ballard_Pos::calcHist()
360 CV_Assert(imageEdges.type() == CV_8UC1);
361 CV_Assert(imageDx.type() == CV_32FC1 && imageDx.size() == imageSize);
362 CV_Assert(imageDy.type() == imageDx.type() && imageDy.size() == imageSize);
363 CV_Assert(levels > 0 && r_table.size() == static_cast<size_t>(levels + 1));
366 const double thetaScale = levels / 360.0;
367 const double idp = 1.0 / dp;
369 hist.create(cvCeil(imageSize.height * idp) + 2, cvCeil(imageSize.width * idp) + 2, CV_32SC1);
372 const int rows = hist.rows - 2;
373 const int cols = hist.cols - 2;
375 for (int y = 0; y < imageSize.height; ++y)
377 const uchar* edgesRow = imageEdges.ptr(y);
378 const float* dxRow = imageDx.ptr<float>(y);
379 const float* dyRow = imageDy.ptr<float>(y);
381 for (int x = 0; x < imageSize.width; ++x)
385 if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
387 const float theta = fastAtan2(dyRow[x], dxRow[x]);
388 const int n = cvRound(theta * thetaScale);
390 const vector<Point>& r_row = r_table[n];
392 for (size_t j = 0; j < r_row.size(); ++j)
394 Point c = p - r_row[j];
396 c.x = cvRound(c.x * idp);
397 c.y = cvRound(c.y * idp);
399 if (c.x >= 0 && c.x < cols && c.y >= 0 && c.y < rows)
400 ++hist.at<int>(c.y + 1, c.x + 1);
407 void GHT_Ballard_Pos::findPosInHist()
409 CV_Assert(votesThreshold > 0);
411 const int histRows = hist.rows - 2;
412 const int histCols = hist.cols - 2;
414 for(int y = 0; y < histRows; ++y)
416 const int* prevRow = hist.ptr<int>(y);
417 const int* curRow = hist.ptr<int>(y + 1);
418 const int* nextRow = hist.ptr<int>(y + 2);
420 for(int x = 0; x < histCols; ++x)
422 const int votes = curRow[x + 1];
424 if (votes > votesThreshold && votes > curRow[x] && votes >= curRow[x + 2] && votes > prevRow[x + 1] && votes >= nextRow[x + 1])
426 posOutBuf.push_back(Vec4f(static_cast<float>(x * dp), static_cast<float>(y * dp), 1.0f, 0.0f));
427 voteOutBuf.push_back(Vec3i(votes, 0, 0));
433 /////////////////////////////////////
436 class GHT_Ballard_PosScale : public GHT_Ballard_Pos
439 AlgorithmInfo* info() const;
441 GHT_Ballard_PosScale();
445 void findPosInHist();
455 CV_INIT_ALGORITHM(GHT_Ballard_PosScale, "GeneralizedHough.POSITION_SCALE",
456 obj.info()->addParam(obj, "minDist", obj.minDist, false, 0, 0,
457 "Minimum distance between the centers of the detected objects.");
458 obj.info()->addParam(obj, "levels", obj.levels, false, 0, 0,
460 obj.info()->addParam(obj, "votesThreshold", obj.votesThreshold, false, 0, 0,
461 "The accumulator threshold for the template centers at the detection stage. The smaller it is, the more false positions may be detected.");
462 obj.info()->addParam(obj, "dp", obj.dp, false, 0, 0,
463 "Inverse ratio of the accumulator resolution to the image resolution.");
464 obj.info()->addParam(obj, "minScale", obj.minScale, false, 0, 0,
465 "Minimal scale to detect.");
466 obj.info()->addParam(obj, "maxScale", obj.maxScale, false, 0, 0,
467 "Maximal scale to detect.");
468 obj.info()->addParam(obj, "scaleStep", obj.scaleStep, false, 0, 0,
471 GHT_Ballard_PosScale::GHT_Ballard_PosScale()
478 class GHT_Ballard_PosScale::Worker : public ParallelLoopBody
481 explicit Worker(GHT_Ballard_PosScale* base_) : base(base_) {}
483 void operator ()(const Range& range) const;
486 GHT_Ballard_PosScale* base;
489 void GHT_Ballard_PosScale::Worker::operator ()(const Range& range) const
491 const double thetaScale = base->levels / 360.0;
492 const double idp = 1.0 / base->dp;
494 for (int s = range.start; s < range.end; ++s)
496 const double scale = base->minScale + s * base->scaleStep;
498 Mat curHist(base->hist.size[1], base->hist.size[2], CV_32SC1, base->hist.ptr(s + 1), base->hist.step[1]);
500 for (int y = 0; y < base->imageSize.height; ++y)
502 const uchar* edgesRow = base->imageEdges.ptr(y);
503 const float* dxRow = base->imageDx.ptr<float>(y);
504 const float* dyRow = base->imageDy.ptr<float>(y);
506 for (int x = 0; x < base->imageSize.width; ++x)
508 const Point2d p(x, y);
510 if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
512 const float theta = fastAtan2(dyRow[x], dxRow[x]);
513 const int n = cvRound(theta * thetaScale);
515 const vector<Point>& r_row = base->r_table[n];
517 for (size_t j = 0; j < r_row.size(); ++j)
519 Point2d d = r_row[j];
520 Point2d c = p - d * scale;
525 if (c.x >= 0 && c.x < base->hist.size[2] - 2 && c.y >= 0 && c.y < base->hist.size[1] - 2)
526 ++curHist.at<int>(cvRound(c.y + 1), cvRound(c.x + 1));
534 void GHT_Ballard_PosScale::calcHist()
536 CV_Assert(imageEdges.type() == CV_8UC1);
537 CV_Assert(imageDx.type() == CV_32FC1 && imageDx.size() == imageSize);
538 CV_Assert(imageDy.type() == imageDx.type() && imageDy.size() == imageSize);
539 CV_Assert(levels > 0 && r_table.size() == static_cast<size_t>(levels + 1));
541 CV_Assert(minScale > 0.0 && minScale < maxScale);
542 CV_Assert(scaleStep > 0.0);
544 const double idp = 1.0 / dp;
545 const int scaleRange = cvCeil((maxScale - minScale) / scaleStep);
547 const int sizes[] = {scaleRange + 2, cvCeil(imageSize.height * idp) + 2, cvCeil(imageSize.width * idp) + 2};
548 hist.create(3, sizes, CV_32SC1);
551 parallel_for_(Range(0, scaleRange), Worker(this));
554 void GHT_Ballard_PosScale::findPosInHist()
556 CV_Assert(votesThreshold > 0);
558 const int scaleRange = hist.size[0] - 2;
559 const int histRows = hist.size[1] - 2;
560 const int histCols = hist.size[2] - 2;
562 for (int s = 0; s < scaleRange; ++s)
564 const float scale = static_cast<float>(minScale + s * scaleStep);
566 const Mat prevHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(s), hist.step[1]);
567 const Mat curHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(s + 1), hist.step[1]);
568 const Mat nextHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(s + 2), hist.step[1]);
570 for(int y = 0; y < histRows; ++y)
572 const int* prevHistRow = prevHist.ptr<int>(y + 1);
573 const int* prevRow = curHist.ptr<int>(y);
574 const int* curRow = curHist.ptr<int>(y + 1);
575 const int* nextRow = curHist.ptr<int>(y + 2);
576 const int* nextHistRow = nextHist.ptr<int>(y + 1);
578 for(int x = 0; x < histCols; ++x)
580 const int votes = curRow[x + 1];
582 if (votes > votesThreshold &&
584 votes >= curRow[x + 2] &&
585 votes > prevRow[x + 1] &&
586 votes >= nextRow[x + 1] &&
587 votes > prevHistRow[x + 1] &&
588 votes >= nextHistRow[x + 1])
590 posOutBuf.push_back(Vec4f(static_cast<float>(x * dp), static_cast<float>(y * dp), scale, 0.0f));
591 voteOutBuf.push_back(Vec3i(votes, votes, 0));
598 /////////////////////////////////////
599 // POSITION & ROTATION
601 class GHT_Ballard_PosRotation : public GHT_Ballard_Pos
604 AlgorithmInfo* info() const;
606 GHT_Ballard_PosRotation();
610 void findPosInHist();
620 CV_INIT_ALGORITHM(GHT_Ballard_PosRotation, "GeneralizedHough.POSITION_ROTATION",
621 obj.info()->addParam(obj, "minDist", obj.minDist, false, 0, 0,
622 "Minimum distance between the centers of the detected objects.");
623 obj.info()->addParam(obj, "levels", obj.levels, false, 0, 0,
625 obj.info()->addParam(obj, "votesThreshold", obj.votesThreshold, false, 0, 0,
626 "The accumulator threshold for the template centers at the detection stage. The smaller it is, the more false positions may be detected.");
627 obj.info()->addParam(obj, "dp", obj.dp, false, 0, 0,
628 "Inverse ratio of the accumulator resolution to the image resolution.");
629 obj.info()->addParam(obj, "minAngle", obj.minAngle, false, 0, 0,
630 "Minimal rotation angle to detect in degrees.");
631 obj.info()->addParam(obj, "maxAngle", obj.maxAngle, false, 0, 0,
632 "Maximal rotation angle to detect in degrees.");
633 obj.info()->addParam(obj, "angleStep", obj.angleStep, false, 0, 0,
634 "Angle step in degrees."))
636 GHT_Ballard_PosRotation::GHT_Ballard_PosRotation()
643 class GHT_Ballard_PosRotation::Worker : public ParallelLoopBody
646 explicit Worker(GHT_Ballard_PosRotation* base_) : base(base_) {}
648 void operator ()(const Range& range) const;
651 GHT_Ballard_PosRotation* base;
654 void GHT_Ballard_PosRotation::Worker::operator ()(const Range& range) const
656 const double thetaScale = base->levels / 360.0;
657 const double idp = 1.0 / base->dp;
659 for (int a = range.start; a < range.end; ++a)
661 const double angle = base->minAngle + a * base->angleStep;
663 const double sinA = ::sin(toRad(angle));
664 const double cosA = ::cos(toRad(angle));
666 Mat curHist(base->hist.size[1], base->hist.size[2], CV_32SC1, base->hist.ptr(a + 1), base->hist.step[1]);
668 for (int y = 0; y < base->imageSize.height; ++y)
670 const uchar* edgesRow = base->imageEdges.ptr(y);
671 const float* dxRow = base->imageDx.ptr<float>(y);
672 const float* dyRow = base->imageDy.ptr<float>(y);
674 for (int x = 0; x < base->imageSize.width; ++x)
676 const Point2d p(x, y);
678 if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
680 double theta = fastAtan2(dyRow[x], dxRow[x]) - angle;
683 const int n = cvRound(theta * thetaScale);
685 const vector<Point>& r_row = base->r_table[n];
687 for (size_t j = 0; j < r_row.size(); ++j)
689 Point2d d = r_row[j];
690 Point2d c = p - Point2d(d.x * cosA - d.y * sinA, d.x * sinA + d.y * cosA);
695 if (c.x >= 0 && c.x < base->hist.size[2] - 2 && c.y >= 0 && c.y < base->hist.size[1] - 2)
696 ++curHist.at<int>(cvRound(c.y + 1), cvRound(c.x + 1));
704 void GHT_Ballard_PosRotation::calcHist()
706 CV_Assert(imageEdges.type() == CV_8UC1);
707 CV_Assert(imageDx.type() == CV_32FC1 && imageDx.size() == imageSize);
708 CV_Assert(imageDy.type() == imageDx.type() && imageDy.size() == imageSize);
709 CV_Assert(levels > 0 && r_table.size() == static_cast<size_t>(levels + 1));
711 CV_Assert(minAngle >= 0.0 && minAngle < maxAngle && maxAngle <= 360.0);
712 CV_Assert(angleStep > 0.0 && angleStep < 360.0);
714 const double idp = 1.0 / dp;
715 const int angleRange = cvCeil((maxAngle - minAngle) / angleStep);
717 const int sizes[] = {angleRange + 2, cvCeil(imageSize.height * idp) + 2, cvCeil(imageSize.width * idp) + 2};
718 hist.create(3, sizes, CV_32SC1);
721 parallel_for_(Range(0, angleRange), Worker(this));
724 void GHT_Ballard_PosRotation::findPosInHist()
726 CV_Assert(votesThreshold > 0);
728 const int angleRange = hist.size[0] - 2;
729 const int histRows = hist.size[1] - 2;
730 const int histCols = hist.size[2] - 2;
732 for (int a = 0; a < angleRange; ++a)
734 const float angle = static_cast<float>(minAngle + a * angleStep);
736 const Mat prevHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(a), hist.step[1]);
737 const Mat curHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(a + 1), hist.step[1]);
738 const Mat nextHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(a + 2), hist.step[1]);
740 for(int y = 0; y < histRows; ++y)
742 const int* prevHistRow = prevHist.ptr<int>(y + 1);
743 const int* prevRow = curHist.ptr<int>(y);
744 const int* curRow = curHist.ptr<int>(y + 1);
745 const int* nextRow = curHist.ptr<int>(y + 2);
746 const int* nextHistRow = nextHist.ptr<int>(y + 1);
748 for(int x = 0; x < histCols; ++x)
750 const int votes = curRow[x + 1];
752 if (votes > votesThreshold &&
754 votes >= curRow[x + 2] &&
755 votes > prevRow[x + 1] &&
756 votes >= nextRow[x + 1] &&
757 votes > prevHistRow[x + 1] &&
758 votes >= nextHistRow[x + 1])
760 posOutBuf.push_back(Vec4f(static_cast<float>(x * dp), static_cast<float>(y * dp), 1.0f, angle));
761 voteOutBuf.push_back(Vec3i(votes, 0, votes));
768 /////////////////////////////////////////
769 // POSITION & SCALE & ROTATION
771 double clampAngle(double a)
783 bool angleEq(double a, double b, double eps = 1.0)
785 return (fabs(clampAngle(a - b)) <= eps);
788 class GHT_Guil_Full : public GHT_Pos
791 AlgorithmInfo* info() const;
819 void buildFeatureList(const Mat& edges, const Mat& dx, const Mat& dy, vector< vector<Feature> >& features, Point2d center = Point2d());
820 void getContourPoints(const Mat& edges, const Mat& dx, const Mat& dy, vector<ContourPoint>& points);
822 void calcOrientation();
823 void calcScale(double angle);
824 void calcPosition(double angle, int angleVotes, double scale, int scaleVotes);
844 vector< vector<Feature> > templFeatures;
845 vector< vector<Feature> > imageFeatures;
847 vector< pair<double, int> > angles;
848 vector< pair<double, int> > scales;
851 CV_INIT_ALGORITHM(GHT_Guil_Full, "GeneralizedHough.POSITION_SCALE_ROTATION",
852 obj.info()->addParam(obj, "minDist", obj.minDist, false, 0, 0,
853 "Minimum distance between the centers of the detected objects.");
854 obj.info()->addParam(obj, "maxSize", obj.maxSize, false, 0, 0,
855 "Maximal size of inner buffers.");
856 obj.info()->addParam(obj, "xi", obj.xi, false, 0, 0,
857 "Angle difference in degrees between two points in feature.");
858 obj.info()->addParam(obj, "levels", obj.levels, false, 0, 0,
859 "Feature table levels.");
860 obj.info()->addParam(obj, "angleEpsilon", obj.angleEpsilon, false, 0, 0,
861 "Maximal difference between angles that treated as equal.");
862 obj.info()->addParam(obj, "minAngle", obj.minAngle, false, 0, 0,
863 "Minimal rotation angle to detect in degrees.");
864 obj.info()->addParam(obj, "maxAngle", obj.maxAngle, false, 0, 0,
865 "Maximal rotation angle to detect in degrees.");
866 obj.info()->addParam(obj, "angleStep", obj.angleStep, false, 0, 0,
867 "Angle step in degrees.");
868 obj.info()->addParam(obj, "angleThresh", obj.angleThresh, false, 0, 0,
870 obj.info()->addParam(obj, "minScale", obj.minScale, false, 0, 0,
871 "Minimal scale to detect.");
872 obj.info()->addParam(obj, "maxScale", obj.maxScale, false, 0, 0,
873 "Maximal scale to detect.");
874 obj.info()->addParam(obj, "scaleStep", obj.scaleStep, false, 0, 0,
876 obj.info()->addParam(obj, "scaleThresh", obj.scaleThresh, false, 0, 0,
878 obj.info()->addParam(obj, "dp", obj.dp, false, 0, 0,
879 "Inverse ratio of the accumulator resolution to the image resolution.");
880 obj.info()->addParam(obj, "posThresh", obj.posThresh, false, 0, 0,
881 "Position threshold."))
883 GHT_Guil_Full::GHT_Guil_Full()
904 void GHT_Guil_Full::releaseImpl()
906 GHT_Pos::releaseImpl();
908 releaseVector(templFeatures);
909 releaseVector(imageFeatures);
911 releaseVector(angles);
912 releaseVector(scales);
915 void GHT_Guil_Full::processTempl()
917 buildFeatureList(templEdges, templDx, templDy, templFeatures, templCenter);
920 void GHT_Guil_Full::processImage()
922 buildFeatureList(imageEdges, imageDx, imageDy, imageFeatures);
926 for (size_t i = 0; i < angles.size(); ++i)
928 const double angle = angles[i].first;
929 const int angleVotes = angles[i].second;
933 for (size_t j = 0; j < scales.size(); ++j)
935 const double scale = scales[j].first;
936 const int scaleVotes = scales[j].second;
938 calcPosition(angle, angleVotes, scale, scaleVotes);
943 void GHT_Guil_Full::buildFeatureList(const Mat& edges, const Mat& dx, const Mat& dy, vector< vector<Feature> >& features, Point2d center)
945 CV_Assert(levels > 0);
947 const double maxDist = sqrt((double) templSize.width * templSize.width + templSize.height * templSize.height) * maxScale;
949 const double alphaScale = levels / 360.0;
951 vector<ContourPoint> points;
952 getContourPoints(edges, dx, dy, points);
954 features.resize(levels + 1);
955 for_each(features.begin(), features.end(), mem_fun_ref(&vector<Feature>::clear));
956 for_each(features.begin(), features.end(), bind2nd(mem_fun_ref(&vector<Feature>::reserve), maxSize));
958 for (size_t i = 0; i < points.size(); ++i)
960 ContourPoint p1 = points[i];
962 for (size_t j = 0; j < points.size(); ++j)
964 ContourPoint p2 = points[j];
966 if (angleEq(p1.theta - p2.theta, xi, angleEpsilon))
968 const Point2d d = p1.pos - p2.pos;
975 f.alpha12 = clampAngle(fastAtan2((float)d.y, (float)d.x) - p1.theta);
981 f.r1 = p1.pos - center;
982 f.r2 = p2.pos - center;
984 const int n = cvRound(f.alpha12 * alphaScale);
986 if (features[n].size() < static_cast<size_t>(maxSize))
987 features[n].push_back(f);
993 void GHT_Guil_Full::getContourPoints(const Mat& edges, const Mat& dx, const Mat& dy, vector<ContourPoint>& points)
995 CV_Assert(edges.type() == CV_8UC1);
996 CV_Assert(dx.type() == CV_32FC1 && dx.size == edges.size);
997 CV_Assert(dy.type() == dx.type() && dy.size == edges.size);
1000 points.reserve(edges.size().area());
1002 for (int y = 0; y < edges.rows; ++y)
1004 const uchar* edgesRow = edges.ptr(y);
1005 const float* dxRow = dx.ptr<float>(y);
1006 const float* dyRow = dy.ptr<float>(y);
1008 for (int x = 0; x < edges.cols; ++x)
1010 if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
1014 p.pos = Point2d(x, y);
1015 p.theta = fastAtan2(dyRow[x], dxRow[x]);
1017 points.push_back(p);
1023 void GHT_Guil_Full::calcOrientation()
1025 CV_Assert(levels > 0);
1026 CV_Assert(templFeatures.size() == static_cast<size_t>(levels + 1));
1027 CV_Assert(imageFeatures.size() == templFeatures.size());
1028 CV_Assert(minAngle >= 0.0 && minAngle < maxAngle && maxAngle <= 360.0);
1029 CV_Assert(angleStep > 0.0 && angleStep < 360.0);
1030 CV_Assert(angleThresh > 0);
1032 const double iAngleStep = 1.0 / angleStep;
1033 const int angleRange = cvCeil((maxAngle - minAngle) * iAngleStep);
1035 vector<int> OHist(angleRange + 1, 0);
1036 for (int i = 0; i <= levels; ++i)
1038 const vector<Feature>& templRow = templFeatures[i];
1039 const vector<Feature>& imageRow = imageFeatures[i];
1041 for (size_t j = 0; j < templRow.size(); ++j)
1043 Feature templF = templRow[j];
1045 for (size_t k = 0; k < imageRow.size(); ++k)
1047 Feature imF = imageRow[k];
1049 const double angle = clampAngle(imF.p1.theta - templF.p1.theta);
1050 if (angle >= minAngle && angle <= maxAngle)
1052 const int n = cvRound((angle - minAngle) * iAngleStep);
1061 for (int n = 0; n < angleRange; ++n)
1063 if (OHist[n] >= angleThresh)
1065 const double angle = minAngle + n * angleStep;
1066 angles.push_back(make_pair(angle, OHist[n]));
1071 void GHT_Guil_Full::calcScale(double angle)
1073 CV_Assert(levels > 0);
1074 CV_Assert(templFeatures.size() == static_cast<size_t>(levels + 1));
1075 CV_Assert(imageFeatures.size() == templFeatures.size());
1076 CV_Assert(minScale > 0.0 && minScale < maxScale);
1077 CV_Assert(scaleStep > 0.0);
1078 CV_Assert(scaleThresh > 0);
1080 const double iScaleStep = 1.0 / scaleStep;
1081 const int scaleRange = cvCeil((maxScale - minScale) * iScaleStep);
1083 vector<int> SHist(scaleRange + 1, 0);
1085 for (int i = 0; i <= levels; ++i)
1087 const vector<Feature>& templRow = templFeatures[i];
1088 const vector<Feature>& imageRow = imageFeatures[i];
1090 for (size_t j = 0; j < templRow.size(); ++j)
1092 Feature templF = templRow[j];
1094 templF.p1.theta += angle;
1096 for (size_t k = 0; k < imageRow.size(); ++k)
1098 Feature imF = imageRow[k];
1100 if (angleEq(imF.p1.theta, templF.p1.theta, angleEpsilon))
1102 const double scale = imF.d12 / templF.d12;
1103 if (scale >= minScale && scale <= maxScale)
1105 const int s = cvRound((scale - minScale) * iScaleStep);
1115 for (int s = 0; s < scaleRange; ++s)
1117 if (SHist[s] >= scaleThresh)
1119 const double scale = minScale + s * scaleStep;
1120 scales.push_back(make_pair(scale, SHist[s]));
1125 void GHT_Guil_Full::calcPosition(double angle, int angleVotes, double scale, int scaleVotes)
1127 CV_Assert(levels > 0);
1128 CV_Assert(templFeatures.size() == static_cast<size_t>(levels + 1));
1129 CV_Assert(imageFeatures.size() == templFeatures.size());
1130 CV_Assert(dp > 0.0);
1131 CV_Assert(posThresh > 0);
1133 const double sinVal = sin(toRad(angle));
1134 const double cosVal = cos(toRad(angle));
1135 const double idp = 1.0 / dp;
1137 const int histRows = cvCeil(imageSize.height * idp);
1138 const int histCols = cvCeil(imageSize.width * idp);
1140 Mat DHist(histRows + 2, histCols + 2, CV_32SC1, Scalar::all(0));
1142 for (int i = 0; i <= levels; ++i)
1144 const vector<Feature>& templRow = templFeatures[i];
1145 const vector<Feature>& imageRow = imageFeatures[i];
1147 for (size_t j = 0; j < templRow.size(); ++j)
1149 Feature templF = templRow[j];
1151 templF.p1.theta += angle;
1156 templF.r1 = Point2d(cosVal * templF.r1.x - sinVal * templF.r1.y, sinVal * templF.r1.x + cosVal * templF.r1.y);
1157 templF.r2 = Point2d(cosVal * templF.r2.x - sinVal * templF.r2.y, sinVal * templF.r2.x + cosVal * templF.r2.y);
1159 for (size_t k = 0; k < imageRow.size(); ++k)
1161 Feature imF = imageRow[k];
1163 if (angleEq(imF.p1.theta, templF.p1.theta, angleEpsilon))
1167 c1 = imF.p1.pos - templF.r1;
1170 c2 = imF.p2.pos - templF.r2;
1173 if (fabs(c1.x - c2.x) > 1 || fabs(c1.y - c2.y) > 1)
1176 if (c1.y >= 0 && c1.y < histRows && c1.x >= 0 && c1.x < histCols)
1177 ++DHist.at<int>(cvRound(c1.y) + 1, cvRound(c1.x) + 1);
1183 for(int y = 0; y < histRows; ++y)
1185 const int* prevRow = DHist.ptr<int>(y);
1186 const int* curRow = DHist.ptr<int>(y + 1);
1187 const int* nextRow = DHist.ptr<int>(y + 2);
1189 for(int x = 0; x < histCols; ++x)
1191 const int votes = curRow[x + 1];
1193 if (votes > posThresh && votes > curRow[x] && votes >= curRow[x + 2] && votes > prevRow[x + 1] && votes >= nextRow[x + 1])
1195 posOutBuf.push_back(Vec4f(static_cast<float>(x * dp), static_cast<float>(y * dp), static_cast<float>(scale), static_cast<float>(angle)));
1196 voteOutBuf.push_back(Vec3i(votes, scaleVotes, angleVotes));
1203 Ptr<GeneralizedHough> cv::GeneralizedHough::create(int method)
1208 CV_Assert( !GHT_Ballard_Pos_info_auto.name().empty() );
1209 return new GHT_Ballard_Pos();
1211 case (GHT_POSITION | GHT_SCALE):
1212 CV_Assert( !GHT_Ballard_PosScale_info_auto.name().empty() );
1213 return new GHT_Ballard_PosScale();
1215 case (GHT_POSITION | GHT_ROTATION):
1216 CV_Assert( !GHT_Ballard_PosRotation_info_auto.name().empty() );
1217 return new GHT_Ballard_PosRotation();
1219 case (GHT_POSITION | GHT_SCALE | GHT_ROTATION):
1220 CV_Assert( !GHT_Guil_Full_info_auto.name().empty() );
1221 return new GHT_Guil_Full();
1224 CV_Error(CV_StsBadArg, "Unsupported method");
1225 return Ptr<GeneralizedHough>();
1228 cv::GeneralizedHough::~GeneralizedHough()
1232 void cv::GeneralizedHough::setTemplate(InputArray _templ, int cannyThreshold, Point templCenter)
1234 Mat templ = _templ.getMat();
1236 CV_Assert(templ.type() == CV_8UC1);
1237 CV_Assert(cannyThreshold > 0);
1239 Canny(templ, edges_, cannyThreshold / 2, cannyThreshold);
1240 Sobel(templ, dx_, CV_32F, 1, 0);
1241 Sobel(templ, dy_, CV_32F, 0, 1);
1243 if (templCenter == Point(-1, -1))
1244 templCenter = Point(templ.cols / 2, templ.rows / 2);
1246 setTemplateImpl(edges_, dx_, dy_, templCenter);
1249 void cv::GeneralizedHough::setTemplate(InputArray _edges, InputArray _dx, InputArray _dy, Point templCenter)
1251 Mat edges = _edges.getMat();
1252 Mat dx = _dx.getMat();
1253 Mat dy = _dy.getMat();
1255 if (templCenter == Point(-1, -1))
1256 templCenter = Point(edges.cols / 2, edges.rows / 2);
1258 setTemplateImpl(edges, dx, dy, templCenter);
1261 void cv::GeneralizedHough::detect(InputArray _image, OutputArray positions, OutputArray votes, int cannyThreshold)
1263 Mat image = _image.getMat();
1265 CV_Assert(image.type() == CV_8UC1);
1266 CV_Assert(cannyThreshold > 0);
1268 Canny(image, edges_, cannyThreshold / 2, cannyThreshold);
1269 Sobel(image, dx_, CV_32F, 1, 0);
1270 Sobel(image, dy_, CV_32F, 0, 1);
1272 detectImpl(edges_, dx_, dy_, positions, votes);
1275 void cv::GeneralizedHough::detect(InputArray _edges, InputArray _dx, InputArray _dy, OutputArray positions, OutputArray votes)
1277 cv::Mat edges = _edges.getMat();
1278 cv::Mat dx = _dx.getMat();
1279 cv::Mat dy = _dy.getMat();
1281 detectImpl(edges, dx, dy, positions, votes);
1284 void cv::GeneralizedHough::release()