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 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
23 //
24 // * Redistribution's in binary form must reproduce the above copyright notice,
25 // this list of conditions and the following disclaimer in the documentation
26 // and/or other materials provided with the distribution.
27 //
28 // * The name of the copyright holders may not be used to endorse or promote products
29 // derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43
44 #pragma once
45
46 #ifndef __OPENCV_CUDEV_PTR2D_DERIV_HPP__
47 #define __OPENCV_CUDEV_PTR2D_DERIV_HPP__
48
49 #include "../common.hpp"
50 #include "../grid/copy.hpp"
51 #include "traits.hpp"
52 #include "gpumat.hpp"
53
54 namespace cv { namespace cudev {
55
56 //! @addtogroup cudev
57 //! @{
58
59 // derivX
60
61 template <class SrcPtr> struct DerivXPtr
62 {
63 typedef typename PtrTraits<SrcPtr>::value_type value_type;
64 typedef int index_type;
65
66 SrcPtr src;
67
operator ()cv::cudev::DerivXPtr68 __device__ __forceinline__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
69 {
70 return src(y, x + 1) - src(y, x - 1);
71 }
72 };
73
74 template <class SrcPtr> struct DerivXPtrSz : DerivXPtr<SrcPtr>
75 {
76 int rows, cols;
77
78 template <typename T>
assignTocv::cudev::DerivXPtrSz79 __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
80 {
81 gridCopy(*this, dst, stream);
82 }
83 };
84
85 template <class SrcPtr>
derivXPtr(const SrcPtr & src)86 __host__ DerivXPtrSz<typename PtrTraits<SrcPtr>::ptr_type> derivXPtr(const SrcPtr& src)
87 {
88 DerivXPtrSz<typename PtrTraits<SrcPtr>::ptr_type> s;
89 s.src = shrinkPtr(src);
90 s.rows = getRows(src);
91 s.cols = getCols(src);
92 return s;
93 }
94
95 template <class SrcPtr> struct PtrTraits< DerivXPtrSz<SrcPtr> > : PtrTraitsBase<DerivXPtrSz<SrcPtr>, DerivXPtr<SrcPtr> >
96 {
97 };
98
99 // derivY
100
101 template <class SrcPtr> struct DerivYPtr
102 {
103 typedef typename PtrTraits<SrcPtr>::value_type value_type;
104 typedef int index_type;
105
106 SrcPtr src;
107
operator ()cv::cudev::DerivYPtr108 __device__ __forceinline__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
109 {
110 return src(y + 1, x) - src(y - 1, x);
111 }
112 };
113
114 template <class SrcPtr> struct DerivYPtrSz : DerivYPtr<SrcPtr>
115 {
116 int rows, cols;
117
118 template <typename T>
assignTocv::cudev::DerivYPtrSz119 __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
120 {
121 gridCopy(*this, dst, stream);
122 }
123 };
124
125 template <class SrcPtr>
derivYPtr(const SrcPtr & src)126 __host__ DerivYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> derivYPtr(const SrcPtr& src)
127 {
128 DerivYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> s;
129 s.src = shrinkPtr(src);
130 s.rows = getRows(src);
131 s.cols = getCols(src);
132 return s;
133 }
134
135 template <class SrcPtr> struct PtrTraits< DerivYPtrSz<SrcPtr> > : PtrTraitsBase<DerivYPtrSz<SrcPtr>, DerivYPtr<SrcPtr> >
136 {
137 };
138
139 // sobelX
140
141 template <class SrcPtr> struct SobelXPtr
142 {
143 typedef typename PtrTraits<SrcPtr>::value_type value_type;
144 typedef int index_type;
145
146 SrcPtr src;
147
operator ()cv::cudev::SobelXPtr148 __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
149 {
150 typename PtrTraits<SrcPtr>::value_type vals[6] =
151 {
152 src(y - 1, x - 1), src(y - 1, x + 1),
153 src(y , x - 1), src(y , x + 1),
154 src(y + 1, x - 1), src(y + 1, x + 1),
155 };
156
157 return (vals[1] - vals[0]) + 2 * (vals[3] - vals[2]) + (vals[5] - vals[4]);
158 }
159 };
160
161 template <class SrcPtr> struct SobelXPtrSz : SobelXPtr<SrcPtr>
162 {
163 int rows, cols;
164
165 template <typename T>
assignTocv::cudev::SobelXPtrSz166 __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
167 {
168 gridCopy(*this, dst, stream);
169 }
170 };
171
172 template <class SrcPtr>
sobelXPtr(const SrcPtr & src)173 __host__ SobelXPtrSz<typename PtrTraits<SrcPtr>::ptr_type> sobelXPtr(const SrcPtr& src)
174 {
175 SobelXPtrSz<typename PtrTraits<SrcPtr>::ptr_type> s;
176 s.src = shrinkPtr(src);
177 s.rows = getRows(src);
178 s.cols = getCols(src);
179 return s;
180 }
181
182 template <class SrcPtr> struct PtrTraits< SobelXPtrSz<SrcPtr> > : PtrTraitsBase<SobelXPtrSz<SrcPtr>, SobelXPtr<SrcPtr> >
183 {
184 };
185
186 // sobelY
187
188 template <class SrcPtr> struct SobelYPtr
189 {
190 typedef typename PtrTraits<SrcPtr>::value_type value_type;
191 typedef int index_type;
192
193 SrcPtr src;
194
operator ()cv::cudev::SobelYPtr195 __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
196 {
197 typename PtrTraits<SrcPtr>::value_type vals[6] =
198 {
199 src(y - 1, x - 1), src(y - 1, x), src(y - 1, x + 1),
200 src(y + 1, x - 1), src(y + 1, x), src(y + 1, x + 1)
201 };
202
203 return (vals[3] - vals[0]) + 2 * (vals[4] - vals[1]) + (vals[5] - vals[2]);
204 }
205 };
206
207 template <class SrcPtr> struct SobelYPtrSz : SobelYPtr<SrcPtr>
208 {
209 int rows, cols;
210
211 template <typename T>
assignTocv::cudev::SobelYPtrSz212 __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
213 {
214 gridCopy(*this, dst, stream);
215 }
216 };
217
218 template <class SrcPtr>
sobelYPtr(const SrcPtr & src)219 __host__ SobelYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> sobelYPtr(const SrcPtr& src)
220 {
221 SobelYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> s;
222 s.src = shrinkPtr(src);
223 s.rows = getRows(src);
224 s.cols = getCols(src);
225 return s;
226 }
227
228 template <class SrcPtr> struct PtrTraits< SobelYPtrSz<SrcPtr> > : PtrTraitsBase<SobelYPtrSz<SrcPtr>, SobelYPtr<SrcPtr> >
229 {
230 };
231
232 // scharrX
233
234 template <class SrcPtr> struct ScharrXPtr
235 {
236 typedef typename PtrTraits<SrcPtr>::value_type value_type;
237 typedef int index_type;
238
239 SrcPtr src;
240
operator ()cv::cudev::ScharrXPtr241 __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
242 {
243 typename PtrTraits<SrcPtr>::value_type vals[6] =
244 {
245 src(y - 1, x - 1), src(y - 1, x + 1),
246 src(y , x - 1), src(y , x + 1),
247 src(y + 1, x - 1), src(y + 1, x + 1),
248 };
249
250 return 3 * (vals[1] - vals[0]) + 10 * (vals[3] - vals[2]) + 3 * (vals[5] - vals[4]);
251 }
252 };
253
254 template <class SrcPtr> struct ScharrXPtrSz : ScharrXPtr<SrcPtr>
255 {
256 int rows, cols;
257
258 template <typename T>
assignTocv::cudev::ScharrXPtrSz259 __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
260 {
261 gridCopy(*this, dst, stream);
262 }
263 };
264
265 template <class SrcPtr>
scharrXPtr(const SrcPtr & src)266 __host__ ScharrXPtrSz<typename PtrTraits<SrcPtr>::ptr_type> scharrXPtr(const SrcPtr& src)
267 {
268 ScharrXPtrSz<typename PtrTraits<SrcPtr>::ptr_type> s;
269 s.src = shrinkPtr(src);
270 s.rows = getRows(src);
271 s.cols = getCols(src);
272 return s;
273 }
274
275 template <class SrcPtr> struct PtrTraits< ScharrXPtrSz<SrcPtr> > : PtrTraitsBase<ScharrXPtrSz<SrcPtr>, ScharrXPtr<SrcPtr> >
276 {
277 };
278
279 // scharrY
280
281 template <class SrcPtr> struct ScharrYPtr
282 {
283 typedef typename PtrTraits<SrcPtr>::value_type value_type;
284 typedef int index_type;
285
286 SrcPtr src;
287
operator ()cv::cudev::ScharrYPtr288 __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
289 {
290 typename PtrTraits<SrcPtr>::value_type vals[6] =
291 {
292 src(y - 1, x - 1), src(y - 1, x), src(y - 1, x + 1),
293 src(y + 1, x - 1), src(y + 1, x), src(y + 1, x + 1)
294 };
295
296 return 3 * (vals[3] - vals[0]) + 10 * (vals[4] - vals[1]) + 3 * (vals[5] - vals[2]);
297 }
298 };
299
300 template <class SrcPtr> struct ScharrYPtrSz : ScharrYPtr<SrcPtr>
301 {
302 int rows, cols;
303
304 template <typename T>
assignTocv::cudev::ScharrYPtrSz305 __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
306 {
307 gridCopy(*this, dst, stream);
308 }
309 };
310
311 template <class SrcPtr>
scharrYPtr(const SrcPtr & src)312 __host__ ScharrYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> scharrYPtr(const SrcPtr& src)
313 {
314 ScharrYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> s;
315 s.src = shrinkPtr(src);
316 s.rows = getRows(src);
317 s.cols = getCols(src);
318 return s;
319 }
320
321 template <class SrcPtr> struct PtrTraits< ScharrYPtrSz<SrcPtr> > : PtrTraitsBase<ScharrYPtrSz<SrcPtr>, ScharrYPtr<SrcPtr> >
322 {
323 };
324
325 // laplacian
326
327 template <int ksize, class SrcPtr> struct LaplacianPtr;
328
329 template <class SrcPtr> struct LaplacianPtr<1, SrcPtr>
330 {
331 typedef typename PtrTraits<SrcPtr>::value_type value_type;
332 typedef int index_type;
333
334 SrcPtr src;
335
operator ()cv::cudev::LaplacianPtr336 __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
337 {
338 typename PtrTraits<SrcPtr>::value_type vals[5] =
339 {
340 src(y - 1, x),
341 src(y, x - 1), src(y , x), src(y, x + 1),
342 src(y + 1, x)
343 };
344
345 return (vals[0] + vals[1] + vals[3] + vals[4]) - 4 * vals[2];
346 }
347 };
348
349 template <class SrcPtr> struct LaplacianPtr<3, SrcPtr>
350 {
351 typedef typename PtrTraits<SrcPtr>::value_type value_type;
352 typedef int index_type;
353
354 SrcPtr src;
355
operator ()cv::cudev::LaplacianPtr356 __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
357 {
358 typename PtrTraits<SrcPtr>::value_type vals[5] =
359 {
360 src(y - 1, x - 1), src(y - 1, x + 1),
361 src(y, x),
362 src(y + 1, x - 1), src(y + 1, x + 1)
363 };
364
365 return 2 * (vals[0] + vals[1] + vals[3] + vals[4]) - 8 * vals[2];
366 }
367 };
368
369 template <int ksize, class SrcPtr> struct LaplacianPtrSz : LaplacianPtr<ksize, SrcPtr>
370 {
371 int rows, cols;
372
373 template <typename T>
assignTocv::cudev::LaplacianPtrSz374 __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
375 {
376 gridCopy(*this, dst, stream);
377 }
378 };
379
380 template <int ksize, class SrcPtr>
laplacianPtr(const SrcPtr & src)381 __host__ LaplacianPtrSz<ksize, typename PtrTraits<SrcPtr>::ptr_type> laplacianPtr(const SrcPtr& src)
382 {
383 LaplacianPtrSz<ksize, typename PtrTraits<SrcPtr>::ptr_type> ptr;
384 ptr.src = shrinkPtr(src);
385 ptr.rows = getRows(src);
386 ptr.cols = getCols(src);
387 return ptr;
388 }
389
390 template <int ksize, class SrcPtr> struct PtrTraits< LaplacianPtrSz<ksize, SrcPtr> > : PtrTraitsBase<LaplacianPtrSz<ksize, SrcPtr>, LaplacianPtr<ksize, SrcPtr> >
391 {
392 };
393
394 //! @}
395
396 }}
397
398 #endif
399