convolve.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // Copyright 2019 Mateusz Loskot <mateusz at loskot dot net>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #include <boost/gil.hpp>
  9. #include <boost/gil/extension/numeric/convolve.hpp>
  10. #include <tuple>
  11. #include <type_traits>
  12. #define BOOST_TEST_MODULE test_ext_numeric_colvolve_2d
  13. #include "unit_test.hpp"
  14. #include "unit_test_utility.hpp"
  15. #include "test_fixture.hpp"
  16. #include "core/image/test_fixture.hpp"
  17. namespace gil = boost::gil;
  18. namespace fixture = boost::gil::test::fixture;
  19. BOOST_AUTO_TEST_SUITE(convolve_1d)
  20. BOOST_AUTO_TEST_CASE_TEMPLATE(image_1x1_kernel_1x1_identity, Image, fixture::image_types)
  21. {
  22. auto const img = fixture::create_image<Image>(1, 1, 7);
  23. Image img_out(img);
  24. using pixel_t = typename Image::value_type;
  25. using channel_t = typename gil::channel_type<pixel_t>::type;
  26. auto const kernel = fixture::create_kernel<channel_t>({1});
  27. gil::detail::convolve_1d<pixel_t>(const_view(img_out), kernel, view(img_out));
  28. // 1x1 kernel reduces convolution to multiplication
  29. BOOST_TEST(gil::const_view(img).front() == gil::const_view(img_out).front());
  30. }
  31. BOOST_AUTO_TEST_CASE_TEMPLATE(image_1x1_kernel_3x3_identity, Image, fixture::image_types)
  32. {
  33. auto const img = fixture::create_image<Image>(1, 1, 7);
  34. Image img_out(img);
  35. using pixel_t = typename Image::value_type;
  36. using channel_t = typename gil::channel_type<pixel_t>::type;
  37. auto const kernel = fixture::create_kernel<channel_t>({0, 0, 0, 0, 1, 0, 0, 0, 0});
  38. gil::detail::convolve_1d<pixel_t>(const_view(img_out), kernel, view(img_out));
  39. BOOST_TEST(gil::const_view(img).front() == gil::const_view(img_out).front());
  40. }
  41. BOOST_AUTO_TEST_CASE_TEMPLATE(image_3x3_kernel_3x3_identity, Image, fixture::image_types)
  42. {
  43. using pixel_t = typename Image::value_type;
  44. using channel_t = typename gil::channel_type<pixel_t>::type;
  45. auto const img = fixture::generate_image<Image>(3, 3, fixture::random_value<channel_t>{});
  46. Image img_out(img);
  47. auto const kernel = fixture::create_kernel<channel_t>({0, 0, 0, 0, 1, 0, 0, 0, 0});
  48. gil::detail::convolve_1d<pixel_t>(const_view(img_out), kernel, view(img_out));
  49. BOOST_TEST(gil::equal_pixels(gil::const_view(img), gil::const_view(img_out)));
  50. }
  51. BOOST_AUTO_TEST_CASE_TEMPLATE(image_5x5_kernel_3x3_identity, Image, fixture::image_types)
  52. {
  53. using pixel_t = typename Image::value_type;
  54. using channel_t = typename gil::channel_type<pixel_t>::type;
  55. auto const img = fixture::generate_image<Image>(5, 5, fixture::random_value<channel_t>{});
  56. Image img_out(img);
  57. auto const kernel = fixture::create_kernel<channel_t>({0, 0, 0, 0, 1, 0, 0, 0, 0});
  58. gil::detail::convolve_1d<pixel_t>(const_view(img_out), kernel, view(img_out));
  59. // TODO: Test different boundary options
  60. BOOST_TEST(gil::equal_pixels(gil::const_view(img), gil::const_view(img_out)));
  61. }
  62. BOOST_AUTO_TEST_SUITE_END()