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"
45 #if !defined (HAVE_CUDA)
47 void cv::gpu::HoughLines(const GpuMat&, GpuMat&, float, float, int, bool, int) { throw_nogpu(); }
48 void cv::gpu::HoughLines(const GpuMat&, GpuMat&, HoughLinesBuf&, float, float, int, bool, int) { throw_nogpu(); }
49 void cv::gpu::HoughLinesDownload(const GpuMat&, OutputArray, OutputArray) { throw_nogpu(); }
51 void cv::gpu::HoughCircles(const GpuMat&, GpuMat&, int, float, float, int, int, int, int, int) { throw_nogpu(); }
52 void cv::gpu::HoughCircles(const GpuMat&, GpuMat&, HoughCirclesBuf&, int, float, float, int, int, int, int, int) { throw_nogpu(); }
53 void cv::gpu::HoughCirclesDownload(const GpuMat&, OutputArray) { throw_nogpu(); }
55 #else /* !defined (HAVE_CUDA) */
57 namespace cv { namespace gpu { namespace device
61 int buildPointList_gpu(PtrStepSzb src, unsigned int* list);
63 void linesAccum_gpu(const unsigned int* list, int count, PtrStepSzi accum, float rho, float theta, size_t sharedMemPerBlock, bool has20);
64 int linesGetResult_gpu(PtrStepSzi accum, float2* out, int* votes, int maxSize, float rho, float theta, int threshold, bool doSort);
66 void circlesAccumCenters_gpu(const unsigned int* list, int count, PtrStepi dx, PtrStepi dy, PtrStepSzi accum, int minRadius, int maxRadius, float idp);
67 int buildCentersList_gpu(PtrStepSzi accum, unsigned int* centers, int threshold);
68 int circlesAccumRadius_gpu(const unsigned int* centers, int centersCount, const unsigned int* list, int count,
69 float3* circles, int maxCircles, float dp, int minRadius, int maxRadius, int threshold, bool has20);
73 //////////////////////////////////////////////////////////
76 void cv::gpu::HoughLines(const GpuMat& src, GpuMat& lines, float rho, float theta, int threshold, bool doSort, int maxLines)
79 HoughLines(src, lines, buf, rho, theta, threshold, doSort, maxLines);
82 void cv::gpu::HoughLines(const GpuMat& src, GpuMat& lines, HoughLinesBuf& buf, float rho, float theta, int threshold, bool doSort, int maxLines)
84 using namespace cv::gpu::device::hough;
86 CV_Assert(src.type() == CV_8UC1);
87 CV_Assert(src.cols < std::numeric_limits<unsigned short>::max());
88 CV_Assert(src.rows < std::numeric_limits<unsigned short>::max());
90 ensureSizeIsEnough(1, src.size().area(), CV_32SC1, buf.list);
91 unsigned int* srcPoints = buf.list.ptr<unsigned int>();
93 const int pointsCount = buildPointList_gpu(src, srcPoints);
100 const int numangle = cvRound(CV_PI / theta);
101 const int numrho = cvRound(((src.cols + src.rows) * 2 + 1) / rho);
102 CV_Assert(numangle > 0 && numrho > 0);
104 ensureSizeIsEnough(numangle + 2, numrho + 2, CV_32SC1, buf.accum);
105 buf.accum.setTo(Scalar::all(0));
108 linesAccum_gpu(srcPoints, pointsCount, buf.accum, rho, theta, devInfo.sharedMemPerBlock(), devInfo.supports(FEATURE_SET_COMPUTE_20));
110 ensureSizeIsEnough(2, maxLines, CV_32FC2, lines);
112 int linesCount = linesGetResult_gpu(buf.accum, lines.ptr<float2>(0), lines.ptr<int>(1), maxLines, rho, theta, threshold, doSort);
114 lines.cols = linesCount;
119 void cv::gpu::HoughLinesDownload(const GpuMat& d_lines, OutputArray h_lines_, OutputArray h_votes_)
124 if (h_votes_.needed())
129 CV_Assert(d_lines.rows == 2 && d_lines.type() == CV_32FC2);
131 h_lines_.create(1, d_lines.cols, CV_32FC2);
132 Mat h_lines = h_lines_.getMat();
133 d_lines.row(0).download(h_lines);
135 if (h_votes_.needed())
137 h_votes_.create(1, d_lines.cols, CV_32SC1);
138 Mat h_votes = h_votes_.getMat();
139 GpuMat d_votes(1, d_lines.cols, CV_32SC1, const_cast<int*>(d_lines.ptr<int>(1)));
140 d_votes.download(h_votes);
144 //////////////////////////////////////////////////////////
147 void cv::gpu::HoughCircles(const GpuMat& src, GpuMat& circles, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles)
150 HoughCircles(src, circles, buf, method, dp, minDist, cannyThreshold, votesThreshold, minRadius, maxRadius, maxCircles);
153 void cv::gpu::HoughCircles(const GpuMat& src, GpuMat& circles, HoughCirclesBuf& buf, int method,
154 float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles)
156 using namespace cv::gpu::device::hough;
158 CV_Assert(src.type() == CV_8UC1);
159 CV_Assert(src.cols < std::numeric_limits<unsigned short>::max());
160 CV_Assert(src.rows < std::numeric_limits<unsigned short>::max());
161 CV_Assert(method == CV_HOUGH_GRADIENT);
163 CV_Assert(minRadius > 0 && maxRadius > minRadius);
164 CV_Assert(cannyThreshold > 0);
165 CV_Assert(votesThreshold > 0);
166 CV_Assert(maxCircles > 0);
168 const float idp = 1.0f / dp;
170 cv::gpu::Canny(src, buf.cannyBuf, buf.edges, std::max(cannyThreshold / 2, 1), cannyThreshold);
172 ensureSizeIsEnough(2, src.size().area(), CV_32SC1, buf.list);
173 unsigned int* srcPoints = buf.list.ptr<unsigned int>(0);
174 unsigned int* centers = buf.list.ptr<unsigned int>(1);
176 const int pointsCount = buildPointList_gpu(buf.edges, srcPoints);
177 if (pointsCount == 0)
183 ensureSizeIsEnough(cvCeil(src.rows * idp) + 2, cvCeil(src.cols * idp) + 2, CV_32SC1, buf.accum);
184 buf.accum.setTo(Scalar::all(0));
186 circlesAccumCenters_gpu(srcPoints, pointsCount, buf.cannyBuf.dx, buf.cannyBuf.dy, buf.accum, minRadius, maxRadius, idp);
188 int centersCount = buildCentersList_gpu(buf.accum, centers, votesThreshold);
189 if (centersCount == 0)
197 cv::AutoBuffer<ushort2> oldBuf_(centersCount);
198 cv::AutoBuffer<ushort2> newBuf_(centersCount);
201 ushort2* oldBuf = oldBuf_;
202 ushort2* newBuf = newBuf_;
204 cudaSafeCall( cudaMemcpy(oldBuf, centers, centersCount * sizeof(ushort2), cudaMemcpyDeviceToHost) );
206 const int cellSize = cvRound(minDist);
207 const int gridWidth = (src.cols + cellSize - 1) / cellSize;
208 const int gridHeight = (src.rows + cellSize - 1) / cellSize;
210 std::vector< std::vector<ushort2> > grid(gridWidth * gridHeight);
214 for (int i = 0; i < centersCount; ++i)
216 ushort2 p = oldBuf[i];
220 int xCell = static_cast<int>(p.x / cellSize);
221 int yCell = static_cast<int>(p.y / cellSize);
229 x1 = std::max(0, x1);
230 y1 = std::max(0, y1);
231 x2 = std::min(gridWidth - 1, x2);
232 y2 = std::min(gridHeight - 1, y2);
234 for (int yy = y1; yy <= y2; ++yy)
236 for (int xx = x1; xx <= x2; ++xx)
238 vector<ushort2>& m = grid[yy * gridWidth + xx];
240 for(size_t j = 0; j < m.size(); ++j)
242 float dx = p.x - m[j].x;
243 float dy = p.y - m[j].y;
245 if (dx * dx + dy * dy < minDist)
258 grid[yCell * gridWidth + xCell].push_back(p);
260 newBuf[newCount++] = p;
264 cudaSafeCall( cudaMemcpy(centers, newBuf, newCount * sizeof(unsigned int), cudaMemcpyHostToDevice) );
265 centersCount = newCount;
268 ensureSizeIsEnough(1, maxCircles, CV_32FC3, circles);
271 const int circlesCount = circlesAccumRadius_gpu(centers, centersCount, srcPoints, pointsCount, circles.ptr<float3>(), maxCircles,
272 dp, minRadius, maxRadius, votesThreshold, devInfo.supports(FEATURE_SET_COMPUTE_20));
274 if (circlesCount > 0)
275 circles.cols = circlesCount;
280 void cv::gpu::HoughCirclesDownload(const GpuMat& d_circles, cv::OutputArray h_circles_)
282 if (d_circles.empty())
284 h_circles_.release();
288 CV_Assert(d_circles.rows == 1 && d_circles.type() == CV_32FC3);
290 h_circles_.create(1, d_circles.cols, CV_32FC3);
291 Mat h_circles = h_circles_.getMat();
292 d_circles.download(h_circles);
295 #endif /* !defined (HAVE_CUDA) */