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.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
43
#include "precomp.hpp"
46
using namespace cv::cuda;
48
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || !defined(HAVE_OPENCV_CUDAFILTERS)
50
Ptr<cuda::HoughCirclesDetector> cv::cuda::createHoughCirclesDetector(float, float, int, int, int, int, int) { throw_no_cuda(); return Ptr<HoughCirclesDetector>(); }
52
#else /* !defined (HAVE_CUDA) */
54
namespace cv { namespace cuda { namespace device
58
int buildPointList_gpu(PtrStepSzb src, unsigned int* list);
61
namespace hough_circles
63
void circlesAccumCenters_gpu(const unsigned int* list, int count, PtrStepi dx, PtrStepi dy, PtrStepSzi accum, int minRadius, int maxRadius, float idp);
64
int buildCentersList_gpu(PtrStepSzi accum, unsigned int* centers, int threshold);
65
int circlesAccumRadius_gpu(const unsigned int* centers, int centersCount, const unsigned int* list, int count,
66
float3* circles, int maxCircles, float dp, int minRadius, int maxRadius, int threshold, bool has20);
72
class HoughCirclesDetectorImpl : public HoughCirclesDetector
75
HoughCirclesDetectorImpl(float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles);
77
void detect(InputArray src, OutputArray circles, Stream& stream);
79
void setDp(float dp) { dp_ = dp; }
80
float getDp() const { return dp_; }
82
void setMinDist(float minDist) { minDist_ = minDist; }
83
float getMinDist() const { return minDist_; }
85
void setCannyThreshold(int cannyThreshold) { cannyThreshold_ = cannyThreshold; }
86
int getCannyThreshold() const { return cannyThreshold_; }
88
void setVotesThreshold(int votesThreshold) { votesThreshold_ = votesThreshold; }
89
int getVotesThreshold() const { return votesThreshold_; }
91
void setMinRadius(int minRadius) { minRadius_ = minRadius; }
92
int getMinRadius() const { return minRadius_; }
94
void setMaxRadius(int maxRadius) { maxRadius_ = maxRadius; }
95
int getMaxRadius() const { return maxRadius_; }
97
void setMaxCircles(int maxCircles) { maxCircles_ = maxCircles; }
98
int getMaxCircles() const { return maxCircles_; }
100
void write(FileStorage& fs) const
102
fs << "name" << "HoughCirclesDetector_CUDA"
104
<< "minDist" << minDist_
105
<< "cannyThreshold" << cannyThreshold_
106
<< "votesThreshold" << votesThreshold_
107
<< "minRadius" << minRadius_
108
<< "maxRadius" << maxRadius_
109
<< "maxCircles" << maxCircles_;
112
void read(const FileNode& fn)
114
CV_Assert( String(fn["name"]) == "HoughCirclesDetector_CUDA" );
115
dp_ = (float)fn["dp"];
116
minDist_ = (float)fn["minDist"];
117
cannyThreshold_ = (int)fn["cannyThreshold"];
118
votesThreshold_ = (int)fn["votesThreshold"];
119
minRadius_ = (int)fn["minRadius"];
120
maxRadius_ = (int)fn["maxRadius"];
121
maxCircles_ = (int)fn["maxCircles"];
136
Mat tt; //CPU copy of accum_
139
Ptr<cuda::Filter> filterDx_;
140
Ptr<cuda::Filter> filterDy_;
141
Ptr<cuda::CannyEdgeDetector> canny_;
144
bool centersCompare(Vec3f a, Vec3f b) {return (a[2] > b[2]);}
146
HoughCirclesDetectorImpl::HoughCirclesDetectorImpl(float dp, float minDist, int cannyThreshold, int votesThreshold,
147
int minRadius, int maxRadius, int maxCircles) :
148
dp_(dp), minDist_(minDist), cannyThreshold_(cannyThreshold), votesThreshold_(votesThreshold),
149
minRadius_(minRadius), maxRadius_(maxRadius), maxCircles_(maxCircles)
151
canny_ = cuda::createCannyEdgeDetector(std::max(cannyThreshold_ / 2, 1), cannyThreshold_);
153
filterDx_ = cuda::createSobelFilter(CV_8UC1, CV_32S, 1, 0);
154
filterDy_ = cuda::createSobelFilter(CV_8UC1, CV_32S, 0, 1);
157
void HoughCirclesDetectorImpl::detect(InputArray _src, OutputArray circles, Stream& stream)
159
// TODO : implement async version
162
using namespace cv::cuda::device::hough;
163
using namespace cv::cuda::device::hough_circles;
165
GpuMat src = _src.getGpuMat();
167
CV_Assert( src.type() == CV_8UC1 );
168
CV_Assert( src.cols < std::numeric_limits<unsigned short>::max() );
169
CV_Assert( src.rows < std::numeric_limits<unsigned short>::max() );
170
CV_Assert( dp_ > 0 );
171
CV_Assert( minRadius_ > 0 && maxRadius_ > minRadius_ );
172
CV_Assert( cannyThreshold_ > 0 );
173
CV_Assert( votesThreshold_ > 0 );
174
CV_Assert( maxCircles_ > 0 );
176
const float idp = 1.0f / dp_;
178
filterDx_->apply(src, dx_);
179
filterDy_->apply(src, dy_);
181
canny_->setLowThreshold(std::max(cannyThreshold_ / 2, 1));
182
canny_->setHighThreshold(cannyThreshold_);
184
canny_->detect(dx_, dy_, edges_);
186
ensureSizeIsEnough(2, src.size().area(), CV_32SC1, list_);
187
unsigned int* srcPoints = list_.ptr<unsigned int>(0);
188
unsigned int* centers = list_.ptr<unsigned int>(1);
190
const int pointsCount = buildPointList_gpu(edges_, srcPoints);
191
if (pointsCount == 0)
197
ensureSizeIsEnough(cvCeil(src.rows * idp) + 2, cvCeil(src.cols * idp) + 2, CV_32SC1, accum_);
198
accum_.setTo(Scalar::all(0));
200
circlesAccumCenters_gpu(srcPoints, pointsCount, dx_, dy_, accum_, minRadius_, maxRadius_, idp);
204
int centersCount = buildCentersList_gpu(accum_, centers, votesThreshold_);
205
if (centersCount == 0)
213
AutoBuffer<ushort2> oldBuf_(centersCount);
214
AutoBuffer<ushort2> newBuf_(centersCount);
217
ushort2* oldBuf = oldBuf_;
218
ushort2* newBuf = newBuf_;
220
cudaSafeCall( cudaMemcpy(oldBuf, centers, centersCount * sizeof(ushort2), cudaMemcpyDeviceToHost) );
222
const int cellSize = cvRound(minDist_);
223
const int gridWidth = (src.cols + cellSize - 1) / cellSize;
224
const int gridHeight = (src.rows + cellSize - 1) / cellSize;
226
std::vector< std::vector<ushort2> > grid(gridWidth * gridHeight);
228
const float minDist2 = minDist_ * minDist_;
230
std::vector<Vec3f> sortBuf;
231
for(int i=0; i<centersCount; i++){
233
temp[0] = oldBuf[i].x;
234
temp[1] = oldBuf[i].y;
235
temp[2] = tt.at<int>(temp[1]+1, temp[0]+1);
236
sortBuf.push_back(temp);
238
std::sort(sortBuf.begin(), sortBuf.end(), centersCompare);
240
for (int i = 0; i < centersCount; ++i)
248
int xCell = static_cast<int>(p.x / cellSize);
249
int yCell = static_cast<int>(p.y / cellSize);
257
x1 = std::max(0, x1);
258
y1 = std::max(0, y1);
259
x2 = std::min(gridWidth - 1, x2);
260
y2 = std::min(gridHeight - 1, y2);
262
for (int yy = y1; yy <= y2; ++yy)
264
for (int xx = x1; xx <= x2; ++xx)
266
std::vector<ushort2>& m = grid[yy * gridWidth + xx];
268
for(size_t j = 0; j < m.size(); ++j)
270
float dx = (float)(p.x - m[j].x);
271
float dy = (float)(p.y - m[j].y);
273
if (dx * dx + dy * dy < minDist2)
286
grid[yCell * gridWidth + xCell].push_back(p);
288
newBuf[newCount++] = p;
292
cudaSafeCall( cudaMemcpy(centers, newBuf, newCount * sizeof(unsigned int), cudaMemcpyHostToDevice) );
293
centersCount = newCount;
296
ensureSizeIsEnough(1, maxCircles_, CV_32FC3, result_);
298
int circlesCount = circlesAccumRadius_gpu(centers, centersCount, srcPoints, pointsCount, result_.ptr<float3>(), maxCircles_,
299
dp_, minRadius_, maxRadius_, votesThreshold_, deviceSupports(FEATURE_SET_COMPUTE_20));
301
if (circlesCount == 0)
307
result_.cols = circlesCount;
308
result_.copyTo(circles);
312
Ptr<HoughCirclesDetector> cv::cuda::createHoughCirclesDetector(float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles)
314
return makePtr<HoughCirclesDetectorImpl>(dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius, maxCircles);
317
#endif /* !defined (HAVE_CUDA) */