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 
createCannyEdgeDetector(double,double,int,bool)50 Ptr<CannyEdgeDetector> cv::cuda::createCannyEdgeDetector(double, double, int, bool) { throw_no_cuda(); return Ptr<CannyEdgeDetector>(); }
51 
52 #else /* !defined (HAVE_CUDA) */
53 
54 namespace canny
55 {
56     void calcMagnitude(PtrStepSzb srcWhole, int xoff, int yoff, PtrStepSzi dx, PtrStepSzi dy, PtrStepSzf mag, bool L2Grad, cudaStream_t stream);
57     void calcMagnitude(PtrStepSzi dx, PtrStepSzi dy, PtrStepSzf mag, bool L2Grad, cudaStream_t stream);
58 
59     void calcMap(PtrStepSzi dx, PtrStepSzi dy, PtrStepSzf mag, PtrStepSzi map, float low_thresh, float high_thresh, cudaStream_t stream);
60 
61     void edgesHysteresisLocal(PtrStepSzi map, short2* st1, cudaStream_t stream);
62 
63     void edgesHysteresisGlobal(PtrStepSzi map, short2* st1, short2* st2, cudaStream_t stream);
64 
65     void getEdges(PtrStepSzi map, PtrStepSzb dst, cudaStream_t stream);
66 }
67 
68 namespace
69 {
70     class CannyImpl : public CannyEdgeDetector
71     {
72     public:
CannyImpl(double low_thresh,double high_thresh,int apperture_size,bool L2gradient)73         CannyImpl(double low_thresh, double high_thresh, int apperture_size, bool L2gradient) :
74             low_thresh_(low_thresh), high_thresh_(high_thresh), apperture_size_(apperture_size), L2gradient_(L2gradient)
75         {
76             old_apperture_size_ = -1;
77         }
78 
79         void detect(InputArray image, OutputArray edges, Stream& stream);
80         void detect(InputArray dx, InputArray dy, OutputArray edges, Stream& stream);
81 
setLowThreshold(double low_thresh)82         void setLowThreshold(double low_thresh) { low_thresh_ = low_thresh; }
getLowThreshold() const83         double getLowThreshold() const { return low_thresh_; }
84 
setHighThreshold(double high_thresh)85         void setHighThreshold(double high_thresh) { high_thresh_ = high_thresh; }
getHighThreshold() const86         double getHighThreshold() const { return high_thresh_; }
87 
setAppertureSize(int apperture_size)88         void setAppertureSize(int apperture_size) { apperture_size_ = apperture_size; }
getAppertureSize() const89         int getAppertureSize() const { return apperture_size_; }
90 
setL2Gradient(bool L2gradient)91         void setL2Gradient(bool L2gradient) { L2gradient_ = L2gradient; }
getL2Gradient() const92         bool getL2Gradient() const { return L2gradient_; }
93 
write(FileStorage & fs) const94         void write(FileStorage& fs) const
95         {
96             fs << "name" << "Canny_CUDA"
97             << "low_thresh" << low_thresh_
98             << "high_thresh" << high_thresh_
99             << "apperture_size" << apperture_size_
100             << "L2gradient" << L2gradient_;
101         }
102 
read(const FileNode & fn)103         void read(const FileNode& fn)
104         {
105             CV_Assert( String(fn["name"]) == "Canny_CUDA" );
106             low_thresh_ = (double)fn["low_thresh"];
107             high_thresh_ = (double)fn["high_thresh"];
108             apperture_size_ = (int)fn["apperture_size"];
109             L2gradient_ = (int)fn["L2gradient"] != 0;
110         }
111 
112     private:
113         void createBuf(Size image_size);
114         void CannyCaller(GpuMat& edges, Stream& stream);
115 
116         double low_thresh_;
117         double high_thresh_;
118         int apperture_size_;
119         bool L2gradient_;
120 
121         GpuMat dx_, dy_;
122         GpuMat mag_;
123         GpuMat map_;
124         GpuMat st1_, st2_;
125 #ifdef HAVE_OPENCV_CUDAFILTERS
126         Ptr<Filter> filterDX_, filterDY_;
127 #endif
128         int old_apperture_size_;
129     };
130 
detect(InputArray _image,OutputArray _edges,Stream & stream)131     void CannyImpl::detect(InputArray _image, OutputArray _edges, Stream& stream)
132     {
133         GpuMat image = _image.getGpuMat();
134 
135         CV_Assert( image.type() == CV_8UC1 );
136         CV_Assert( deviceSupports(SHARED_ATOMICS) );
137 
138         if (low_thresh_ > high_thresh_)
139             std::swap(low_thresh_, high_thresh_);
140 
141         createBuf(image.size());
142 
143         _edges.create(image.size(), CV_8UC1);
144         GpuMat edges = _edges.getGpuMat();
145 
146         if (apperture_size_ == 3)
147         {
148             Size wholeSize;
149             Point ofs;
150             image.locateROI(wholeSize, ofs);
151             GpuMat srcWhole(wholeSize, image.type(), image.datastart, image.step);
152 
153             canny::calcMagnitude(srcWhole, ofs.x, ofs.y, dx_, dy_, mag_, L2gradient_, StreamAccessor::getStream(stream));
154         }
155         else
156         {
157 #ifndef HAVE_OPENCV_CUDAFILTERS
158             throw_no_cuda();
159 #else
160             filterDX_->apply(image, dx_, stream);
161             filterDY_->apply(image, dy_, stream);
162 
163             canny::calcMagnitude(dx_, dy_, mag_, L2gradient_, StreamAccessor::getStream(stream));
164 #endif
165         }
166 
167         CannyCaller(edges, stream);
168     }
169 
detect(InputArray _dx,InputArray _dy,OutputArray _edges,Stream & stream)170     void CannyImpl::detect(InputArray _dx, InputArray _dy, OutputArray _edges, Stream& stream)
171     {
172         GpuMat dx = _dx.getGpuMat();
173         GpuMat dy = _dy.getGpuMat();
174 
175         CV_Assert( dx.type() == CV_32SC1 );
176         CV_Assert( dy.type() == dx.type() && dy.size() == dx.size() );
177         CV_Assert( deviceSupports(SHARED_ATOMICS) );
178 
179         dx.copyTo(dx_, stream);
180         dy.copyTo(dy_, stream);
181 
182         if (low_thresh_ > high_thresh_)
183             std::swap(low_thresh_, high_thresh_);
184 
185         createBuf(dx.size());
186 
187         _edges.create(dx.size(), CV_8UC1);
188         GpuMat edges = _edges.getGpuMat();
189 
190         canny::calcMagnitude(dx_, dy_, mag_, L2gradient_, StreamAccessor::getStream(stream));
191 
192         CannyCaller(edges, stream);
193     }
194 
createBuf(Size image_size)195     void CannyImpl::createBuf(Size image_size)
196     {
197         CV_Assert(image_size.width < std::numeric_limits<short>::max() && image_size.height < std::numeric_limits<short>::max());
198 
199         ensureSizeIsEnough(image_size, CV_32SC1, dx_);
200         ensureSizeIsEnough(image_size, CV_32SC1, dy_);
201 
202 #ifdef HAVE_OPENCV_CUDAFILTERS
203         if (apperture_size_ != 3 && apperture_size_ != old_apperture_size_)
204         {
205             filterDX_ = cuda::createDerivFilter(CV_8UC1, CV_32S, 1, 0, apperture_size_, false, 1, BORDER_REPLICATE);
206             filterDY_ = cuda::createDerivFilter(CV_8UC1, CV_32S, 0, 1, apperture_size_, false, 1, BORDER_REPLICATE);
207             old_apperture_size_ = apperture_size_;
208         }
209 #endif
210 
211         ensureSizeIsEnough(image_size, CV_32FC1, mag_);
212         ensureSizeIsEnough(image_size, CV_32SC1, map_);
213 
214         ensureSizeIsEnough(1, image_size.area(), CV_16SC2, st1_);
215         ensureSizeIsEnough(1, image_size.area(), CV_16SC2, st2_);
216     }
217 
CannyCaller(GpuMat & edges,Stream & stream)218     void CannyImpl::CannyCaller(GpuMat& edges, Stream& stream)
219     {
220         map_.setTo(Scalar::all(0));
221         canny::calcMap(dx_, dy_, mag_, map_, static_cast<float>(low_thresh_), static_cast<float>(high_thresh_), StreamAccessor::getStream(stream));
222 
223         canny::edgesHysteresisLocal(map_, st1_.ptr<short2>(), StreamAccessor::getStream(stream));
224 
225         canny::edgesHysteresisGlobal(map_, st1_.ptr<short2>(), st2_.ptr<short2>(), StreamAccessor::getStream(stream));
226 
227         canny::getEdges(map_, edges, StreamAccessor::getStream(stream));
228     }
229 }
230 
createCannyEdgeDetector(double low_thresh,double high_thresh,int apperture_size,bool L2gradient)231 Ptr<CannyEdgeDetector> cv::cuda::createCannyEdgeDetector(double low_thresh, double high_thresh, int apperture_size, bool L2gradient)
232 {
233     return makePtr<CannyImpl>(low_thresh, high_thresh, apperture_size, L2gradient);
234 }
235 
236 #endif /* !defined (HAVE_CUDA) */
237