9
3

convolve_2d.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // Copyright 2019 Miral Shah <miralshah2211@gmail.com>
  3. //
  4. // Use, modification and distribution are subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #include <cstddef>
  9. #include <boost/gil.hpp>
  10. #include <boost/gil/extension/numeric/convolve.hpp>
  11. #define BOOST_TEST_MODULE test_ext_convolve_2d
  12. #include "unit_test.hpp"
  13. namespace gil = boost::gil;
  14. std::uint8_t img[] =
  15. {
  16. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  17. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  18. 0, 0, 255, 0, 0, 0, 255, 0, 0,
  19. 0, 0, 0, 255, 0, 255, 0, 0, 0,
  20. 0, 0, 0, 0, 255, 0, 0, 0, 0,
  21. 0, 0, 0, 255, 0, 255, 0, 0, 0,
  22. 0, 0, 255, 0, 0, 0, 255, 0, 0,
  23. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0
  25. };
  26. std::uint8_t output[] =
  27. {
  28. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29. 0, 28, 28, 28, 0, 28, 28, 28, 0,
  30. 0, 28, 56, 56, 56, 56, 56, 28, 0,
  31. 0, 28, 56, 85, 85, 85, 56, 28, 0,
  32. 0, 0, 56, 85, 141, 85, 56, 0, 0,
  33. 0, 28, 56, 85, 85, 85, 56, 28, 0,
  34. 0, 28, 56, 56, 56, 56, 56, 28, 0,
  35. 0, 28, 28, 28, 0, 28, 28, 28, 0,
  36. 0, 0, 0, 0, 0, 0, 0, 0, 0
  37. };
  38. BOOST_AUTO_TEST_SUITE(convolve_2d)
  39. BOOST_AUTO_TEST_CASE(convolve_2d_with_normalized_mean_filter)
  40. {
  41. gil::gray8c_view_t src_view =
  42. gil::interleaved_view(9, 9, reinterpret_cast<const gil::gray8_pixel_t*>(img), 9);
  43. gil::image<gil::gray8_pixel_t> temp_img(src_view.width(), src_view.height());
  44. typename gil::image<gil::gray8_pixel_t>::view_t temp_view = view(temp_img);
  45. gil::gray8_view_t dst_view(temp_view);
  46. std::vector<float> v(9, 1.0f / 9.0f);
  47. gil::detail::kernel_2d<float> kernel(v.begin(), v.size(), 1, 1);
  48. gil::detail::convolve_2d(src_view, kernel, dst_view);
  49. gil::gray8c_view_t out_view =
  50. gil::interleaved_view(9, 9, reinterpret_cast<const gil::gray8_pixel_t*>(output), 9);
  51. BOOST_TEST(gil::equal_pixels(out_view, dst_view));
  52. }
  53. BOOST_AUTO_TEST_SUITE_END()