1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
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.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
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.
26 //
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.
29 //
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.
40 //
41 //M*/
42 
43 #include "precomp.hpp"
44 
45 using namespace cv;
46 using namespace cv::cuda;
47 
48 #if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
49 
createHoughSegmentDetector(float,float,int,int,int)50 Ptr<cuda::HoughSegmentDetector> cv::cuda::createHoughSegmentDetector(float, float, int, int, int) { throw_no_cuda(); return Ptr<HoughSegmentDetector>(); }
51 
52 #else /* !defined (HAVE_CUDA) */
53 
54 namespace cv { namespace cuda { namespace device
55 {
56     namespace hough
57     {
58         int buildPointList_gpu(PtrStepSzb src, unsigned int* list, int* counterPtr, cudaStream_t stream);
59     }
60 
61     namespace hough_lines
62     {
63         void linesAccum_gpu(const unsigned int* list, int count, PtrStepSzi accum, float rho, float theta, size_t sharedMemPerBlock, bool has20, cudaStream_t stream);
64     }
65 
66     namespace hough_segments
67     {
68         int houghLinesProbabilistic_gpu(PtrStepSzb mask, PtrStepSzi accum, int4* out, int maxSize, float rho, float theta, int lineGap, int lineLength, int* counterPtr, cudaStream_t stream);
69     }
70 }}}
71 
72 namespace
73 {
74     class HoughSegmentDetectorImpl : public HoughSegmentDetector
75     {
76     public:
77         HoughSegmentDetectorImpl(float rho, float theta, int minLineLength, int maxLineGap, int maxLines);
78         ~HoughSegmentDetectorImpl();
79 
80         void detect(InputArray src, OutputArray lines, Stream& stream);
81 
setRho(float rho)82         void setRho(float rho) { rho_ = rho; }
getRho() const83         float getRho() const { return rho_; }
84 
setTheta(float theta)85         void setTheta(float theta) { theta_ = theta; }
getTheta() const86         float getTheta() const { return theta_; }
87 
setMinLineLength(int minLineLength)88         void setMinLineLength(int minLineLength) { minLineLength_ = minLineLength; }
getMinLineLength() const89         int getMinLineLength() const { return minLineLength_; }
90 
setMaxLineGap(int maxLineGap)91         void setMaxLineGap(int maxLineGap) { maxLineGap_ = maxLineGap; }
getMaxLineGap() const92         int getMaxLineGap() const { return maxLineGap_; }
93 
setMaxLines(int maxLines)94         void setMaxLines(int maxLines) { maxLines_ = maxLines; }
getMaxLines() const95         int getMaxLines() const { return maxLines_; }
96 
write(FileStorage & fs) const97         void write(FileStorage& fs) const
98         {
99             writeFormat(fs);
100             fs << "name" << "PHoughLinesDetector_CUDA"
101             << "rho" << rho_
102             << "theta" << theta_
103             << "minLineLength" << minLineLength_
104             << "maxLineGap" << maxLineGap_
105             << "maxLines" << maxLines_;
106         }
107 
read(const FileNode & fn)108         void read(const FileNode& fn)
109         {
110             CV_Assert( String(fn["name"]) == "PHoughLinesDetector_CUDA" );
111             rho_ = (float)fn["rho"];
112             theta_ = (float)fn["theta"];
113             minLineLength_ = (int)fn["minLineLength"];
114             maxLineGap_ = (int)fn["maxLineGap"];
115             maxLines_ = (int)fn["maxLines"];
116         }
117 
118     private:
119         float rho_;
120         float theta_;
121         int minLineLength_;
122         int maxLineGap_;
123         int maxLines_;
124 
125         GpuMat accum_;
126         GpuMat list_;
127         GpuMat result_;
128 
129         int* counterPtr_;
130     };
131 
HoughSegmentDetectorImpl(float rho,float theta,int minLineLength,int maxLineGap,int maxLines)132     HoughSegmentDetectorImpl::HoughSegmentDetectorImpl(float rho, float theta, int minLineLength, int maxLineGap, int maxLines) :
133         rho_(rho), theta_(theta), minLineLength_(minLineLength), maxLineGap_(maxLineGap), maxLines_(maxLines)
134     {
135         cudaSafeCall(cudaMalloc(&counterPtr_, sizeof(int)));
136     }
137 
~HoughSegmentDetectorImpl()138     HoughSegmentDetectorImpl::~HoughSegmentDetectorImpl()
139     {
140         cudaSafeCall(cudaFree(counterPtr_));
141     }
142 
detect(InputArray _src,OutputArray lines,Stream & stream)143     void HoughSegmentDetectorImpl::detect(InputArray _src, OutputArray lines, Stream& stream)
144     {
145         // TODO : implement async version
146         CV_UNUSED(stream);
147 
148         using namespace cv::cuda::device::hough;
149         using namespace cv::cuda::device::hough_lines;
150         using namespace cv::cuda::device::hough_segments;
151 
152         auto cudaStream = StreamAccessor::getStream(stream);
153         GpuMat src = _src.getGpuMat();
154 
155         CV_Assert( src.type() == CV_8UC1 );
156         CV_Assert( src.cols < std::numeric_limits<unsigned short>::max() );
157         CV_Assert( src.rows < std::numeric_limits<unsigned short>::max() );
158 
159         ensureSizeIsEnough(1, src.size().area(), CV_32SC1, list_);
160         unsigned int* srcPoints = list_.ptr<unsigned int>();
161 
162         const int pointsCount = buildPointList_gpu(src, srcPoints, counterPtr_, cudaStream);
163         if (pointsCount == 0)
164         {
165             lines.release();
166             return;
167         }
168 
169         const int numangle = cvRound(CV_PI / theta_);
170         const int numrho = cvRound(((src.cols + src.rows) * 2 + 1) / rho_);
171         CV_Assert( numangle > 0 && numrho > 0 );
172 
173         ensureSizeIsEnough(numangle + 2, numrho + 2, CV_32SC1, accum_);
174         accum_.setTo(Scalar::all(0), stream);
175 
176         DeviceInfo devInfo;
177         linesAccum_gpu(srcPoints, pointsCount, accum_, rho_, theta_, devInfo.sharedMemPerBlock(), devInfo.supports(FEATURE_SET_COMPUTE_20), cudaStream);
178 
179         ensureSizeIsEnough(1, maxLines_, CV_32SC4, result_);
180 
181         int linesCount = houghLinesProbabilistic_gpu(src, accum_, result_.ptr<int4>(), maxLines_, rho_, theta_, maxLineGap_, minLineLength_, counterPtr_, cudaStream);
182 
183         if (linesCount == 0)
184         {
185             lines.release();
186             return;
187         }
188 
189         result_.cols = linesCount;
190         result_.copyTo(lines, stream);
191     }
192 }
193 
createHoughSegmentDetector(float rho,float theta,int minLineLength,int maxLineGap,int maxLines)194 Ptr<HoughSegmentDetector> cv::cuda::createHoughSegmentDetector(float rho, float theta, int minLineLength, int maxLineGap, int maxLines)
195 {
196     return makePtr<HoughSegmentDetectorImpl>(rho, theta, minLineLength, maxLineGap, maxLines);
197 }
198 
199 #endif /* !defined (HAVE_CUDA) */
200