deriv.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #pragma once
  44. #ifndef OPENCV_CUDEV_PTR2D_DERIV_HPP
  45. #define OPENCV_CUDEV_PTR2D_DERIV_HPP
  46. #include "../common.hpp"
  47. #include "../grid/copy.hpp"
  48. #include "traits.hpp"
  49. #include "gpumat.hpp"
  50. namespace cv { namespace cudev {
  51. //! @addtogroup cudev
  52. //! @{
  53. // derivX
  54. template <class SrcPtr> struct DerivXPtr
  55. {
  56. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  57. typedef int index_type;
  58. SrcPtr src;
  59. __device__ __forceinline__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
  60. {
  61. return src(y, x + 1) - src(y, x - 1);
  62. }
  63. };
  64. template <class SrcPtr> struct DerivXPtrSz : DerivXPtr<SrcPtr>
  65. {
  66. int rows, cols;
  67. template <typename T>
  68. __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
  69. {
  70. gridCopy(*this, dst, stream);
  71. }
  72. };
  73. template <class SrcPtr>
  74. __host__ DerivXPtrSz<typename PtrTraits<SrcPtr>::ptr_type> derivXPtr(const SrcPtr& src)
  75. {
  76. DerivXPtrSz<typename PtrTraits<SrcPtr>::ptr_type> s;
  77. s.src = shrinkPtr(src);
  78. s.rows = getRows(src);
  79. s.cols = getCols(src);
  80. return s;
  81. }
  82. template <class SrcPtr> struct PtrTraits< DerivXPtrSz<SrcPtr> > : PtrTraitsBase<DerivXPtrSz<SrcPtr>, DerivXPtr<SrcPtr> >
  83. {
  84. };
  85. // derivY
  86. template <class SrcPtr> struct DerivYPtr
  87. {
  88. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  89. typedef int index_type;
  90. SrcPtr src;
  91. __device__ __forceinline__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
  92. {
  93. return src(y + 1, x) - src(y - 1, x);
  94. }
  95. };
  96. template <class SrcPtr> struct DerivYPtrSz : DerivYPtr<SrcPtr>
  97. {
  98. int rows, cols;
  99. template <typename T>
  100. __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
  101. {
  102. gridCopy(*this, dst, stream);
  103. }
  104. };
  105. template <class SrcPtr>
  106. __host__ DerivYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> derivYPtr(const SrcPtr& src)
  107. {
  108. DerivYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> s;
  109. s.src = shrinkPtr(src);
  110. s.rows = getRows(src);
  111. s.cols = getCols(src);
  112. return s;
  113. }
  114. template <class SrcPtr> struct PtrTraits< DerivYPtrSz<SrcPtr> > : PtrTraitsBase<DerivYPtrSz<SrcPtr>, DerivYPtr<SrcPtr> >
  115. {
  116. };
  117. // sobelX
  118. template <class SrcPtr> struct SobelXPtr
  119. {
  120. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  121. typedef int index_type;
  122. SrcPtr src;
  123. __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
  124. {
  125. typename PtrTraits<SrcPtr>::value_type vals[6] =
  126. {
  127. src(y - 1, x - 1), src(y - 1, x + 1),
  128. src(y , x - 1), src(y , x + 1),
  129. src(y + 1, x - 1), src(y + 1, x + 1),
  130. };
  131. return (vals[1] - vals[0]) + 2 * (vals[3] - vals[2]) + (vals[5] - vals[4]);
  132. }
  133. };
  134. template <class SrcPtr> struct SobelXPtrSz : SobelXPtr<SrcPtr>
  135. {
  136. int rows, cols;
  137. template <typename T>
  138. __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
  139. {
  140. gridCopy(*this, dst, stream);
  141. }
  142. };
  143. template <class SrcPtr>
  144. __host__ SobelXPtrSz<typename PtrTraits<SrcPtr>::ptr_type> sobelXPtr(const SrcPtr& src)
  145. {
  146. SobelXPtrSz<typename PtrTraits<SrcPtr>::ptr_type> s;
  147. s.src = shrinkPtr(src);
  148. s.rows = getRows(src);
  149. s.cols = getCols(src);
  150. return s;
  151. }
  152. template <class SrcPtr> struct PtrTraits< SobelXPtrSz<SrcPtr> > : PtrTraitsBase<SobelXPtrSz<SrcPtr>, SobelXPtr<SrcPtr> >
  153. {
  154. };
  155. // sobelY
  156. template <class SrcPtr> struct SobelYPtr
  157. {
  158. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  159. typedef int index_type;
  160. SrcPtr src;
  161. __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
  162. {
  163. typename PtrTraits<SrcPtr>::value_type vals[6] =
  164. {
  165. src(y - 1, x - 1), src(y - 1, x), src(y - 1, x + 1),
  166. src(y + 1, x - 1), src(y + 1, x), src(y + 1, x + 1)
  167. };
  168. return (vals[3] - vals[0]) + 2 * (vals[4] - vals[1]) + (vals[5] - vals[2]);
  169. }
  170. };
  171. template <class SrcPtr> struct SobelYPtrSz : SobelYPtr<SrcPtr>
  172. {
  173. int rows, cols;
  174. template <typename T>
  175. __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
  176. {
  177. gridCopy(*this, dst, stream);
  178. }
  179. };
  180. template <class SrcPtr>
  181. __host__ SobelYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> sobelYPtr(const SrcPtr& src)
  182. {
  183. SobelYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> s;
  184. s.src = shrinkPtr(src);
  185. s.rows = getRows(src);
  186. s.cols = getCols(src);
  187. return s;
  188. }
  189. template <class SrcPtr> struct PtrTraits< SobelYPtrSz<SrcPtr> > : PtrTraitsBase<SobelYPtrSz<SrcPtr>, SobelYPtr<SrcPtr> >
  190. {
  191. };
  192. // scharrX
  193. template <class SrcPtr> struct ScharrXPtr
  194. {
  195. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  196. typedef int index_type;
  197. SrcPtr src;
  198. __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
  199. {
  200. typename PtrTraits<SrcPtr>::value_type vals[6] =
  201. {
  202. src(y - 1, x - 1), src(y - 1, x + 1),
  203. src(y , x - 1), src(y , x + 1),
  204. src(y + 1, x - 1), src(y + 1, x + 1),
  205. };
  206. return 3 * (vals[1] - vals[0]) + 10 * (vals[3] - vals[2]) + 3 * (vals[5] - vals[4]);
  207. }
  208. };
  209. template <class SrcPtr> struct ScharrXPtrSz : ScharrXPtr<SrcPtr>
  210. {
  211. int rows, cols;
  212. template <typename T>
  213. __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
  214. {
  215. gridCopy(*this, dst, stream);
  216. }
  217. };
  218. template <class SrcPtr>
  219. __host__ ScharrXPtrSz<typename PtrTraits<SrcPtr>::ptr_type> scharrXPtr(const SrcPtr& src)
  220. {
  221. ScharrXPtrSz<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. template <class SrcPtr> struct PtrTraits< ScharrXPtrSz<SrcPtr> > : PtrTraitsBase<ScharrXPtrSz<SrcPtr>, ScharrXPtr<SrcPtr> >
  228. {
  229. };
  230. // scharrY
  231. template <class SrcPtr> struct ScharrYPtr
  232. {
  233. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  234. typedef int index_type;
  235. SrcPtr src;
  236. __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
  237. {
  238. typename PtrTraits<SrcPtr>::value_type vals[6] =
  239. {
  240. src(y - 1, x - 1), src(y - 1, x), src(y - 1, x + 1),
  241. src(y + 1, x - 1), src(y + 1, x), src(y + 1, x + 1)
  242. };
  243. return 3 * (vals[3] - vals[0]) + 10 * (vals[4] - vals[1]) + 3 * (vals[5] - vals[2]);
  244. }
  245. };
  246. template <class SrcPtr> struct ScharrYPtrSz : ScharrYPtr<SrcPtr>
  247. {
  248. int rows, cols;
  249. template <typename T>
  250. __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
  251. {
  252. gridCopy(*this, dst, stream);
  253. }
  254. };
  255. template <class SrcPtr>
  256. __host__ ScharrYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> scharrYPtr(const SrcPtr& src)
  257. {
  258. ScharrYPtrSz<typename PtrTraits<SrcPtr>::ptr_type> s;
  259. s.src = shrinkPtr(src);
  260. s.rows = getRows(src);
  261. s.cols = getCols(src);
  262. return s;
  263. }
  264. template <class SrcPtr> struct PtrTraits< ScharrYPtrSz<SrcPtr> > : PtrTraitsBase<ScharrYPtrSz<SrcPtr>, ScharrYPtr<SrcPtr> >
  265. {
  266. };
  267. // laplacian
  268. template <int ksize, class SrcPtr> struct LaplacianPtr;
  269. template <class SrcPtr> struct LaplacianPtr<1, SrcPtr>
  270. {
  271. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  272. typedef int index_type;
  273. SrcPtr src;
  274. __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
  275. {
  276. typename PtrTraits<SrcPtr>::value_type vals[5] =
  277. {
  278. src(y - 1, x),
  279. src(y, x - 1), src(y , x), src(y, x + 1),
  280. src(y + 1, x)
  281. };
  282. return (vals[0] + vals[1] + vals[3] + vals[4]) - 4 * vals[2];
  283. }
  284. };
  285. template <class SrcPtr> struct LaplacianPtr<3, SrcPtr>
  286. {
  287. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  288. typedef int index_type;
  289. SrcPtr src;
  290. __device__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
  291. {
  292. typename PtrTraits<SrcPtr>::value_type vals[5] =
  293. {
  294. src(y - 1, x - 1), src(y - 1, x + 1),
  295. src(y, x),
  296. src(y + 1, x - 1), src(y + 1, x + 1)
  297. };
  298. return 2 * (vals[0] + vals[1] + vals[3] + vals[4]) - 8 * vals[2];
  299. }
  300. };
  301. template <int ksize, class SrcPtr> struct LaplacianPtrSz : LaplacianPtr<ksize, SrcPtr>
  302. {
  303. int rows, cols;
  304. template <typename T>
  305. __host__ void assignTo(GpuMat_<T>& dst, Stream& stream = Stream::Null()) const
  306. {
  307. gridCopy(*this, dst, stream);
  308. }
  309. };
  310. template <int ksize, class SrcPtr>
  311. __host__ LaplacianPtrSz<ksize, typename PtrTraits<SrcPtr>::ptr_type> laplacianPtr(const SrcPtr& src)
  312. {
  313. LaplacianPtrSz<ksize, typename PtrTraits<SrcPtr>::ptr_type> ptr;
  314. ptr.src = shrinkPtr(src);
  315. ptr.rows = getRows(src);
  316. ptr.cols = getCols(src);
  317. return ptr;
  318. }
  319. template <int ksize, class SrcPtr> struct PtrTraits< LaplacianPtrSz<ksize, SrcPtr> > : PtrTraitsBase<LaplacianPtrSz<ksize, SrcPtr>, LaplacianPtr<ksize, SrcPtr> >
  320. {
  321. };
  322. //! @}
  323. }}
  324. #endif