interpolation.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_INTERPOLATION_HPP
  45. #define OPENCV_CUDEV_PTR2D_INTERPOLATION_HPP
  46. #include "../common.hpp"
  47. #include "../util/vec_traits.hpp"
  48. #include "../util/saturate_cast.hpp"
  49. #include "../util/type_traits.hpp"
  50. #include "../util/limits.hpp"
  51. #include "traits.hpp"
  52. namespace cv { namespace cudev {
  53. //! @addtogroup cudev
  54. //! @{
  55. // Nearest
  56. template <class SrcPtr> struct NearestInterPtr
  57. {
  58. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  59. typedef float index_type;
  60. SrcPtr src;
  61. __device__ __forceinline__ typename PtrTraits<SrcPtr>::value_type operator ()(float y, float x) const
  62. {
  63. return src(__float2int_rn(y), __float2int_rn(x));
  64. }
  65. };
  66. template <class SrcPtr> struct NearestInterPtrSz : NearestInterPtr<SrcPtr>
  67. {
  68. int rows, cols;
  69. };
  70. template <class SrcPtr>
  71. __host__ NearestInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> interNearest(const SrcPtr& src)
  72. {
  73. NearestInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> i;
  74. i.src = shrinkPtr(src);
  75. i.rows = getRows(src);
  76. i.cols = getCols(src);
  77. return i;
  78. }
  79. template <class SrcPtr> struct PtrTraits< NearestInterPtrSz<SrcPtr> > : PtrTraitsBase<NearestInterPtrSz<SrcPtr>, NearestInterPtr<SrcPtr> >
  80. {
  81. };
  82. // Linear
  83. template <typename SrcPtr> struct LinearInterPtr
  84. {
  85. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  86. typedef float index_type;
  87. SrcPtr src;
  88. __device__ typename PtrTraits<SrcPtr>::value_type operator ()(float y, float x) const
  89. {
  90. typedef typename PtrTraits<SrcPtr>::value_type src_type;
  91. typedef typename VecTraits<src_type>::elem_type src_elem_type;
  92. typedef typename LargerType<float, src_elem_type>::type work_elem_type;
  93. typedef typename MakeVec<work_elem_type, VecTraits<src_type>::cn>::type work_type;
  94. work_type out = VecTraits<work_type>::all(0);
  95. const int x1 = __float2int_rd(x);
  96. const int y1 = __float2int_rd(y);
  97. const int x2 = x1 + 1;
  98. const int y2 = y1 + 1;
  99. typename PtrTraits<SrcPtr>::value_type src_reg = src(y1, x1);
  100. out = out + src_reg * static_cast<work_elem_type>((x2 - x) * (y2 - y));
  101. src_reg = src(y1, x2);
  102. out = out + src_reg * static_cast<work_elem_type>((x - x1) * (y2 - y));
  103. src_reg = src(y2, x1);
  104. out = out + src_reg * static_cast<work_elem_type>((x2 - x) * (y - y1));
  105. src_reg = src(y2, x2);
  106. out = out + src_reg * static_cast<work_elem_type>((x - x1) * (y - y1));
  107. return saturate_cast<typename PtrTraits<SrcPtr>::value_type>(out);
  108. }
  109. };
  110. template <class SrcPtr> struct LinearInterPtrSz : LinearInterPtr<SrcPtr>
  111. {
  112. int rows, cols;
  113. };
  114. template <class SrcPtr>
  115. __host__ LinearInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> interLinear(const SrcPtr& src)
  116. {
  117. LinearInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> i;
  118. i.src = shrinkPtr(src);
  119. i.rows = getRows(src);
  120. i.cols = getCols(src);
  121. return i;
  122. }
  123. template <class SrcPtr> struct PtrTraits< LinearInterPtrSz<SrcPtr> > : PtrTraitsBase<LinearInterPtrSz<SrcPtr>, LinearInterPtr<SrcPtr> >
  124. {
  125. };
  126. // Cubic
  127. template <typename SrcPtr> struct CubicInterPtr
  128. {
  129. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  130. typedef float index_type;
  131. SrcPtr src;
  132. __device__ static float bicubicCoeff(float x_)
  133. {
  134. float x = ::fabsf(x_);
  135. if (x <= 1.0f)
  136. {
  137. return x * x * (1.5f * x - 2.5f) + 1.0f;
  138. }
  139. else if (x < 2.0f)
  140. {
  141. return x * (x * (-0.5f * x + 2.5f) - 4.0f) + 2.0f;
  142. }
  143. else
  144. {
  145. return 0.0f;
  146. }
  147. }
  148. __device__ typename PtrTraits<SrcPtr>::value_type operator ()(float y, float x) const
  149. {
  150. typedef typename PtrTraits<SrcPtr>::value_type src_type;
  151. typedef typename VecTraits<src_type>::elem_type src_elem_type;
  152. typedef typename LargerType<float, src_elem_type>::type work_elem_type;
  153. typedef typename MakeVec<work_elem_type, VecTraits<src_type>::cn>::type work_type;
  154. const float xmin = ::ceilf(x - 2.0f);
  155. const float xmax = ::floorf(x + 2.0f);
  156. const float ymin = ::ceilf(y - 2.0f);
  157. const float ymax = ::floorf(y + 2.0f);
  158. work_type sum = VecTraits<work_type>::all(0);
  159. float wsum = 0.0f;
  160. for (float cy = ymin; cy <= ymax; cy += 1.0f)
  161. {
  162. for (float cx = xmin; cx <= xmax; cx += 1.0f)
  163. {
  164. typename PtrTraits<SrcPtr>::value_type src_reg = src(__float2int_rd(cy), __float2int_rd(cx));
  165. const float w = bicubicCoeff(x - cx) * bicubicCoeff(y - cy);
  166. sum = sum + static_cast<work_elem_type>(w) * src_reg;
  167. wsum += w;
  168. }
  169. }
  170. work_type res = (wsum > numeric_limits<float>::epsilon()) ? VecTraits<work_type>::all(0) : sum / static_cast<work_elem_type>(wsum);
  171. return saturate_cast<typename PtrTraits<SrcPtr>::value_type>(res);
  172. }
  173. };
  174. template <class SrcPtr> struct CubicInterPtrSz : CubicInterPtr<SrcPtr>
  175. {
  176. int rows, cols;
  177. };
  178. template <class SrcPtr>
  179. __host__ CubicInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> interCubic(const SrcPtr& src)
  180. {
  181. CubicInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> i;
  182. i.src = shrinkPtr(src);
  183. i.rows = getRows(src);
  184. i.cols = getCols(src);
  185. return i;
  186. }
  187. template <class SrcPtr> struct PtrTraits< CubicInterPtrSz<SrcPtr> > : PtrTraitsBase<CubicInterPtrSz<SrcPtr>, CubicInterPtr<SrcPtr> >
  188. {
  189. };
  190. // IntegerArea
  191. template <typename SrcPtr> struct IntegerAreaInterPtr
  192. {
  193. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  194. typedef float index_type;
  195. SrcPtr src;
  196. int area_width, area_height;
  197. __device__ typename PtrTraits<SrcPtr>::value_type operator ()(float y, float x) const
  198. {
  199. typedef typename PtrTraits<SrcPtr>::value_type src_type;
  200. typedef typename VecTraits<src_type>::elem_type src_elem_type;
  201. typedef typename LargerType<float, src_elem_type>::type work_elem_type;
  202. typedef typename MakeVec<work_elem_type, VecTraits<src_type>::cn>::type work_type;
  203. const int sx1 = __float2int_rd(x);
  204. const int sx2 = sx1 + area_width;
  205. const int sy1 = __float2int_rd(y);
  206. const int sy2 = sy1 + area_height;
  207. work_type out = VecTraits<work_type>::all(0);
  208. for (int dy = sy1; dy < sy2; ++dy)
  209. {
  210. for (int dx = sx1; dx < sx2; ++dx)
  211. {
  212. out = out + saturate_cast<work_type>(src(dy, dx));
  213. }
  214. }
  215. const work_elem_type scale = 1.0f / (area_width * area_height);
  216. return saturate_cast<typename PtrTraits<SrcPtr>::value_type>(out * scale);
  217. }
  218. };
  219. template <class SrcPtr> struct IntegerAreaInterPtrSz : IntegerAreaInterPtr<SrcPtr>
  220. {
  221. int rows, cols;
  222. };
  223. template <class SrcPtr>
  224. __host__ IntegerAreaInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> interArea(const SrcPtr& src, Size areaSize)
  225. {
  226. IntegerAreaInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> i;
  227. i.src = shrinkPtr(src);
  228. i.area_width = areaSize.width;
  229. i.area_height = areaSize.height;
  230. i.rows = getRows(src);
  231. i.cols = getCols(src);
  232. return i;
  233. }
  234. template <class SrcPtr> struct PtrTraits< IntegerAreaInterPtrSz<SrcPtr> > : PtrTraitsBase<IntegerAreaInterPtrSz<SrcPtr>, IntegerAreaInterPtr<SrcPtr> >
  235. {
  236. };
  237. // CommonArea
  238. template <typename SrcPtr> struct CommonAreaInterPtr
  239. {
  240. typedef typename PtrTraits<SrcPtr>::value_type value_type;
  241. typedef float index_type;
  242. SrcPtr src;
  243. float area_width, area_height;
  244. __device__ typename PtrTraits<SrcPtr>::value_type operator ()(float y, float x) const
  245. {
  246. typedef typename PtrTraits<SrcPtr>::value_type src_type;
  247. typedef typename VecTraits<src_type>::elem_type src_elem_type;
  248. typedef typename LargerType<float, src_elem_type>::type work_elem_type;
  249. typedef typename MakeVec<work_elem_type, VecTraits<src_type>::cn>::type work_type;
  250. const float fsx1 = x;
  251. const float fsx2 = fsx1 + area_width;
  252. const int sx1 = __float2int_rd(fsx1);
  253. const int sx2 = __float2int_ru(fsx2);
  254. const float fsy1 = y;
  255. const float fsy2 = fsy1 + area_height;
  256. const int sy1 = __float2int_rd(fsy1);
  257. const int sy2 = __float2int_ru(fsy2);
  258. work_type out = VecTraits<work_type>::all(0);
  259. for (int dy = sy1; dy < sy2; ++dy)
  260. {
  261. for (int dx = sx1; dx < sx2; ++dx)
  262. out = out + saturate_cast<work_type>(src(dy, dx));
  263. if (sx1 > fsx1)
  264. out = out + saturate_cast<work_type>(src(dy, sx1 - 1)) * static_cast<work_elem_type>(sx1 - fsx1);
  265. if (sx2 < fsx2)
  266. out = out + saturate_cast<work_type>(src(dy, sx2)) * static_cast<work_elem_type>(fsx2 - sx2);
  267. }
  268. if (sy1 > fsy1)
  269. {
  270. for (int dx = sx1; dx < sx2; ++dx)
  271. out = out + saturate_cast<work_type>(src(sy1 - 1, dx)) * static_cast<work_elem_type>(sy1 - fsy1);
  272. }
  273. if (sy2 < fsy2)
  274. {
  275. for (int dx = sx1; dx < sx2; ++dx)
  276. out = out + saturate_cast<work_type>(src(sy2, dx)) * static_cast<work_elem_type>(fsy2 - sy2);
  277. }
  278. if ((sy1 > fsy1) && (sx1 > fsx1))
  279. out = out + saturate_cast<work_type>(src(sy1 - 1, sx1 - 1)) * static_cast<work_elem_type>((sy1 - fsy1) * (sx1 - fsx1));
  280. if ((sy1 > fsy1) && (sx2 < fsx2))
  281. out = out + saturate_cast<work_type>(src(sy1 - 1, sx2)) * static_cast<work_elem_type>((sy1 - fsy1) * (fsx2 - sx2));
  282. if ((sy2 < fsy2) && (sx2 < fsx2))
  283. out = out + saturate_cast<work_type>(src(sy2, sx2)) * static_cast<work_elem_type>((fsy2 - sy2) * (fsx2 - sx2));
  284. if ((sy2 < fsy2) && (sx1 > fsx1))
  285. out = out + saturate_cast<work_type>(src(sy2, sx1 - 1)) * static_cast<work_elem_type>((fsy2 - sy2) * (sx1 - fsx1));
  286. const work_elem_type scale = 1.0f / (area_width * area_height);
  287. return saturate_cast<typename PtrTraits<SrcPtr>::value_type>(out * scale);
  288. }
  289. };
  290. template <class SrcPtr> struct CommonAreaInterPtrSz : CommonAreaInterPtr<SrcPtr>
  291. {
  292. int rows, cols;
  293. };
  294. template <class SrcPtr>
  295. __host__ CommonAreaInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> interArea(const SrcPtr& src, Size2f areaSize)
  296. {
  297. CommonAreaInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> i;
  298. i.src = shrinkPtr(src);
  299. i.area_width = areaSize.width;
  300. i.area_height = areaSize.height;
  301. i.rows = getRows(src);
  302. i.cols = getCols(src);
  303. return i;
  304. }
  305. template <class SrcPtr> struct PtrTraits< CommonAreaInterPtrSz<SrcPtr> > : PtrTraitsBase<CommonAreaInterPtrSz<SrcPtr>, CommonAreaInterPtr<SrcPtr> >
  306. {
  307. };
  308. //! @}
  309. }}
  310. #endif