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 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); 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); 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); 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 rho_(rho), theta_(theta), minLineLength_(minLineLength), maxLineGap_(maxLineGap), maxLines_(maxLines) 79 { 80 } 81 82 void detect(InputArray src, OutputArray lines, Stream& stream); 83 84 void setRho(float rho) { rho_ = rho; } 85 float getRho() const { return rho_; } 86 87 void setTheta(float theta) { theta_ = theta; } 88 float getTheta() const { return theta_; } 89 90 void setMinLineLength(int minLineLength) { minLineLength_ = minLineLength; } 91 int getMinLineLength() const { return minLineLength_; } 92 93 void setMaxLineGap(int maxLineGap) { maxLineGap_ = maxLineGap; } 94 int getMaxLineGap() const { return maxLineGap_; } 95 96 void setMaxLines(int maxLines) { maxLines_ = maxLines; } 97 int getMaxLines() const { return maxLines_; } 98 99 void write(FileStorage& fs) const 100 { 101 fs << "name" << "PHoughLinesDetector_CUDA" 102 << "rho" << rho_ 103 << "theta" << theta_ 104 << "minLineLength" << minLineLength_ 105 << "maxLineGap" << maxLineGap_ 106 << "maxLines" << maxLines_; 107 } 108 109 void read(const FileNode& fn) 110 { 111 CV_Assert( String(fn["name"]) == "PHoughLinesDetector_CUDA" ); 112 rho_ = (float)fn["rho"]; 113 theta_ = (float)fn["theta"]; 114 minLineLength_ = (int)fn["minLineLength"]; 115 maxLineGap_ = (int)fn["maxLineGap"]; 116 maxLines_ = (int)fn["maxLines"]; 117 } 118 119 private: 120 float rho_; 121 float theta_; 122 int minLineLength_; 123 int maxLineGap_; 124 int maxLines_; 125 126 GpuMat accum_; 127 GpuMat list_; 128 GpuMat result_; 129 }; 130 131 void HoughSegmentDetectorImpl::detect(InputArray _src, OutputArray lines, Stream& stream) 132 { 133 // TODO : implement async version 134 (void) stream; 135 136 using namespace cv::cuda::device::hough; 137 using namespace cv::cuda::device::hough_lines; 138 using namespace cv::cuda::device::hough_segments; 139 140 GpuMat src = _src.getGpuMat(); 141 142 CV_Assert( src.type() == CV_8UC1 ); 143 CV_Assert( src.cols < std::numeric_limits<unsigned short>::max() ); 144 CV_Assert( src.rows < std::numeric_limits<unsigned short>::max() ); 145 146 ensureSizeIsEnough(1, src.size().area(), CV_32SC1, list_); 147 unsigned int* srcPoints = list_.ptr<unsigned int>(); 148 149 const int pointsCount = buildPointList_gpu(src, srcPoints); 150 if (pointsCount == 0) 151 { 152 lines.release(); 153 return; 154 } 155 156 const int numangle = cvRound(CV_PI / theta_); 157 const int numrho = cvRound(((src.cols + src.rows) * 2 + 1) / rho_); 158 CV_Assert( numangle > 0 && numrho > 0 ); 159 160 ensureSizeIsEnough(numangle + 2, numrho + 2, CV_32SC1, accum_); 161 accum_.setTo(Scalar::all(0)); 162 163 DeviceInfo devInfo; 164 linesAccum_gpu(srcPoints, pointsCount, accum_, rho_, theta_, devInfo.sharedMemPerBlock(), devInfo.supports(FEATURE_SET_COMPUTE_20)); 165 166 ensureSizeIsEnough(1, maxLines_, CV_32SC4, result_); 167 168 int linesCount = houghLinesProbabilistic_gpu(src, accum_, result_.ptr<int4>(), maxLines_, rho_, theta_, maxLineGap_, minLineLength_); 169 170 if (linesCount == 0) 171 { 172 lines.release(); 173 return; 174 } 175 176 result_.cols = linesCount; 177 result_.copyTo(lines); 178 } 179 } 180 181 Ptr<HoughSegmentDetector> cv::cuda::createHoughSegmentDetector(float rho, float theta, int minLineLength, int maxLineGap, int maxLines) 182 { 183 return makePtr<HoughSegmentDetectorImpl>(rho, theta, minLineLength, maxLineGap, maxLines); 184 } 185 186 #endif /* !defined (HAVE_CUDA) */ 187