threshold_otsu.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <boost/gil/image_processing/threshold.hpp>
  9. #include <boost/gil/image_view.hpp>
  10. #include <boost/gil/algorithm.hpp>
  11. #include <boost/gil/gray.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. namespace gil = boost::gil;
  14. int height = 2;
  15. int width = 2;
  16. gil::gray8_image_t original_gray(width, height), otsu_gray(width, height),
  17. expected_gray(width, height);
  18. gil::rgb8_image_t original_rgb(width, height), otsu_rgb(width, height), expected_rgb(width, height);
  19. void fill_gray()
  20. {
  21. gil::view(original_gray)(0, 0) = gil::gray8_pixel_t(56);
  22. gil::view(original_gray)(1, 0) = gil::gray8_pixel_t(89);
  23. gil::view(original_gray)(0, 1) = gil::gray8_pixel_t(206);
  24. gil::view(original_gray)(1, 1) = gil::gray8_pixel_t(139);
  25. }
  26. void fill_rgb()
  27. {
  28. gil::view(original_rgb)(0, 0) = gil::rgb8_pixel_t(15, 158, 150);
  29. gil::view(original_rgb)(1, 0) = gil::rgb8_pixel_t(200, 175, 150);
  30. gil::view(original_rgb)(0, 1) = gil::rgb8_pixel_t(230, 170, 150);
  31. gil::view(original_rgb)(1, 1) = gil::rgb8_pixel_t(25, 248, 150);
  32. }
  33. void test_gray_regular()
  34. {
  35. gil::view(expected_gray)(0, 0) = gil::gray8_pixel_t(0);
  36. gil::view(expected_gray)(1, 0) = gil::gray8_pixel_t(0);
  37. gil::view(expected_gray)(0, 1) = gil::gray8_pixel_t(255);
  38. gil::view(expected_gray)(1, 1) = gil::gray8_pixel_t(255);
  39. gil::threshold_optimal(
  40. gil::view(original_gray),
  41. gil::view(otsu_gray),
  42. gil::threshold_optimal_value::otsu
  43. );
  44. BOOST_TEST(gil::equal_pixels(gil::view(otsu_gray), gil::view(expected_gray)));
  45. }
  46. void test_gray_inverse()
  47. {
  48. gil::view(expected_gray)(0, 0) = gil::gray8_pixel_t(255);
  49. gil::view(expected_gray)(1, 0) = gil::gray8_pixel_t(255);
  50. gil::view(expected_gray)(0, 1) = gil::gray8_pixel_t(0);
  51. gil::view(expected_gray)(1, 1) = gil::gray8_pixel_t(0);
  52. gil::threshold_optimal(
  53. gil::view(original_gray),
  54. gil::view(otsu_gray),
  55. gil::threshold_optimal_value::otsu,
  56. gil::threshold_direction::inverse
  57. );
  58. BOOST_TEST(gil::equal_pixels(gil::view(otsu_gray), gil::view(expected_gray)));
  59. }
  60. void test_rgb_regular()
  61. {
  62. gil::view(expected_rgb)(0, 0) = gil::rgb8_pixel_t(0, 0, 255);
  63. gil::view(expected_rgb)(1, 0) = gil::rgb8_pixel_t(255, 0, 255);
  64. gil::view(expected_rgb)(0, 1) = gil::rgb8_pixel_t(255, 0, 255);
  65. gil::view(expected_rgb)(1, 1) = gil::rgb8_pixel_t(0, 255, 255);
  66. gil::threshold_optimal(
  67. gil::view(original_rgb),
  68. gil::view(otsu_rgb),
  69. gil::threshold_optimal_value::otsu
  70. );
  71. BOOST_TEST(gil::equal_pixels(gil::view(otsu_rgb), gil::view(expected_rgb)));
  72. }
  73. void test_rgb_inverse()
  74. {
  75. gil::view(expected_rgb)(0, 0) = gil::rgb8_pixel_t(255, 255, 0);
  76. gil::view(expected_rgb)(1, 0) = gil::rgb8_pixel_t(0, 255, 0);
  77. gil::view(expected_rgb)(0, 1) = gil::rgb8_pixel_t(0, 255, 0);
  78. gil::view(expected_rgb)(1, 1) = gil::rgb8_pixel_t(255, 0, 0);
  79. gil::threshold_optimal(
  80. gil::view(original_rgb),
  81. gil::view(otsu_rgb),
  82. gil::threshold_optimal_value::otsu,
  83. gil::threshold_direction::inverse
  84. );
  85. BOOST_TEST(gil::equal_pixels(gil::view(otsu_rgb), gil::view(expected_rgb)));
  86. }
  87. int main()
  88. {
  89. fill_gray();
  90. fill_rgb();
  91. test_gray_regular();
  92. test_gray_inverse();
  93. test_rgb_regular();
  94. test_rgb_inverse();
  95. return boost::report_errors();
  96. }