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"
49 /////////////////////////////////////
52 template <typename T, class A> void releaseVector(vector<T, A>& v)
58 double toRad(double a)
60 return a * CV_PI / 180.0;
65 return fabs(v) > numeric_limits<float>::epsilon();
67 bool notNull(double v)
69 return fabs(v) > numeric_limits<double>::epsilon();
72 class GHT_Pos : public GeneralizedHough
78 void setTemplateImpl(const Mat& edges, const Mat& dx, const Mat& dy, Point templCenter);
79 void detectImpl(const Mat& edges, const Mat& dx, const Mat& dy, OutputArray positions, OutputArray votes);
82 virtual void processTempl() = 0;
83 virtual void processImage() = 0;
86 void convertTo(OutputArray positions, OutputArray votes);
101 vector<Vec4f> posOutBuf;
102 vector<Vec3i> voteOutBuf;
110 void GHT_Pos::setTemplateImpl(const Mat& edges, const Mat& dx, const Mat& dy, Point templCenter_)
112 templSize = edges.size();
113 templCenter = templCenter_;
114 edges.copyTo(templEdges);
121 void GHT_Pos::detectImpl(const Mat& edges, const Mat& dx, const Mat& dy, OutputArray positions, OutputArray votes)
123 imageSize = edges.size();
124 edges.copyTo(imageEdges);
133 if (!posOutBuf.empty())
137 convertTo(positions, votes);
147 void GHT_Pos::releaseImpl()
150 templCenter = Point(-1, -1);
151 templEdges.release();
156 imageEdges.release();
160 releaseVector(posOutBuf);
161 releaseVector(voteOutBuf);
164 #define votes_cmp_gt(l1, l2) (aux[l1][0] > aux[l2][0])
165 static CV_IMPLEMENT_QSORT_EX( sortIndexies, size_t, votes_cmp_gt, const Vec3i* )
167 void GHT_Pos::filterMinDist()
169 size_t oldSize = posOutBuf.size();
170 const bool hasVotes = !voteOutBuf.empty();
172 CV_Assert(!hasVotes || voteOutBuf.size() == oldSize);
174 vector<Vec4f> oldPosBuf(posOutBuf);
175 vector<Vec3i> oldVoteBuf(voteOutBuf);
177 vector<size_t> indexies(oldSize);
178 for (size_t i = 0; i < oldSize; ++i)
180 sortIndexies(&indexies[0], oldSize, &oldVoteBuf[0]);
185 const int cellSize = cvRound(minDist);
186 const int gridWidth = (imageSize.width + cellSize - 1) / cellSize;
187 const int gridHeight = (imageSize.height + cellSize - 1) / cellSize;
189 vector< vector<Point2f> > grid(gridWidth * gridHeight);
191 const double minDist2 = minDist * minDist;
193 for (size_t i = 0; i < oldSize; ++i)
195 const size_t ind = indexies[i];
197 Point2f p(oldPosBuf[ind][0], oldPosBuf[ind][1]);
201 const int xCell = static_cast<int>(p.x / cellSize);
202 const int yCell = static_cast<int>(p.y / cellSize);
210 x1 = std::max(0, x1);
211 y1 = std::max(0, y1);
212 x2 = std::min(gridWidth - 1, x2);
213 y2 = std::min(gridHeight - 1, y2);
215 for (int yy = y1; yy <= y2; ++yy)
217 for (int xx = x1; xx <= x2; ++xx)
219 const vector<Point2f>& m = grid[yy * gridWidth + xx];
221 for(size_t j = 0; j < m.size(); ++j)
223 const Point2f d = p - m[j];
225 if (d.ddot(d) < minDist2)
238 grid[yCell * gridWidth + xCell].push_back(p);
240 posOutBuf.push_back(oldPosBuf[ind]);
242 voteOutBuf.push_back(oldVoteBuf[ind]);
247 void GHT_Pos::convertTo(OutputArray _positions, OutputArray _votes)
249 const int total = static_cast<int>(posOutBuf.size());
250 const bool hasVotes = !voteOutBuf.empty();
252 CV_Assert(!hasVotes || voteOutBuf.size() == posOutBuf.size());
254 _positions.create(1, total, CV_32FC4);
255 Mat positions = _positions.getMat();
256 Mat(1, total, CV_32FC4, &posOutBuf[0]).copyTo(positions);
264 _votes.create(1, total, CV_32SC3);
265 Mat votes = _votes.getMat();
266 Mat(1, total, CV_32SC3, &voteOutBuf[0]).copyTo(votes);
271 /////////////////////////////////////
274 class GHT_Ballard_Pos : public GHT_Pos
277 AlgorithmInfo* info() const;
287 virtual void calcHist();
288 virtual void findPosInHist();
294 vector< vector<Point> > r_table;
298 CV_INIT_ALGORITHM(GHT_Ballard_Pos, "GeneralizedHough.POSITION",
299 obj.info()->addParam(obj, "minDist", obj.minDist, false, 0, 0,
300 "Minimum distance between the centers of the detected objects.");
301 obj.info()->addParam(obj, "levels", obj.levels, false, 0, 0,
303 obj.info()->addParam(obj, "votesThreshold", obj.votesThreshold, false, 0, 0,
304 "The accumulator threshold for the template centers at the detection stage. The smaller it is, the more false positions may be detected.");
305 obj.info()->addParam(obj, "dp", obj.dp, false, 0, 0,
306 "Inverse ratio of the accumulator resolution to the image resolution."));
308 GHT_Ballard_Pos::GHT_Ballard_Pos()
311 votesThreshold = 100;
315 void GHT_Ballard_Pos::releaseImpl()
317 GHT_Pos::releaseImpl();
319 releaseVector(r_table);
323 void GHT_Ballard_Pos::processTempl()
325 CV_Assert(templEdges.type() == CV_8UC1);
326 CV_Assert(templDx.type() == CV_32FC1 && templDx.size() == templSize);
327 CV_Assert(templDy.type() == templDx.type() && templDy.size() == templSize);
328 CV_Assert(levels > 0);
330 const double thetaScale = levels / 360.0;
332 r_table.resize(levels + 1);
333 for_each(r_table.begin(), r_table.end(), mem_fun_ref(&vector<Point>::clear));
335 for (int y = 0; y < templSize.height; ++y)
337 const uchar* edgesRow = templEdges.ptr(y);
338 const float* dxRow = templDx.ptr<float>(y);
339 const float* dyRow = templDy.ptr<float>(y);
341 for (int x = 0; x < templSize.width; ++x)
345 if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
347 const float theta = fastAtan2(dyRow[x], dxRow[x]);
348 const int n = cvRound(theta * thetaScale);
349 r_table[n].push_back(p - templCenter);
355 void GHT_Ballard_Pos::processImage()
361 void GHT_Ballard_Pos::calcHist()
363 CV_Assert(imageEdges.type() == CV_8UC1);
364 CV_Assert(imageDx.type() == CV_32FC1 && imageDx.size() == imageSize);
365 CV_Assert(imageDy.type() == imageDx.type() && imageDy.size() == imageSize);
366 CV_Assert(levels > 0 && r_table.size() == static_cast<size_t>(levels + 1));
369 const double thetaScale = levels / 360.0;
370 const double idp = 1.0 / dp;
372 hist.create(cvCeil(imageSize.height * idp) + 2, cvCeil(imageSize.width * idp) + 2, CV_32SC1);
375 const int rows = hist.rows - 2;
376 const int cols = hist.cols - 2;
378 for (int y = 0; y < imageSize.height; ++y)
380 const uchar* edgesRow = imageEdges.ptr(y);
381 const float* dxRow = imageDx.ptr<float>(y);
382 const float* dyRow = imageDy.ptr<float>(y);
384 for (int x = 0; x < imageSize.width; ++x)
388 if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
390 const float theta = fastAtan2(dyRow[x], dxRow[x]);
391 const int n = cvRound(theta * thetaScale);
393 const vector<Point>& r_row = r_table[n];
395 for (size_t j = 0; j < r_row.size(); ++j)
397 Point c = p - r_row[j];
399 c.x = cvRound(c.x * idp);
400 c.y = cvRound(c.y * idp);
402 if (c.x >= 0 && c.x < cols && c.y >= 0 && c.y < rows)
403 ++hist.at<int>(c.y + 1, c.x + 1);
410 void GHT_Ballard_Pos::findPosInHist()
412 CV_Assert(votesThreshold > 0);
414 const int histRows = hist.rows - 2;
415 const int histCols = hist.cols - 2;
417 for(int y = 0; y < histRows; ++y)
419 const int* prevRow = hist.ptr<int>(y);
420 const int* curRow = hist.ptr<int>(y + 1);
421 const int* nextRow = hist.ptr<int>(y + 2);
423 for(int x = 0; x < histCols; ++x)
425 const int votes = curRow[x + 1];
427 if (votes > votesThreshold && votes > curRow[x] && votes >= curRow[x + 2] && votes > prevRow[x + 1] && votes >= nextRow[x + 1])
429 posOutBuf.push_back(Vec4f(static_cast<float>(x * dp), static_cast<float>(y * dp), 1.0f, 0.0f));
430 voteOutBuf.push_back(Vec3i(votes, 0, 0));
436 /////////////////////////////////////
439 class GHT_Ballard_PosScale : public GHT_Ballard_Pos
442 AlgorithmInfo* info() const;
444 GHT_Ballard_PosScale();
448 void findPosInHist();
458 CV_INIT_ALGORITHM(GHT_Ballard_PosScale, "GeneralizedHough.POSITION_SCALE",
459 obj.info()->addParam(obj, "minDist", obj.minDist, false, 0, 0,
460 "Minimum distance between the centers of the detected objects.");
461 obj.info()->addParam(obj, "levels", obj.levels, false, 0, 0,
463 obj.info()->addParam(obj, "votesThreshold", obj.votesThreshold, false, 0, 0,
464 "The accumulator threshold for the template centers at the detection stage. The smaller it is, the more false positions may be detected.");
465 obj.info()->addParam(obj, "dp", obj.dp, false, 0, 0,
466 "Inverse ratio of the accumulator resolution to the image resolution.");
467 obj.info()->addParam(obj, "minScale", obj.minScale, false, 0, 0,
468 "Minimal scale to detect.");
469 obj.info()->addParam(obj, "maxScale", obj.maxScale, false, 0, 0,
470 "Maximal scale to detect.");
471 obj.info()->addParam(obj, "scaleStep", obj.scaleStep, false, 0, 0,
474 GHT_Ballard_PosScale::GHT_Ballard_PosScale()
481 class GHT_Ballard_PosScale::Worker : public ParallelLoopBody
484 explicit Worker(GHT_Ballard_PosScale* base_) : base(base_) {}
486 void operator ()(const Range& range) const;
489 GHT_Ballard_PosScale* base;
492 void GHT_Ballard_PosScale::Worker::operator ()(const Range& range) const
494 const double thetaScale = base->levels / 360.0;
495 const double idp = 1.0 / base->dp;
497 for (int s = range.start; s < range.end; ++s)
499 const double scale = base->minScale + s * base->scaleStep;
501 Mat curHist(base->hist.size[1], base->hist.size[2], CV_32SC1, base->hist.ptr(s + 1), base->hist.step[1]);
503 for (int y = 0; y < base->imageSize.height; ++y)
505 const uchar* edgesRow = base->imageEdges.ptr(y);
506 const float* dxRow = base->imageDx.ptr<float>(y);
507 const float* dyRow = base->imageDy.ptr<float>(y);
509 for (int x = 0; x < base->imageSize.width; ++x)
511 const Point2d p(x, y);
513 if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
515 const float theta = fastAtan2(dyRow[x], dxRow[x]);
516 const int n = cvRound(theta * thetaScale);
518 const vector<Point>& r_row = base->r_table[n];
520 for (size_t j = 0; j < r_row.size(); ++j)
522 Point2d d = r_row[j];
523 Point2d c = p - d * scale;
528 if (c.x >= 0 && c.x < base->hist.size[2] - 2 && c.y >= 0 && c.y < base->hist.size[1] - 2)
529 ++curHist.at<int>(cvRound(c.y + 1), cvRound(c.x + 1));
537 void GHT_Ballard_PosScale::calcHist()
539 CV_Assert(imageEdges.type() == CV_8UC1);
540 CV_Assert(imageDx.type() == CV_32FC1 && imageDx.size() == imageSize);
541 CV_Assert(imageDy.type() == imageDx.type() && imageDy.size() == imageSize);
542 CV_Assert(levels > 0 && r_table.size() == static_cast<size_t>(levels + 1));
544 CV_Assert(minScale > 0.0 && minScale < maxScale);
545 CV_Assert(scaleStep > 0.0);
547 const double idp = 1.0 / dp;
548 const int scaleRange = cvCeil((maxScale - minScale) / scaleStep);
550 const int sizes[] = {scaleRange + 2, cvCeil(imageSize.height * idp) + 2, cvCeil(imageSize.width * idp) + 2};
551 hist.create(3, sizes, CV_32SC1);
554 parallel_for_(Range(0, scaleRange), Worker(this));
557 void GHT_Ballard_PosScale::findPosInHist()
559 CV_Assert(votesThreshold > 0);
561 const int scaleRange = hist.size[0] - 2;
562 const int histRows = hist.size[1] - 2;
563 const int histCols = hist.size[2] - 2;
565 for (int s = 0; s < scaleRange; ++s)
567 const float scale = static_cast<float>(minScale + s * scaleStep);
569 const Mat prevHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(s), hist.step[1]);
570 const Mat curHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(s + 1), hist.step[1]);
571 const Mat nextHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(s + 2), hist.step[1]);
573 for(int y = 0; y < histRows; ++y)
575 const int* prevHistRow = prevHist.ptr<int>(y + 1);
576 const int* prevRow = curHist.ptr<int>(y);
577 const int* curRow = curHist.ptr<int>(y + 1);
578 const int* nextRow = curHist.ptr<int>(y + 2);
579 const int* nextHistRow = nextHist.ptr<int>(y + 1);
581 for(int x = 0; x < histCols; ++x)
583 const int votes = curRow[x + 1];
585 if (votes > votesThreshold &&
587 votes >= curRow[x + 2] &&
588 votes > prevRow[x + 1] &&
589 votes >= nextRow[x + 1] &&
590 votes > prevHistRow[x + 1] &&
591 votes >= nextHistRow[x + 1])
593 posOutBuf.push_back(Vec4f(static_cast<float>(x * dp), static_cast<float>(y * dp), scale, 0.0f));
594 voteOutBuf.push_back(Vec3i(votes, votes, 0));
601 /////////////////////////////////////
602 // POSITION & ROTATION
604 class GHT_Ballard_PosRotation : public GHT_Ballard_Pos
607 AlgorithmInfo* info() const;
609 GHT_Ballard_PosRotation();
613 void findPosInHist();
623 CV_INIT_ALGORITHM(GHT_Ballard_PosRotation, "GeneralizedHough.POSITION_ROTATION",
624 obj.info()->addParam(obj, "minDist", obj.minDist, false, 0, 0,
625 "Minimum distance between the centers of the detected objects.");
626 obj.info()->addParam(obj, "levels", obj.levels, false, 0, 0,
628 obj.info()->addParam(obj, "votesThreshold", obj.votesThreshold, false, 0, 0,
629 "The accumulator threshold for the template centers at the detection stage. The smaller it is, the more false positions may be detected.");
630 obj.info()->addParam(obj, "dp", obj.dp, false, 0, 0,
631 "Inverse ratio of the accumulator resolution to the image resolution.");
632 obj.info()->addParam(obj, "minAngle", obj.minAngle, false, 0, 0,
633 "Minimal rotation angle to detect in degrees.");
634 obj.info()->addParam(obj, "maxAngle", obj.maxAngle, false, 0, 0,
635 "Maximal rotation angle to detect in degrees.");
636 obj.info()->addParam(obj, "angleStep", obj.angleStep, false, 0, 0,
637 "Angle step in degrees."));
639 GHT_Ballard_PosRotation::GHT_Ballard_PosRotation()
646 class GHT_Ballard_PosRotation::Worker : public ParallelLoopBody
649 explicit Worker(GHT_Ballard_PosRotation* base_) : base(base_) {}
651 void operator ()(const Range& range) const;
654 GHT_Ballard_PosRotation* base;
657 void GHT_Ballard_PosRotation::Worker::operator ()(const Range& range) const
659 const double thetaScale = base->levels / 360.0;
660 const double idp = 1.0 / base->dp;
662 for (int a = range.start; a < range.end; ++a)
664 const double angle = base->minAngle + a * base->angleStep;
666 const double sinA = ::sin(toRad(angle));
667 const double cosA = ::cos(toRad(angle));
669 Mat curHist(base->hist.size[1], base->hist.size[2], CV_32SC1, base->hist.ptr(a + 1), base->hist.step[1]);
671 for (int y = 0; y < base->imageSize.height; ++y)
673 const uchar* edgesRow = base->imageEdges.ptr(y);
674 const float* dxRow = base->imageDx.ptr<float>(y);
675 const float* dyRow = base->imageDy.ptr<float>(y);
677 for (int x = 0; x < base->imageSize.width; ++x)
679 const Point2d p(x, y);
681 if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
683 double theta = fastAtan2(dyRow[x], dxRow[x]) - angle;
686 const int n = cvRound(theta * thetaScale);
688 const vector<Point>& r_row = base->r_table[n];
690 for (size_t j = 0; j < r_row.size(); ++j)
692 Point2d d = r_row[j];
693 Point2d c = p - Point2d(d.x * cosA - d.y * sinA, d.x * sinA + d.y * cosA);
698 if (c.x >= 0 && c.x < base->hist.size[2] - 2 && c.y >= 0 && c.y < base->hist.size[1] - 2)
699 ++curHist.at<int>(cvRound(c.y + 1), cvRound(c.x + 1));
707 void GHT_Ballard_PosRotation::calcHist()
709 CV_Assert(imageEdges.type() == CV_8UC1);
710 CV_Assert(imageDx.type() == CV_32FC1 && imageDx.size() == imageSize);
711 CV_Assert(imageDy.type() == imageDx.type() && imageDy.size() == imageSize);
712 CV_Assert(levels > 0 && r_table.size() == static_cast<size_t>(levels + 1));
714 CV_Assert(minAngle >= 0.0 && minAngle < maxAngle && maxAngle <= 360.0);
715 CV_Assert(angleStep > 0.0 && angleStep < 360.0);
717 const double idp = 1.0 / dp;
718 const int angleRange = cvCeil((maxAngle - minAngle) / angleStep);
720 const int sizes[] = {angleRange + 2, cvCeil(imageSize.height * idp) + 2, cvCeil(imageSize.width * idp) + 2};
721 hist.create(3, sizes, CV_32SC1);
724 parallel_for_(Range(0, angleRange), Worker(this));
727 void GHT_Ballard_PosRotation::findPosInHist()
729 CV_Assert(votesThreshold > 0);
731 const int angleRange = hist.size[0] - 2;
732 const int histRows = hist.size[1] - 2;
733 const int histCols = hist.size[2] - 2;
735 for (int a = 0; a < angleRange; ++a)
737 const float angle = static_cast<float>(minAngle + a * angleStep);
739 const Mat prevHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(a), hist.step[1]);
740 const Mat curHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(a + 1), hist.step[1]);
741 const Mat nextHist(histRows + 2, histCols + 2, CV_32SC1, hist.ptr(a + 2), hist.step[1]);
743 for(int y = 0; y < histRows; ++y)
745 const int* prevHistRow = prevHist.ptr<int>(y + 1);
746 const int* prevRow = curHist.ptr<int>(y);
747 const int* curRow = curHist.ptr<int>(y + 1);
748 const int* nextRow = curHist.ptr<int>(y + 2);
749 const int* nextHistRow = nextHist.ptr<int>(y + 1);
751 for(int x = 0; x < histCols; ++x)
753 const int votes = curRow[x + 1];
755 if (votes > votesThreshold &&
757 votes >= curRow[x + 2] &&
758 votes > prevRow[x + 1] &&
759 votes >= nextRow[x + 1] &&
760 votes > prevHistRow[x + 1] &&
761 votes >= nextHistRow[x + 1])
763 posOutBuf.push_back(Vec4f(static_cast<float>(x * dp), static_cast<float>(y * dp), 1.0f, angle));
764 voteOutBuf.push_back(Vec3i(votes, 0, votes));
771 /////////////////////////////////////////
772 // POSITION & SCALE & ROTATION
774 double clampAngle(double a)
786 bool angleEq(double a, double b, double eps = 1.0)
788 return (fabs(clampAngle(a - b)) <= eps);
791 class GHT_Guil_Full : public GHT_Pos
794 AlgorithmInfo* info() const;
822 void buildFeatureList(const Mat& edges, const Mat& dx, const Mat& dy, vector< vector<Feature> >& features, Point2d center = Point2d());
823 void getContourPoints(const Mat& edges, const Mat& dx, const Mat& dy, vector<ContourPoint>& points);
825 void calcOrientation();
826 void calcScale(double angle);
827 void calcPosition(double angle, int angleVotes, double scale, int scaleVotes);
847 vector< vector<Feature> > templFeatures;
848 vector< vector<Feature> > imageFeatures;
850 vector< pair<double, int> > angles;
851 vector< pair<double, int> > scales;
854 CV_INIT_ALGORITHM(GHT_Guil_Full, "GeneralizedHough.POSITION_SCALE_ROTATION",
855 obj.info()->addParam(obj, "minDist", obj.minDist, false, 0, 0,
856 "Minimum distance between the centers of the detected objects.");
857 obj.info()->addParam(obj, "maxSize", obj.maxSize, false, 0, 0,
858 "Maximal size of inner buffers.");
859 obj.info()->addParam(obj, "xi", obj.xi, false, 0, 0,
860 "Angle difference in degrees between two points in feature.");
861 obj.info()->addParam(obj, "levels", obj.levels, false, 0, 0,
862 "Feature table levels.");
863 obj.info()->addParam(obj, "angleEpsilon", obj.angleEpsilon, false, 0, 0,
864 "Maximal difference between angles that treated as equal.");
865 obj.info()->addParam(obj, "minAngle", obj.minAngle, false, 0, 0,
866 "Minimal rotation angle to detect in degrees.");
867 obj.info()->addParam(obj, "maxAngle", obj.maxAngle, false, 0, 0,
868 "Maximal rotation angle to detect in degrees.");
869 obj.info()->addParam(obj, "angleStep", obj.angleStep, false, 0, 0,
870 "Angle step in degrees.");
871 obj.info()->addParam(obj, "angleThresh", obj.angleThresh, false, 0, 0,
873 obj.info()->addParam(obj, "minScale", obj.minScale, false, 0, 0,
874 "Minimal scale to detect.");
875 obj.info()->addParam(obj, "maxScale", obj.maxScale, false, 0, 0,
876 "Maximal scale to detect.");
877 obj.info()->addParam(obj, "scaleStep", obj.scaleStep, false, 0, 0,
879 obj.info()->addParam(obj, "scaleThresh", obj.scaleThresh, false, 0, 0,
881 obj.info()->addParam(obj, "dp", obj.dp, false, 0, 0,
882 "Inverse ratio of the accumulator resolution to the image resolution.");
883 obj.info()->addParam(obj, "posThresh", obj.posThresh, false, 0, 0,
884 "Position threshold."));
886 GHT_Guil_Full::GHT_Guil_Full()
907 void GHT_Guil_Full::releaseImpl()
909 GHT_Pos::releaseImpl();
911 releaseVector(templFeatures);
912 releaseVector(imageFeatures);
914 releaseVector(angles);
915 releaseVector(scales);
918 void GHT_Guil_Full::processTempl()
920 buildFeatureList(templEdges, templDx, templDy, templFeatures, templCenter);
923 void GHT_Guil_Full::processImage()
925 buildFeatureList(imageEdges, imageDx, imageDy, imageFeatures);
929 for (size_t i = 0; i < angles.size(); ++i)
931 const double angle = angles[i].first;
932 const int angleVotes = angles[i].second;
936 for (size_t j = 0; j < scales.size(); ++j)
938 const double scale = scales[j].first;
939 const int scaleVotes = scales[j].second;
941 calcPosition(angle, angleVotes, scale, scaleVotes);
946 void GHT_Guil_Full::buildFeatureList(const Mat& edges, const Mat& dx, const Mat& dy, vector< vector<Feature> >& features, Point2d center)
948 CV_Assert(levels > 0);
950 const double maxDist = sqrt((double) templSize.width * templSize.width + templSize.height * templSize.height) * maxScale;
952 const double alphaScale = levels / 360.0;
954 vector<ContourPoint> points;
955 getContourPoints(edges, dx, dy, points);
957 features.resize(levels + 1);
958 for_each(features.begin(), features.end(), mem_fun_ref(&vector<Feature>::clear));
959 for_each(features.begin(), features.end(), bind2nd(mem_fun_ref(&vector<Feature>::reserve), maxSize));
961 for (size_t i = 0; i < points.size(); ++i)
963 ContourPoint p1 = points[i];
965 for (size_t j = 0; j < points.size(); ++j)
967 ContourPoint p2 = points[j];
969 if (angleEq(p1.theta - p2.theta, xi, angleEpsilon))
971 const Point2d d = p1.pos - p2.pos;
978 f.alpha12 = clampAngle(fastAtan2(d.y, d.x) - p1.theta);
984 f.r1 = p1.pos - center;
985 f.r2 = p2.pos - center;
987 const int n = cvRound(f.alpha12 * alphaScale);
989 if (features[n].size() < static_cast<size_t>(maxSize))
990 features[n].push_back(f);
996 void GHT_Guil_Full::getContourPoints(const Mat& edges, const Mat& dx, const Mat& dy, vector<ContourPoint>& points)
998 CV_Assert(edges.type() == CV_8UC1);
999 CV_Assert(dx.type() == CV_32FC1 && dx.size == edges.size);
1000 CV_Assert(dy.type() == dx.type() && dy.size == edges.size);
1003 points.reserve(edges.size().area());
1005 for (int y = 0; y < edges.rows; ++y)
1007 const uchar* edgesRow = edges.ptr(y);
1008 const float* dxRow = dx.ptr<float>(y);
1009 const float* dyRow = dy.ptr<float>(y);
1011 for (int x = 0; x < edges.cols; ++x)
1013 if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
1017 p.pos = Point2d(x, y);
1018 p.theta = fastAtan2(dyRow[x], dxRow[x]);
1020 points.push_back(p);
1026 void GHT_Guil_Full::calcOrientation()
1028 CV_Assert(levels > 0);
1029 CV_Assert(templFeatures.size() == static_cast<size_t>(levels + 1));
1030 CV_Assert(imageFeatures.size() == templFeatures.size());
1031 CV_Assert(minAngle >= 0.0 && minAngle < maxAngle && maxAngle <= 360.0);
1032 CV_Assert(angleStep > 0.0 && angleStep < 360.0);
1033 CV_Assert(angleThresh > 0);
1035 const double iAngleStep = 1.0 / angleStep;
1036 const int angleRange = cvCeil((maxAngle - minAngle) * iAngleStep);
1038 vector<int> OHist(angleRange + 1, 0);
1039 for (int i = 0; i <= levels; ++i)
1041 const vector<Feature>& templRow = templFeatures[i];
1042 const vector<Feature>& imageRow = imageFeatures[i];
1044 for (size_t j = 0; j < templRow.size(); ++j)
1046 Feature templF = templRow[j];
1048 for (size_t k = 0; k < imageRow.size(); ++k)
1050 Feature imF = imageRow[k];
1052 const double angle = clampAngle(imF.p1.theta - templF.p1.theta);
1053 if (angle >= minAngle && angle <= maxAngle)
1055 const int n = cvRound((angle - minAngle) * iAngleStep);
1064 for (int n = 0; n < angleRange; ++n)
1066 if (OHist[n] >= angleThresh)
1068 const double angle = minAngle + n * angleStep;
1069 angles.push_back(make_pair(angle, OHist[n]));
1074 void GHT_Guil_Full::calcScale(double angle)
1076 CV_Assert(levels > 0);
1077 CV_Assert(templFeatures.size() == static_cast<size_t>(levels + 1));
1078 CV_Assert(imageFeatures.size() == templFeatures.size());
1079 CV_Assert(minScale > 0.0 && minScale < maxScale);
1080 CV_Assert(scaleStep > 0.0);
1081 CV_Assert(scaleThresh > 0);
1083 const double iScaleStep = 1.0 / scaleStep;
1084 const int scaleRange = cvCeil((maxScale - minScale) * iScaleStep);
1086 vector<int> SHist(scaleRange + 1, 0);
1088 for (int i = 0; i <= levels; ++i)
1090 const vector<Feature>& templRow = templFeatures[i];
1091 const vector<Feature>& imageRow = imageFeatures[i];
1093 for (size_t j = 0; j < templRow.size(); ++j)
1095 Feature templF = templRow[j];
1097 templF.p1.theta += angle;
1099 for (size_t k = 0; k < imageRow.size(); ++k)
1101 Feature imF = imageRow[k];
1103 if (angleEq(imF.p1.theta, templF.p1.theta, angleEpsilon))
1105 const double scale = imF.d12 / templF.d12;
1106 if (scale >= minScale && scale <= maxScale)
1108 const int s = cvRound((scale - minScale) * iScaleStep);
1118 for (int s = 0; s < scaleRange; ++s)
1120 if (SHist[s] >= scaleThresh)
1122 const double scale = minScale + s * scaleStep;
1123 scales.push_back(make_pair(scale, SHist[s]));
1128 void GHT_Guil_Full::calcPosition(double angle, int angleVotes, double scale, int scaleVotes)
1130 CV_Assert(levels > 0);
1131 CV_Assert(templFeatures.size() == static_cast<size_t>(levels + 1));
1132 CV_Assert(imageFeatures.size() == templFeatures.size());
1133 CV_Assert(dp > 0.0);
1134 CV_Assert(posThresh > 0);
1136 const double sinVal = sin(toRad(angle));
1137 const double cosVal = cos(toRad(angle));
1138 const double idp = 1.0 / dp;
1140 const int histRows = cvCeil(imageSize.height * idp);
1141 const int histCols = cvCeil(imageSize.width * idp);
1143 Mat DHist(histRows + 2, histCols + 2, CV_32SC1, Scalar::all(0));
1145 for (int i = 0; i <= levels; ++i)
1147 const vector<Feature>& templRow = templFeatures[i];
1148 const vector<Feature>& imageRow = imageFeatures[i];
1150 for (size_t j = 0; j < templRow.size(); ++j)
1152 Feature templF = templRow[j];
1154 templF.p1.theta += angle;
1159 templF.r1 = Point2d(cosVal * templF.r1.x - sinVal * templF.r1.y, sinVal * templF.r1.x + cosVal * templF.r1.y);
1160 templF.r2 = Point2d(cosVal * templF.r2.x - sinVal * templF.r2.y, sinVal * templF.r2.x + cosVal * templF.r2.y);
1162 for (size_t k = 0; k < imageRow.size(); ++k)
1164 Feature imF = imageRow[k];
1166 if (angleEq(imF.p1.theta, templF.p1.theta, angleEpsilon))
1170 c1 = imF.p1.pos - templF.r1;
1173 c2 = imF.p2.pos - templF.r2;
1176 if (fabs(c1.x - c2.x) > 1 || fabs(c1.y - c2.y) > 1)
1179 if (c1.y >= 0 && c1.y < histRows && c1.x >= 0 && c1.x < histCols)
1180 ++DHist.at<int>(cvRound(c1.y) + 1, cvRound(c1.x) + 1);
1186 for(int y = 0; y < histRows; ++y)
1188 const int* prevRow = DHist.ptr<int>(y);
1189 const int* curRow = DHist.ptr<int>(y + 1);
1190 const int* nextRow = DHist.ptr<int>(y + 2);
1192 for(int x = 0; x < histCols; ++x)
1194 const int votes = curRow[x + 1];
1196 if (votes > posThresh && votes > curRow[x] && votes >= curRow[x + 2] && votes > prevRow[x + 1] && votes >= nextRow[x + 1])
1198 posOutBuf.push_back(Vec4f(static_cast<float>(x * dp), static_cast<float>(y * dp), static_cast<float>(scale), static_cast<float>(angle)));
1199 voteOutBuf.push_back(Vec3i(votes, scaleVotes, angleVotes));
1206 Ptr<GeneralizedHough> cv::GeneralizedHough::create(int method)
1211 CV_Assert( !GHT_Ballard_Pos_info_auto.name().empty() );
1212 return new GHT_Ballard_Pos();
1214 case (GHT_POSITION | GHT_SCALE):
1215 CV_Assert( !GHT_Ballard_PosScale_info_auto.name().empty() );
1216 return new GHT_Ballard_PosScale();
1218 case (GHT_POSITION | GHT_ROTATION):
1219 CV_Assert( !GHT_Ballard_PosRotation_info_auto.name().empty() );
1220 return new GHT_Ballard_PosRotation();
1222 case (GHT_POSITION | GHT_SCALE | GHT_ROTATION):
1223 CV_Assert( !GHT_Guil_Full_info_auto.name().empty() );
1224 return new GHT_Guil_Full();
1227 CV_Error(CV_StsBadArg, "Unsupported method");
1228 return Ptr<GeneralizedHough>();
1231 cv::GeneralizedHough::~GeneralizedHough()
1235 void cv::GeneralizedHough::setTemplate(InputArray _templ, int cannyThreshold, Point templCenter)
1237 Mat templ = _templ.getMat();
1239 CV_Assert(templ.type() == CV_8UC1);
1240 CV_Assert(cannyThreshold > 0);
1242 Canny(templ, edges_, cannyThreshold / 2, cannyThreshold);
1243 Sobel(templ, dx_, CV_32F, 1, 0);
1244 Sobel(templ, dy_, CV_32F, 0, 1);
1246 if (templCenter == Point(-1, -1))
1247 templCenter = Point(templ.cols / 2, templ.rows / 2);
1249 setTemplateImpl(edges_, dx_, dy_, templCenter);
1252 void cv::GeneralizedHough::setTemplate(InputArray _edges, InputArray _dx, InputArray _dy, Point templCenter)
1254 Mat edges = _edges.getMat();
1255 Mat dx = _dx.getMat();
1256 Mat dy = _dy.getMat();
1258 if (templCenter == Point(-1, -1))
1259 templCenter = Point(edges.cols / 2, edges.rows / 2);
1261 setTemplateImpl(edges, dx, dy, templCenter);
1264 void cv::GeneralizedHough::detect(InputArray _image, OutputArray positions, OutputArray votes, int cannyThreshold)
1266 Mat image = _image.getMat();
1268 CV_Assert(image.type() == CV_8UC1);
1269 CV_Assert(cannyThreshold > 0);
1271 Canny(image, edges_, cannyThreshold / 2, cannyThreshold);
1272 Sobel(image, dx_, CV_32F, 1, 0);
1273 Sobel(image, dy_, CV_32F, 0, 1);
1275 detectImpl(edges_, dx_, dy_, positions, votes);
1278 void cv::GeneralizedHough::detect(InputArray _edges, InputArray _dx, InputArray _dy, OutputArray positions, OutputArray votes)
1280 cv::Mat edges = _edges.getMat();
1281 cv::Mat dx = _dx.getMat();
1282 cv::Mat dy = _dy.getMat();
1284 detectImpl(edges, dx, dy, positions, votes);
1287 void cv::GeneralizedHough::release()