convolve.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. // Copyright 2019 Miral Shah <miralshah2211@gmail.com>
  4. //
  5. // Distributed under the Boost Software License, Version 1.0
  6. // See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt
  8. //
  9. #ifndef BOOST_GIL_EXTENSION_NUMERIC_CONVOLVE_HPP
  10. #define BOOST_GIL_EXTENSION_NUMERIC_CONVOLVE_HPP
  11. #include <boost/gil/extension/numeric/algorithm.hpp>
  12. #include <boost/gil/extension/numeric/kernel.hpp>
  13. #include <boost/gil/extension/numeric/pixel_numeric_operations.hpp>
  14. #include <boost/gil/algorithm.hpp>
  15. #include <boost/gil/image_view_factory.hpp>
  16. #include <boost/gil/metafunctions.hpp>
  17. #include <boost/assert.hpp>
  18. #include <algorithm>
  19. #include <cstddef>
  20. #include <functional>
  21. #include <type_traits>
  22. #include <vector>
  23. namespace boost { namespace gil {
  24. // 2D spatial seperable convolutions and cross-correlations
  25. namespace detail {
  26. /// \brief Compute the cross-correlation of 1D kernel with the rows of an image
  27. /// \tparam PixelAccum - TODO
  28. /// \tparam SrcView Models ImageViewConcept
  29. /// \tparam Kernel - TODO
  30. /// \tparam DstView Models MutableImageViewConcept
  31. /// \tparam Correlator - TODO
  32. /// \param src_view
  33. /// \param kernel - TODO
  34. /// \param dst_view Destination where new computed values of pixels are assigned to
  35. /// \param option - TODO
  36. /// \param correlator - TODO
  37. template
  38. <
  39. typename PixelAccum,
  40. typename SrcView,
  41. typename Kernel,
  42. typename DstView,
  43. typename Correlator
  44. >
  45. void correlate_rows_impl(
  46. SrcView const& src_view,
  47. Kernel const& kernel,
  48. DstView const& dst_view,
  49. boundary_option option,
  50. Correlator correlator)
  51. {
  52. BOOST_ASSERT(src_view.dimensions() == dst_view.dimensions());
  53. BOOST_ASSERT(kernel.size() != 0);
  54. if(kernel.size() == 1)
  55. {
  56. // Reduces to a multiplication
  57. view_multiplies_scalar<PixelAccum>(src_view, *kernel.begin(), dst_view);
  58. return;
  59. }
  60. using src_pixel_ref_t = typename pixel_proxy<typename SrcView::value_type>::type;
  61. using dst_pixel_ref_t = typename pixel_proxy<typename DstView::value_type>::type;
  62. using x_coord_t = typename SrcView::x_coord_t;
  63. using y_coord_t = typename SrcView::y_coord_t;
  64. x_coord_t const width = src_view.width();
  65. y_coord_t const height = src_view.height();
  66. if (width == 0)
  67. return;
  68. PixelAccum acc_zero;
  69. pixel_zeros_t<PixelAccum>()(acc_zero);
  70. if (option == boundary_option::output_ignore || option == boundary_option::output_zero)
  71. {
  72. typename DstView::value_type dst_zero;
  73. pixel_assigns_t<PixelAccum, dst_pixel_ref_t>()(acc_zero, dst_zero);
  74. if (width < static_cast<x_coord_t>(kernel.size()))
  75. {
  76. if (option == boundary_option::output_zero)
  77. fill_pixels(dst_view, dst_zero);
  78. }
  79. else
  80. {
  81. std::vector<PixelAccum> buffer(width);
  82. for (y_coord_t y = 0; y < height; ++y)
  83. {
  84. assign_pixels(src_view.row_begin(y), src_view.row_end(y), &buffer.front());
  85. typename DstView::x_iterator it_dst = dst_view.row_begin(y);
  86. if (option == boundary_option::output_zero)
  87. std::fill_n(it_dst, kernel.left_size(), dst_zero);
  88. it_dst += kernel.left_size();
  89. correlator(&buffer.front(), &buffer.front() + width + 1 - kernel.size(),
  90. kernel.begin(), it_dst);
  91. it_dst += width + 1 - kernel.size();
  92. if (option == boundary_option::output_zero)
  93. std::fill_n(it_dst, kernel.right_size(), dst_zero);
  94. }
  95. }
  96. }
  97. else
  98. {
  99. std::vector<PixelAccum> buffer(width + kernel.size() - 1);
  100. for (y_coord_t y = 0; y < height; ++y)
  101. {
  102. PixelAccum *it_buffer = &buffer.front();
  103. if (option == boundary_option::extend_padded)
  104. {
  105. assign_pixels(
  106. src_view.row_begin(y) - kernel.left_size(),
  107. src_view.row_end(y) + kernel.right_size(),
  108. it_buffer);
  109. }
  110. else if (option == boundary_option::extend_zero)
  111. {
  112. std::fill_n(it_buffer, kernel.left_size(), acc_zero);
  113. it_buffer += kernel.left_size();
  114. assign_pixels(src_view.row_begin(y), src_view.row_end(y), it_buffer);
  115. it_buffer += width;
  116. std::fill_n(it_buffer, kernel.right_size(), acc_zero);
  117. }
  118. else if (option == boundary_option::extend_constant)
  119. {
  120. PixelAccum filler;
  121. pixel_assigns_t<src_pixel_ref_t, PixelAccum>()(*src_view.row_begin(y), filler);
  122. std::fill_n(it_buffer, kernel.left_size(), filler);
  123. it_buffer += kernel.left_size();
  124. assign_pixels(src_view.row_begin(y), src_view.row_end(y), it_buffer);
  125. it_buffer += width;
  126. pixel_assigns_t<src_pixel_ref_t, PixelAccum>()(src_view.row_end(y)[-1], filler);
  127. std::fill_n(it_buffer, kernel.right_size(), filler);
  128. }
  129. correlator(
  130. &buffer.front(), &buffer.front() + width,
  131. kernel.begin(),
  132. dst_view.row_begin(y));
  133. }
  134. }
  135. }
  136. template <typename PixelAccum>
  137. class correlator_n
  138. {
  139. public:
  140. correlator_n(std::size_t size) : size_(size) {}
  141. template <typename SrcIterator, typename KernelIterator, typename DstIterator>
  142. void operator()(
  143. SrcIterator src_begin,
  144. SrcIterator src_end,
  145. KernelIterator kernel_begin,
  146. DstIterator dst_begin)
  147. {
  148. correlate_pixels_n<PixelAccum>(src_begin, src_end, kernel_begin, size_, dst_begin);
  149. }
  150. private:
  151. std::size_t size_{0};
  152. };
  153. template <std::size_t Size, typename PixelAccum>
  154. struct correlator_k
  155. {
  156. template <typename SrcIterator, typename KernelIterator, typename DstIterator>
  157. void operator()(
  158. SrcIterator src_begin,
  159. SrcIterator src_end,
  160. KernelIterator kernel_begin,
  161. DstIterator dst_begin)
  162. {
  163. correlate_pixels_k<Size, PixelAccum>(src_begin, src_end, kernel_begin, dst_begin);
  164. }
  165. };
  166. } // namespace detail
  167. /// \ingroup ImageAlgorithms
  168. /// \brief Correlate 1D variable-size kernel along the rows of image
  169. /// \tparam PixelAccum TODO
  170. /// \tparam SrcView Models ImageViewConcept
  171. /// \tparam Kernel TODO
  172. /// \tparam DstView Models MutableImageViewConcept
  173. template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
  174. BOOST_FORCEINLINE
  175. void correlate_rows(
  176. SrcView const& src_view,
  177. Kernel const& kernel,
  178. DstView const& dst_view,
  179. boundary_option option = boundary_option::extend_zero)
  180. {
  181. detail::correlate_rows_impl<PixelAccum>(
  182. src_view, kernel, dst_view, option, detail::correlator_n<PixelAccum>(kernel.size()));
  183. }
  184. /// \ingroup ImageAlgorithms
  185. /// \brief Correlate 1D variable-size kernel along the columns of image
  186. /// \tparam PixelAccum TODO
  187. /// \tparam SrcView Models ImageViewConcept
  188. /// \tparam Kernel TODO
  189. /// \tparam DstView Models MutableImageViewConcept
  190. template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
  191. BOOST_FORCEINLINE
  192. void correlate_cols(
  193. SrcView const& src_view,
  194. Kernel const& kernel,
  195. DstView const& dst_view,
  196. boundary_option option = boundary_option::extend_zero)
  197. {
  198. correlate_rows<PixelAccum>(
  199. transposed_view(src_view), kernel, transposed_view(dst_view), option);
  200. }
  201. /// \ingroup ImageAlgorithms
  202. /// \brief Convolve 1D variable-size kernel along the rows of image
  203. /// \tparam PixelAccum TODO
  204. /// \tparam SrcView Models ImageViewConcept
  205. /// \tparam Kernel TODO
  206. /// \tparam DstView Models MutableImageViewConcept
  207. template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
  208. BOOST_FORCEINLINE
  209. void convolve_rows(
  210. SrcView const& src_view,
  211. Kernel const& kernel,
  212. DstView const& dst_view,
  213. boundary_option option = boundary_option::extend_zero)
  214. {
  215. correlate_rows<PixelAccum>(src_view, reverse_kernel(kernel), dst_view, option);
  216. }
  217. /// \ingroup ImageAlgorithms
  218. /// \brief Convolve 1D variable-size kernel along the columns of image
  219. /// \tparam PixelAccum TODO
  220. /// \tparam SrcView Models ImageViewConcept
  221. /// \tparam Kernel TODO
  222. /// \tparam DstView Models MutableImageViewConcept
  223. template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
  224. BOOST_FORCEINLINE
  225. void convolve_cols(
  226. SrcView const& src_view,
  227. Kernel const& kernel,
  228. DstView const& dst_view,
  229. boundary_option option = boundary_option::extend_zero)
  230. {
  231. convolve_rows<PixelAccum>(
  232. transposed_view(src_view), kernel, transposed_view(dst_view), option);
  233. }
  234. /// \ingroup ImageAlgorithms
  235. /// \brief Correlate 1D fixed-size kernel along the rows of image
  236. /// \tparam PixelAccum TODO
  237. /// \tparam SrcView Models ImageViewConcept
  238. /// \tparam Kernel TODO
  239. /// \tparam DstView Models MutableImageViewConcept
  240. template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
  241. BOOST_FORCEINLINE
  242. void correlate_rows_fixed(
  243. SrcView const& src_view,
  244. Kernel const& kernel,
  245. DstView const& dst_view,
  246. boundary_option option = boundary_option::extend_zero)
  247. {
  248. using correlator = detail::correlator_k<Kernel::static_size, PixelAccum>;
  249. detail::correlate_rows_impl<PixelAccum>(src_view, kernel, dst_view, option, correlator{});
  250. }
  251. /// \ingroup ImageAlgorithms
  252. /// \brief Correlate 1D fixed-size kernel along the columns of image
  253. /// \tparam PixelAccum TODO
  254. /// \tparam SrcView Models ImageViewConcept
  255. /// \tparam Kernel TODO
  256. /// \tparam DstView Models MutableImageViewConcept
  257. template <typename PixelAccum,typename SrcView,typename Kernel,typename DstView>
  258. BOOST_FORCEINLINE
  259. void correlate_cols_fixed(
  260. SrcView const& src_view,
  261. Kernel const& kernel,
  262. DstView const& dst_view,
  263. boundary_option option = boundary_option::extend_zero)
  264. {
  265. correlate_rows_fixed<PixelAccum>(
  266. transposed_view(src_view), kernel, transposed_view(dst_view), option);
  267. }
  268. /// \ingroup ImageAlgorithms
  269. /// \brief Convolve 1D fixed-size kernel along the rows of image
  270. /// \tparam PixelAccum TODO
  271. /// \tparam SrcView Models ImageViewConcept
  272. /// \tparam Kernel TODO
  273. /// \tparam DstView Models MutableImageViewConcept
  274. template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
  275. BOOST_FORCEINLINE
  276. void convolve_rows_fixed(
  277. SrcView const& src_view,
  278. Kernel const& kernel,
  279. DstView const& dst_view,
  280. boundary_option option = boundary_option::extend_zero)
  281. {
  282. correlate_rows_fixed<PixelAccum>(src_view, reverse_kernel(kernel), dst_view, option);
  283. }
  284. /// \ingroup ImageAlgorithms
  285. /// \brief Convolve 1D fixed-size kernel along the columns of image
  286. /// \tparam PixelAccum TODO
  287. /// \tparam SrcView Models ImageViewConcept
  288. /// \tparam Kernel TODO
  289. /// \tparam DstView Models MutableImageViewConcept
  290. template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
  291. BOOST_FORCEINLINE
  292. void convolve_cols_fixed(
  293. SrcView const& src_view,
  294. Kernel const& kernel,
  295. DstView const& dst_view,
  296. boundary_option option = boundary_option::extend_zero)
  297. {
  298. convolve_rows_fixed<PixelAccum>(
  299. transposed_view(src_view), kernel, transposed_view(dst_view), option);
  300. }
  301. namespace detail
  302. {
  303. /// \ingroup ImageAlgorithms
  304. /// \brief Convolve 1D variable-size kernel along both rows and columns of image
  305. /// \tparam PixelAccum TODO
  306. /// \tparam SrcView Models ImageViewConcept
  307. /// \tparam Kernel TODO
  308. /// \tparam DstView Models MutableImageViewConcept
  309. template <typename PixelAccum, typename SrcView, typename Kernel, typename DstView>
  310. BOOST_FORCEINLINE
  311. void convolve_1d(
  312. SrcView const& src_view,
  313. Kernel const& kernel,
  314. DstView const& dst_view,
  315. boundary_option option = boundary_option::extend_zero)
  316. {
  317. convolve_rows<PixelAccum>(src_view, kernel, dst_view, option);
  318. convolve_cols<PixelAccum>(dst_view, kernel, dst_view, option);
  319. }
  320. template <typename SrcView, typename DstView, typename Kernel>
  321. void convolve_2d_impl(SrcView const& src_view, DstView const& dst_view, Kernel const& kernel)
  322. {
  323. int flip_ker_row, flip_ker_col, row_boundary, col_boundary;
  324. float aux_total;
  325. for (std::ptrdiff_t view_row = 0; view_row < src_view.height(); ++view_row)
  326. {
  327. for (std::ptrdiff_t view_col = 0; view_col < src_view.width(); ++view_col)
  328. {
  329. aux_total = 0.0f;
  330. for (std::size_t kernel_row = 0; kernel_row < kernel.size(); ++kernel_row)
  331. {
  332. flip_ker_row = kernel.size() - 1 - kernel_row; // row index of flipped kernel
  333. for (std::size_t kernel_col = 0; kernel_col < kernel.size(); ++kernel_col)
  334. {
  335. flip_ker_col = kernel.size() - 1 - kernel_col; // column index of flipped kernel
  336. // index of input signal, used for checking boundary
  337. row_boundary = view_row + (kernel.center_y() - flip_ker_row);
  338. col_boundary = view_col + (kernel.center_x() - flip_ker_col);
  339. // ignore input samples which are out of bound
  340. if (row_boundary >= 0 && row_boundary < src_view.height() &&
  341. col_boundary >= 0 && col_boundary < src_view.width())
  342. {
  343. aux_total +=
  344. src_view(col_boundary, row_boundary) *
  345. kernel.at(flip_ker_row, flip_ker_col);
  346. }
  347. }
  348. }
  349. dst_view(view_col, view_row) = aux_total;
  350. }
  351. }
  352. }
  353. /// \ingroup ImageAlgorithms
  354. /// \brief convolve_2d can only use convolve_option_extend_zero as convolve_boundary_option
  355. /// this is the default option and cannot be changed for now
  356. /// (In future there are plans to improve the algorithm and allow user to use other options as well)
  357. /// \tparam SrcView Models ImageViewConcept
  358. /// \tparam Kernel TODO
  359. /// \tparam DstView Models MutableImageViewConcept
  360. template <typename SrcView, typename DstView, typename Kernel>
  361. void convolve_2d(SrcView const& src_view, Kernel const& kernel, DstView const& dst_view)
  362. {
  363. BOOST_ASSERT(src_view.dimensions() == dst_view.dimensions());
  364. BOOST_ASSERT(kernel.size() != 0);
  365. gil_function_requires<ImageViewConcept<SrcView>>();
  366. gil_function_requires<MutableImageViewConcept<DstView>>();
  367. static_assert(color_spaces_are_compatible
  368. <
  369. typename color_space_type<SrcView>::type,
  370. typename color_space_type<DstView>::type
  371. >::value, "Source and destination views must have pixels with the same color space");
  372. for (std::size_t i = 0; i < src_view.num_channels(); i++)
  373. {
  374. detail::convolve_2d_impl(
  375. nth_channel_view(src_view, i),
  376. nth_channel_view(dst_view, i),
  377. kernel
  378. );
  379. }
  380. }
  381. }}} // namespace boost::gil::detail
  382. #endif