simple_kernels.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // Copyright 2019 Olzhas Zhumabek <anonymous.from.applecity@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 <boost/gil/image_processing/numeric.hpp>
  9. #include <boost/gil/image.hpp>
  10. #include <boost/gil/image_view.hpp>
  11. #include <boost/gil/typedefs.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. namespace gil = boost::gil;
  14. void test_normalized_mean_generation()
  15. {
  16. auto kernel = gil::generate_normalized_mean(5);
  17. for (const auto& cell: kernel)
  18. {
  19. const auto expected_value = static_cast<float>(1 / 25.f);
  20. BOOST_TEST(cell == expected_value);
  21. }
  22. }
  23. void test_unnormalized_mean_generation()
  24. {
  25. auto kernel = gil::generate_unnormalized_mean(5);
  26. for (const auto& cell: kernel)
  27. {
  28. BOOST_TEST(cell == 1.0f);
  29. }
  30. }
  31. void test_gaussian_kernel_generation()
  32. {
  33. auto kernel = boost::gil::generate_gaussian_kernel(7, 0.84089642);
  34. const float expected_values[7][7] =
  35. {
  36. {0.00000067f, 0.00002292f, 0.00019117f, 0.00038771f, 0.00019117f, 0.00002292f, 0.00000067f},
  37. {0.00002292f, 0.00078633f, 0.00655965f, 0.01330373f, 0.00655965f, 0.00078633f, 0.00002292f},
  38. {0.00019117f, 0.00655965f, 0.05472157f, 0.11098164f, 0.05472157f, 0.00655965f, 0.00019117f},
  39. {0.00038771f, 0.01330373f, 0.11098164f, 0.25508352f, 0.11098164f, 0.01330373f, 0.00038711f},
  40. {0.00019117f, 0.00655965f, 0.05472157f, 0.11098164f, 0.05472157f, 0.00655965f, 0.00019117f},
  41. {0.00002292f, 0.00078633f, 0.00655965f, 0.01330373f, 0.00655965f, 0.00078633f, 0.00002292f},
  42. {0.00000067f, 0.00002292f, 0.00019117f, 0.00038771f, 0.00019117f, 0.00002292f, 0.00000067f}
  43. };
  44. for (gil::gray32f_view_t::coord_t y = 0; static_cast<std::size_t>(y) < kernel.size(); ++y)
  45. {
  46. for (gil::gray32f_view_t::coord_t x = 0; static_cast<std::size_t>(x) < kernel.size(); ++x)
  47. {
  48. auto output = kernel.at(static_cast<std::size_t>(x), static_cast<std::size_t>(y));
  49. auto expected = expected_values[y][x];
  50. auto percent_difference = std::ceil(std::abs(expected - output) / expected);
  51. BOOST_TEST(percent_difference < 5);
  52. }
  53. }
  54. }
  55. int main()
  56. {
  57. test_normalized_mean_generation();
  58. test_unnormalized_mean_generation();
  59. test_gaussian_kernel_generation();
  60. return boost::report_errors();
  61. }