convolve.hpp 18 KB

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