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);
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)
159 using namespace cv::cuda::device::hough;
160 using namespace cv::cuda::device::hough_circles;
162 GpuMat src = _src.getGpuMat();
164 CV_Assert( src.type() == CV_8UC1 );
165 CV_Assert( src.cols < std::numeric_limits<unsigned short>::max() );
166 CV_Assert( src.rows < std::numeric_limits<unsigned short>::max() );
167 CV_Assert( dp_ > 0 );
168 CV_Assert( minRadius_ > 0 && maxRadius_ > minRadius_ );
169 CV_Assert( cannyThreshold_ > 0 );
170 CV_Assert( votesThreshold_ > 0 );
171 CV_Assert( maxCircles_ > 0 );
173 const float idp = 1.0f / dp_;
175 filterDx_->apply(src, dx_);
176 filterDy_->apply(src, dy_);
178 canny_->setLowThreshold(std::max(cannyThreshold_ / 2, 1));
179 canny_->setHighThreshold(cannyThreshold_);
181 canny_->detect(dx_, dy_, edges_);
183 ensureSizeIsEnough(2, src.size().area(), CV_32SC1, list_);
184 unsigned int* srcPoints = list_.ptr<unsigned int>(0);
185 unsigned int* centers = list_.ptr<unsigned int>(1);
187 const int pointsCount = buildPointList_gpu(edges_, srcPoints);
188 if (pointsCount == 0)
194 ensureSizeIsEnough(cvCeil(src.rows * idp) + 2, cvCeil(src.cols * idp) + 2, CV_32SC1, accum_);
195 accum_.setTo(Scalar::all(0));
197 circlesAccumCenters_gpu(srcPoints, pointsCount, dx_, dy_, accum_, minRadius_, maxRadius_, idp);
201 int centersCount = buildCentersList_gpu(accum_, centers, votesThreshold_);
202 if (centersCount == 0)
210 AutoBuffer<ushort2> oldBuf_(centersCount);
211 AutoBuffer<ushort2> newBuf_(centersCount);
214 ushort2* oldBuf = oldBuf_;
215 ushort2* newBuf = newBuf_;
217 cudaSafeCall( cudaMemcpy(oldBuf, centers, centersCount * sizeof(ushort2), cudaMemcpyDeviceToHost) );
219 const int cellSize = cvRound(minDist_);
220 const int gridWidth = (src.cols + cellSize - 1) / cellSize;
221 const int gridHeight = (src.rows + cellSize - 1) / cellSize;
223 std::vector< std::vector<ushort2> > grid(gridWidth * gridHeight);
225 const float minDist2 = minDist_ * minDist_;
227 std::vector<Vec3f> sortBuf;
228 for(int i=0; i<centersCount; i++){
230 temp[0] = oldBuf[i].x;
231 temp[1] = oldBuf[i].y;
232 temp[2] = tt.at<int>(temp[1]+1, temp[0]+1);
233 sortBuf.push_back(temp);
235 std::sort(sortBuf.begin(), sortBuf.end(), centersCompare);
237 for (int i = 0; i < centersCount; ++i)
245 int xCell = static_cast<int>(p.x / cellSize);
246 int yCell = static_cast<int>(p.y / cellSize);
254 x1 = std::max(0, x1);
255 y1 = std::max(0, y1);
256 x2 = std::min(gridWidth - 1, x2);
257 y2 = std::min(gridHeight - 1, y2);
259 for (int yy = y1; yy <= y2; ++yy)
261 for (int xx = x1; xx <= x2; ++xx)
263 std::vector<ushort2>& m = grid[yy * gridWidth + xx];
265 for(size_t j = 0; j < m.size(); ++j)
267 float dx = (float)(p.x - m[j].x);
268 float dy = (float)(p.y - m[j].y);
270 if (dx * dx + dy * dy < minDist2)
283 grid[yCell * gridWidth + xCell].push_back(p);
285 newBuf[newCount++] = p;
289 cudaSafeCall( cudaMemcpy(centers, newBuf, newCount * sizeof(unsigned int), cudaMemcpyHostToDevice) );
290 centersCount = newCount;
293 ensureSizeIsEnough(1, maxCircles_, CV_32FC3, result_);
295 int circlesCount = circlesAccumRadius_gpu(centers, centersCount, srcPoints, pointsCount, result_.ptr<float3>(), maxCircles_,
296 dp_, minRadius_, maxRadius_, votesThreshold_, deviceSupports(FEATURE_SET_COMPUTE_20));
298 if (circlesCount == 0)
304 result_.cols = circlesCount;
305 result_.copyTo(circles);
309 Ptr<HoughCirclesDetector> cv::cuda::createHoughCirclesDetector(float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles)
311 return makePtr<HoughCirclesDetectorImpl>(dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius, maxCircles);
314 #endif /* !defined (HAVE_CUDA) */