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"
52
double toRad(double a)
54
return a * CV_PI / 180.0;
59
return fabs(v) > std::numeric_limits<float>::epsilon();
62
class GeneralizedHoughBase
65
GeneralizedHoughBase();
66
virtual ~GeneralizedHoughBase() {}
68
void setTemplateImpl(InputArray templ, Point templCenter);
69
void setTemplateImpl(InputArray edges, InputArray dx, InputArray dy, Point templCenter);
71
void detectImpl(InputArray image, OutputArray positions, OutputArray votes);
72
void detectImpl(InputArray edges, InputArray dx, InputArray dy, OutputArray positions, OutputArray votes);
74
virtual void processTempl() = 0;
75
virtual void processImage() = 0;
93
std::vector<Vec4f> posOutBuf_;
94
std::vector<Vec3i> voteOutBuf_;
97
void calcEdges(InputArray src, Mat& edges, Mat& dx, Mat& dy);
99
void convertTo(OutputArray positions, OutputArray votes);
102
GeneralizedHoughBase::GeneralizedHoughBase()
104
cannyLowThresh_ = 50;
105
cannyHighThresh_ = 100;
110
void GeneralizedHoughBase::calcEdges(InputArray _src, Mat& edges, Mat& dx, Mat& dy)
112
Mat src = _src.getMat();
114
CV_Assert( src.type() == CV_8UC1 );
115
CV_Assert( cannyLowThresh_ > 0 && cannyLowThresh_ < cannyHighThresh_ );
117
Canny(src, edges, cannyLowThresh_, cannyHighThresh_);
118
Sobel(src, dx, CV_32F, 1, 0);
119
Sobel(src, dy, CV_32F, 0, 1);
122
void GeneralizedHoughBase::setTemplateImpl(InputArray templ, Point templCenter)
124
calcEdges(templ, templEdges_, templDx_, templDy_);
126
if (templCenter == Point(-1, -1))
127
templCenter = Point(templEdges_.cols / 2, templEdges_.rows / 2);
129
templSize_ = templEdges_.size();
130
templCenter_ = templCenter;
135
void GeneralizedHoughBase::setTemplateImpl(InputArray edges, InputArray dx, InputArray dy, Point templCenter)
137
edges.getMat().copyTo(templEdges_);
138
dx.getMat().copyTo(templDx_);
139
dy.getMat().copyTo(templDy_);
141
CV_Assert( templEdges_.type() == CV_8UC1 );
142
CV_Assert( templDx_.type() == CV_32FC1 && templDx_.size() == templEdges_.size() );
143
CV_Assert( templDy_.type() == templDx_.type() && templDy_.size() == templEdges_.size() );
145
if (templCenter == Point(-1, -1))
146
templCenter = Point(templEdges_.cols / 2, templEdges_.rows / 2);
148
templSize_ = templEdges_.size();
149
templCenter_ = templCenter;
154
void GeneralizedHoughBase::detectImpl(InputArray image, OutputArray positions, OutputArray votes)
156
calcEdges(image, imageEdges_, imageDx_, imageDy_);
158
imageSize_ = imageEdges_.size();
165
if (!posOutBuf_.empty())
169
convertTo(positions, votes);
179
void GeneralizedHoughBase::detectImpl(InputArray edges, InputArray dx, InputArray dy, OutputArray positions, OutputArray votes)
181
edges.getMat().copyTo(imageEdges_);
182
dx.getMat().copyTo(imageDx_);
183
dy.getMat().copyTo(imageDy_);
185
CV_Assert( imageEdges_.type() == CV_8UC1 );
186
CV_Assert( imageDx_.type() == CV_32FC1 && imageDx_.size() == imageEdges_.size() );
187
CV_Assert( imageDy_.type() == imageDx_.type() && imageDy_.size() == imageEdges_.size() );
189
imageSize_ = imageEdges_.size();
196
if (!posOutBuf_.empty())
200
convertTo(positions, votes);
210
class Vec3iGreaterThanIdx
213
Vec3iGreaterThanIdx( const Vec3i* _arr ) : arr(_arr) {}
214
bool operator()(size_t a, size_t b) const { return arr[a][0] > arr[b][0]; }
218
void GeneralizedHoughBase::filterMinDist()
220
size_t oldSize = posOutBuf_.size();
221
const bool hasVotes = !voteOutBuf_.empty();
223
CV_Assert( !hasVotes || voteOutBuf_.size() == oldSize );
225
std::vector<Vec4f> oldPosBuf(posOutBuf_);
226
std::vector<Vec3i> oldVoteBuf(voteOutBuf_);
228
std::vector<size_t> indexies(oldSize);
229
for (size_t i = 0; i < oldSize; ++i)
231
std::sort(indexies.begin(), indexies.end(), Vec3iGreaterThanIdx(&oldVoteBuf[0]));
236
const int cellSize = cvRound(minDist_);
237
const int gridWidth = (imageSize_.width + cellSize - 1) / cellSize;
238
const int gridHeight = (imageSize_.height + cellSize - 1) / cellSize;
240
std::vector< std::vector<Point2f> > grid(gridWidth * gridHeight);
242
const double minDist2 = minDist_ * minDist_;
244
for (size_t i = 0; i < oldSize; ++i)
246
const size_t ind = indexies[i];
248
Point2f p(oldPosBuf[ind][0], oldPosBuf[ind][1]);
252
const int xCell = static_cast<int>(p.x / cellSize);
253
const int yCell = static_cast<int>(p.y / cellSize);
261
x1 = std::max(0, x1);
262
y1 = std::max(0, y1);
263
x2 = std::min(gridWidth - 1, x2);
264
y2 = std::min(gridHeight - 1, y2);
266
for (int yy = y1; yy <= y2; ++yy)
268
for (int xx = x1; xx <= x2; ++xx)
270
const std::vector<Point2f>& m = grid[yy * gridWidth + xx];
272
for(size_t j = 0; j < m.size(); ++j)
274
const Point2f d = p - m[j];
276
if (d.ddot(d) < minDist2)
289
grid[yCell * gridWidth + xCell].push_back(p);
291
posOutBuf_.push_back(oldPosBuf[ind]);
293
voteOutBuf_.push_back(oldVoteBuf[ind]);
298
void GeneralizedHoughBase::convertTo(OutputArray _positions, OutputArray _votes)
300
const int total = static_cast<int>(posOutBuf_.size());
301
const bool hasVotes = !voteOutBuf_.empty();
303
CV_Assert( !hasVotes || voteOutBuf_.size() == posOutBuf_.size() );
305
_positions.create(1, total, CV_32FC4);
306
Mat positions = _positions.getMat();
307
Mat(1, total, CV_32FC4, &posOutBuf_[0]).copyTo(positions);
317
_votes.create(1, total, CV_32SC3);
318
Mat votes = _votes.getMat();
319
Mat(1, total, CV_32SC3, &voteOutBuf_[0]).copyTo(votes);
325
// GeneralizedHoughBallard
329
class GeneralizedHoughBallardImpl : public GeneralizedHoughBallard, private GeneralizedHoughBase
332
GeneralizedHoughBallardImpl();
334
void setTemplate(InputArray templ, Point templCenter) { setTemplateImpl(templ, templCenter); }
335
void setTemplate(InputArray edges, InputArray dx, InputArray dy, Point templCenter) { setTemplateImpl(edges, dx, dy, templCenter); }
337
void detect(InputArray image, OutputArray positions, OutputArray votes) { detectImpl(image, positions, votes); }
338
void detect(InputArray edges, InputArray dx, InputArray dy, OutputArray positions, OutputArray votes) { detectImpl(edges, dx, dy, positions, votes); }
340
void setCannyLowThresh(int cannyLowThresh) { cannyLowThresh_ = cannyLowThresh; }
341
int getCannyLowThresh() const { return cannyLowThresh_; }
343
void setCannyHighThresh(int cannyHighThresh) { cannyHighThresh_ = cannyHighThresh; }
344
int getCannyHighThresh() const { return cannyHighThresh_; }
346
void setMinDist(double minDist) { minDist_ = minDist; }
347
double getMinDist() const { return minDist_; }
349
void setDp(double dp) { dp_ = dp; }
350
double getDp() const { return dp_; }
352
void setMaxBufferSize(int) { }
353
int getMaxBufferSize() const { return 0; }
355
void setLevels(int levels) { levels_ = levels; }
356
int getLevels() const { return levels_; }
358
void setVotesThreshold(int votesThreshold) { votesThreshold_ = votesThreshold; }
359
int getVotesThreshold() const { return votesThreshold_; }
366
void findPosInHist();
371
std::vector< std::vector<Point> > r_table_;
375
GeneralizedHoughBallardImpl::GeneralizedHoughBallardImpl()
378
votesThreshold_ = 100;
381
void GeneralizedHoughBallardImpl::processTempl()
383
CV_Assert( levels_ > 0 );
385
const double thetaScale = levels_ / 360.0;
387
r_table_.resize(levels_ + 1);
388
std::for_each(r_table_.begin(), r_table_.end(), std::mem_fun_ref(&std::vector<Point>::clear));
390
for (int y = 0; y < templSize_.height; ++y)
392
const uchar* edgesRow = templEdges_.ptr(y);
393
const float* dxRow = templDx_.ptr<float>(y);
394
const float* dyRow = templDy_.ptr<float>(y);
396
for (int x = 0; x < templSize_.width; ++x)
400
if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
402
const float theta = fastAtan2(dyRow[x], dxRow[x]);
403
const int n = cvRound(theta * thetaScale);
404
r_table_[n].push_back(p - templCenter_);
410
void GeneralizedHoughBallardImpl::processImage()
416
void GeneralizedHoughBallardImpl::calcHist()
418
CV_Assert( imageEdges_.type() == CV_8UC1 );
419
CV_Assert( imageDx_.type() == CV_32FC1 && imageDx_.size() == imageSize_);
420
CV_Assert( imageDy_.type() == imageDx_.type() && imageDy_.size() == imageSize_);
421
CV_Assert( levels_ > 0 && r_table_.size() == static_cast<size_t>(levels_ + 1) );
422
CV_Assert( dp_ > 0.0 );
424
const double thetaScale = levels_ / 360.0;
425
const double idp = 1.0 / dp_;
427
hist_.create(cvCeil(imageSize_.height * idp) + 2, cvCeil(imageSize_.width * idp) + 2, CV_32SC1);
430
const int rows = hist_.rows - 2;
431
const int cols = hist_.cols - 2;
433
for (int y = 0; y < imageSize_.height; ++y)
435
const uchar* edgesRow = imageEdges_.ptr(y);
436
const float* dxRow = imageDx_.ptr<float>(y);
437
const float* dyRow = imageDy_.ptr<float>(y);
439
for (int x = 0; x < imageSize_.width; ++x)
443
if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
445
const float theta = fastAtan2(dyRow[x], dxRow[x]);
446
const int n = cvRound(theta * thetaScale);
448
const std::vector<Point>& r_row = r_table_[n];
450
for (size_t j = 0; j < r_row.size(); ++j)
452
Point c = p - r_row[j];
454
c.x = cvRound(c.x * idp);
455
c.y = cvRound(c.y * idp);
457
if (c.x >= 0 && c.x < cols && c.y >= 0 && c.y < rows)
458
++hist_.at<int>(c.y + 1, c.x + 1);
465
void GeneralizedHoughBallardImpl::findPosInHist()
467
CV_Assert( votesThreshold_ > 0 );
469
const int histRows = hist_.rows - 2;
470
const int histCols = hist_.cols - 2;
472
for(int y = 0; y < histRows; ++y)
474
const int* prevRow = hist_.ptr<int>(y);
475
const int* curRow = hist_.ptr<int>(y + 1);
476
const int* nextRow = hist_.ptr<int>(y + 2);
478
for(int x = 0; x < histCols; ++x)
480
const int votes = curRow[x + 1];
482
if (votes > votesThreshold_ && votes > curRow[x] && votes >= curRow[x + 2] && votes > prevRow[x + 1] && votes >= nextRow[x + 1])
484
posOutBuf_.push_back(Vec4f(static_cast<float>(x * dp_), static_cast<float>(y * dp_), 1.0f, 0.0f));
485
voteOutBuf_.push_back(Vec3i(votes, 0, 0));
492
Ptr<GeneralizedHoughBallard> cv::createGeneralizedHoughBallard()
494
return makePtr<GeneralizedHoughBallardImpl>();
497
// GeneralizedHoughGuil
501
class GeneralizedHoughGuilImpl : public GeneralizedHoughGuil, private GeneralizedHoughBase
504
GeneralizedHoughGuilImpl();
506
void setTemplate(InputArray templ, Point templCenter) { setTemplateImpl(templ, templCenter); }
507
void setTemplate(InputArray edges, InputArray dx, InputArray dy, Point templCenter) { setTemplateImpl(edges, dx, dy, templCenter); }
509
void detect(InputArray image, OutputArray positions, OutputArray votes) { detectImpl(image, positions, votes); }
510
void detect(InputArray edges, InputArray dx, InputArray dy, OutputArray positions, OutputArray votes) { detectImpl(edges, dx, dy, positions, votes); }
512
void setCannyLowThresh(int cannyLowThresh) { cannyLowThresh_ = cannyLowThresh; }
513
int getCannyLowThresh() const { return cannyLowThresh_; }
515
void setCannyHighThresh(int cannyHighThresh) { cannyHighThresh_ = cannyHighThresh; }
516
int getCannyHighThresh() const { return cannyHighThresh_; }
518
void setMinDist(double minDist) { minDist_ = minDist; }
519
double getMinDist() const { return minDist_; }
521
void setDp(double dp) { dp_ = dp; }
522
double getDp() const { return dp_; }
524
void setMaxBufferSize(int maxBufferSize) { maxBufferSize_ = maxBufferSize; }
525
int getMaxBufferSize() const { return maxBufferSize_; }
527
void setXi(double xi) { xi_ = xi; }
528
double getXi() const { return xi_; }
530
void setLevels(int levels) { levels_ = levels; }
531
int getLevels() const { return levels_; }
533
void setAngleEpsilon(double angleEpsilon) { angleEpsilon_ = angleEpsilon; }
534
double getAngleEpsilon() const { return angleEpsilon_; }
536
void setMinAngle(double minAngle) { minAngle_ = minAngle; }
537
double getMinAngle() const { return minAngle_; }
539
void setMaxAngle(double maxAngle) { maxAngle_ = maxAngle; }
540
double getMaxAngle() const { return maxAngle_; }
542
void setAngleStep(double angleStep) { angleStep_ = angleStep; }
543
double getAngleStep() const { return angleStep_; }
545
void setAngleThresh(int angleThresh) { angleThresh_ = angleThresh; }
546
int getAngleThresh() const { return angleThresh_; }
548
void setMinScale(double minScale) { minScale_ = minScale; }
549
double getMinScale() const { return minScale_; }
551
void setMaxScale(double maxScale) { maxScale_ = maxScale; }
552
double getMaxScale() const { return maxScale_; }
554
void setScaleStep(double scaleStep) { scaleStep_ = scaleStep; }
555
double getScaleStep() const { return scaleStep_; }
557
void setScaleThresh(int scaleThresh) { scaleThresh_ = scaleThresh; }
558
int getScaleThresh() const { return scaleThresh_; }
560
void setPosThresh(int posThresh) { posThresh_ = posThresh; }
561
int getPosThresh() const { return posThresh_; }
570
double angleEpsilon_;
602
void buildFeatureList(const Mat& edges, const Mat& dx, const Mat& dy, std::vector< std::vector<Feature> >& features, Point2d center = Point2d());
603
void getContourPoints(const Mat& edges, const Mat& dx, const Mat& dy, std::vector<ContourPoint>& points);
605
void calcOrientation();
606
void calcScale(double angle);
607
void calcPosition(double angle, int angleVotes, double scale, int scaleVotes);
609
std::vector< std::vector<Feature> > templFeatures_;
610
std::vector< std::vector<Feature> > imageFeatures_;
612
std::vector< std::pair<double, int> > angles_;
613
std::vector< std::pair<double, int> > scales_;
616
double clampAngle(double a)
628
bool angleEq(double a, double b, double eps = 1.0)
630
return (fabs(clampAngle(a - b)) <= eps);
633
GeneralizedHoughGuilImpl::GeneralizedHoughGuilImpl()
635
maxBufferSize_ = 1000;
643
angleThresh_ = 15000;
653
void GeneralizedHoughGuilImpl::processTempl()
655
buildFeatureList(templEdges_, templDx_, templDy_, templFeatures_, templCenter_);
658
void GeneralizedHoughGuilImpl::processImage()
660
buildFeatureList(imageEdges_, imageDx_, imageDy_, imageFeatures_);
664
for (size_t i = 0; i < angles_.size(); ++i)
666
const double angle = angles_[i].first;
667
const int angleVotes = angles_[i].second;
671
for (size_t j = 0; j < scales_.size(); ++j)
673
const double scale = scales_[j].first;
674
const int scaleVotes = scales_[j].second;
676
calcPosition(angle, angleVotes, scale, scaleVotes);
681
void GeneralizedHoughGuilImpl::buildFeatureList(const Mat& edges, const Mat& dx, const Mat& dy, std::vector< std::vector<Feature> >& features, Point2d center)
683
CV_Assert( levels_ > 0 );
685
const double maxDist = sqrt((double) templSize_.width * templSize_.width + templSize_.height * templSize_.height) * maxScale_;
687
const double alphaScale = levels_ / 360.0;
689
std::vector<ContourPoint> points;
690
getContourPoints(edges, dx, dy, points);
692
features.resize(levels_ + 1);
693
std::for_each(features.begin(), features.end(), std::mem_fun_ref(&std::vector<Feature>::clear));
694
std::for_each(features.begin(), features.end(), std::bind2nd(std::mem_fun_ref(&std::vector<Feature>::reserve), maxBufferSize_));
696
for (size_t i = 0; i < points.size(); ++i)
698
ContourPoint p1 = points[i];
700
for (size_t j = 0; j < points.size(); ++j)
702
ContourPoint p2 = points[j];
704
if (angleEq(p1.theta - p2.theta, xi_, angleEpsilon_))
706
const Point2d d = p1.pos - p2.pos;
713
f.alpha12 = clampAngle(fastAtan2((float)d.y, (float)d.x) - p1.theta);
719
f.r1 = p1.pos - center;
720
f.r2 = p2.pos - center;
722
const int n = cvRound(f.alpha12 * alphaScale);
724
if (features[n].size() < static_cast<size_t>(maxBufferSize_))
725
features[n].push_back(f);
731
void GeneralizedHoughGuilImpl::getContourPoints(const Mat& edges, const Mat& dx, const Mat& dy, std::vector<ContourPoint>& points)
733
CV_Assert( edges.type() == CV_8UC1 );
734
CV_Assert( dx.type() == CV_32FC1 && dx.size == edges.size );
735
CV_Assert( dy.type() == dx.type() && dy.size == edges.size );
738
points.reserve(edges.size().area());
740
for (int y = 0; y < edges.rows; ++y)
742
const uchar* edgesRow = edges.ptr(y);
743
const float* dxRow = dx.ptr<float>(y);
744
const float* dyRow = dy.ptr<float>(y);
746
for (int x = 0; x < edges.cols; ++x)
748
if (edgesRow[x] && (notNull(dyRow[x]) || notNull(dxRow[x])))
752
p.pos = Point2d(x, y);
753
p.theta = fastAtan2(dyRow[x], dxRow[x]);
761
void GeneralizedHoughGuilImpl::calcOrientation()
763
CV_Assert( levels_ > 0 );
764
CV_Assert( templFeatures_.size() == static_cast<size_t>(levels_ + 1) );
765
CV_Assert( imageFeatures_.size() == templFeatures_.size() );
766
CV_Assert( minAngle_ >= 0.0 && minAngle_ < maxAngle_ && maxAngle_ <= 360.0 );
767
CV_Assert( angleStep_ > 0.0 && angleStep_ < 360.0 );
768
CV_Assert( angleThresh_ > 0 );
770
const double iAngleStep = 1.0 / angleStep_;
771
const int angleRange = cvCeil((maxAngle_ - minAngle_) * iAngleStep);
773
std::vector<int> OHist(angleRange + 1, 0);
774
for (int i = 0; i <= levels_; ++i)
776
const std::vector<Feature>& templRow = templFeatures_[i];
777
const std::vector<Feature>& imageRow = imageFeatures_[i];
779
for (size_t j = 0; j < templRow.size(); ++j)
781
Feature templF = templRow[j];
783
for (size_t k = 0; k < imageRow.size(); ++k)
785
Feature imF = imageRow[k];
787
const double angle = clampAngle(imF.p1.theta - templF.p1.theta);
788
if (angle >= minAngle_ && angle <= maxAngle_)
790
const int n = cvRound((angle - minAngle_) * iAngleStep);
799
for (int n = 0; n < angleRange; ++n)
801
if (OHist[n] >= angleThresh_)
803
const double angle = minAngle_ + n * angleStep_;
804
angles_.push_back(std::make_pair(angle, OHist[n]));
809
void GeneralizedHoughGuilImpl::calcScale(double angle)
811
CV_Assert( levels_ > 0 );
812
CV_Assert( templFeatures_.size() == static_cast<size_t>(levels_ + 1) );
813
CV_Assert( imageFeatures_.size() == templFeatures_.size() );
814
CV_Assert( minScale_ > 0.0 && minScale_ < maxScale_ );
815
CV_Assert( scaleStep_ > 0.0 );
816
CV_Assert( scaleThresh_ > 0 );
818
const double iScaleStep = 1.0 / scaleStep_;
819
const int scaleRange = cvCeil((maxScale_ - minScale_) * iScaleStep);
821
std::vector<int> SHist(scaleRange + 1, 0);
823
for (int i = 0; i <= levels_; ++i)
825
const std::vector<Feature>& templRow = templFeatures_[i];
826
const std::vector<Feature>& imageRow = imageFeatures_[i];
828
for (size_t j = 0; j < templRow.size(); ++j)
830
Feature templF = templRow[j];
832
templF.p1.theta += angle;
834
for (size_t k = 0; k < imageRow.size(); ++k)
836
Feature imF = imageRow[k];
838
if (angleEq(imF.p1.theta, templF.p1.theta, angleEpsilon_))
840
const double scale = imF.d12 / templF.d12;
841
if (scale >= minScale_ && scale <= maxScale_)
843
const int s = cvRound((scale - minScale_) * iScaleStep);
853
for (int s = 0; s < scaleRange; ++s)
855
if (SHist[s] >= scaleThresh_)
857
const double scale = minScale_ + s * scaleStep_;
858
scales_.push_back(std::make_pair(scale, SHist[s]));
863
void GeneralizedHoughGuilImpl::calcPosition(double angle, int angleVotes, double scale, int scaleVotes)
865
CV_Assert( levels_ > 0 );
866
CV_Assert( templFeatures_.size() == static_cast<size_t>(levels_ + 1) );
867
CV_Assert( imageFeatures_.size() == templFeatures_.size() );
868
CV_Assert( dp_ > 0.0 );
869
CV_Assert( posThresh_ > 0 );
871
const double sinVal = sin(toRad(angle));
872
const double cosVal = cos(toRad(angle));
873
const double idp = 1.0 / dp_;
875
const int histRows = cvCeil(imageSize_.height * idp);
876
const int histCols = cvCeil(imageSize_.width * idp);
878
Mat DHist(histRows + 2, histCols + 2, CV_32SC1, Scalar::all(0));
880
for (int i = 0; i <= levels_; ++i)
882
const std::vector<Feature>& templRow = templFeatures_[i];
883
const std::vector<Feature>& imageRow = imageFeatures_[i];
885
for (size_t j = 0; j < templRow.size(); ++j)
887
Feature templF = templRow[j];
889
templF.p1.theta += angle;
894
templF.r1 = Point2d(cosVal * templF.r1.x - sinVal * templF.r1.y, sinVal * templF.r1.x + cosVal * templF.r1.y);
895
templF.r2 = Point2d(cosVal * templF.r2.x - sinVal * templF.r2.y, sinVal * templF.r2.x + cosVal * templF.r2.y);
897
for (size_t k = 0; k < imageRow.size(); ++k)
899
Feature imF = imageRow[k];
901
if (angleEq(imF.p1.theta, templF.p1.theta, angleEpsilon_))
905
c1 = imF.p1.pos - templF.r1;
908
c2 = imF.p2.pos - templF.r2;
911
if (fabs(c1.x - c2.x) > 1 || fabs(c1.y - c2.y) > 1)
914
if (c1.y >= 0 && c1.y < histRows && c1.x >= 0 && c1.x < histCols)
915
++DHist.at<int>(cvRound(c1.y) + 1, cvRound(c1.x) + 1);
921
for(int y = 0; y < histRows; ++y)
923
const int* prevRow = DHist.ptr<int>(y);
924
const int* curRow = DHist.ptr<int>(y + 1);
925
const int* nextRow = DHist.ptr<int>(y + 2);
927
for(int x = 0; x < histCols; ++x)
929
const int votes = curRow[x + 1];
931
if (votes > posThresh_ && votes > curRow[x] && votes >= curRow[x + 2] && votes > prevRow[x + 1] && votes >= nextRow[x + 1])
933
posOutBuf_.push_back(Vec4f(static_cast<float>(x * dp_), static_cast<float>(y * dp_), static_cast<float>(scale), static_cast<float>(angle)));
934
voteOutBuf_.push_back(Vec3i(votes, scaleVotes, angleVotes));
941
Ptr<GeneralizedHoughGuil> cv::createGeneralizedHoughGuil()
943
return makePtr<GeneralizedHoughGuilImpl>();