threshold_binary.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 = 4;
  15. int width = 4;
  16. gil::gray8_image_t original_gray(width, height), threshold_gray(width, height),
  17. expected_gray(width, height);
  18. gil::rgb8_image_t original_rgb(width, height), threshold_rgb(width, height),
  19. expected_rgb(width, height);
  20. void fill_original_gray()
  21. {
  22. //filling original_gray view's upper half part with gray pixels of value 50
  23. //filling original_gray view's lower half part with gray pixels of value 150
  24. gil::fill_pixels(gil::subimage_view(gil::view(original_gray), 0, 0, original_gray.width(),
  25. original_gray.height() / 2), gil::gray8_pixel_t(50));
  26. gil::fill_pixels(gil::subimage_view(gil::view(original_gray), 0, original_gray.height() / 2,
  27. original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(150));
  28. }
  29. void fill_original_rgb()
  30. {
  31. //filling original_rgb view's upper half part with rgb pixels of value 50, 155, 115
  32. //filling original_rgb view's lower half part with rgb pixels of value 203, 9, 60
  33. gil::fill_pixels(gil::subimage_view(gil::view(original_rgb), 0, 0, original_rgb.width(),
  34. original_rgb.height() / 2), gil::rgb8_pixel_t(50, 155, 115));
  35. gil::fill_pixels(gil::subimage_view(gil::view(original_rgb), 0, original_rgb.height() / 2,
  36. original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(203, 9, 60));
  37. }
  38. void binary_gray_to_gray()
  39. {
  40. //expected_gray view after thresholding of the original_gray view with threshold_gray value of 100
  41. //filling expected_gray view's upper half part with gray pixels of value 0
  42. //filling expected_gray view's lower half part with gray pixels of value 255
  43. gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(),
  44. original_gray.height() / 2), gil::gray8_pixel_t(0));
  45. gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2,
  46. original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(255));
  47. gil::threshold_binary(gil::view(original_gray), gil::view(threshold_gray), 100);
  48. //comparing threshold_gray view generated by the function with the expected_gray view
  49. BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
  50. }
  51. void binary_inverse_gray_to_gray()
  52. {
  53. //expected_gray view after thresholding of the original_gray view with threshold_gray value of 100
  54. //filling expected_gray view's upper half part with gray pixels of value 200
  55. //filling expected_gray view's lower half part with gray pixels of value 0
  56. gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(),
  57. original_gray.height() / 2), gil::gray8_pixel_t(200));
  58. gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2,
  59. original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(0));
  60. gil::threshold_binary
  61. (
  62. gil::view(original_gray),
  63. gil::view(threshold_gray),
  64. 100,
  65. 200,
  66. gil::threshold_direction::inverse
  67. );
  68. //comparing threshold_gray view generated by the function with the expected_gray view
  69. BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
  70. }
  71. void binary_rgb_to_rgb()
  72. {
  73. //expected_rgb view after thresholding of the original_rgb view with threshold value of 100
  74. //filling expected_rgb view's upper half part with rgb pixels of value 0, 165, 165
  75. //filling expected_rgb view's lower half part with rgb pixels of value 165, 0, 0
  76. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
  77. original_rgb.height() / 2), gil::rgb8_pixel_t(0, 165, 165));
  78. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
  79. original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(165, 0, 0));
  80. gil::threshold_binary(gil::view(original_rgb), gil::view(threshold_rgb), 100, 165);
  81. //comparing threshold_rgb view generated by the function with the expected_rgb view
  82. BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
  83. }
  84. void binary_inverse_rgb_to_rgb()
  85. {
  86. //expected_rgb view after thresholding of the original_rgb view with threshold value of 100
  87. //filling expected_rgb view's upper half part with rgb pixels of value 90, 0, 0
  88. //filling expected_rgb view's lower half part with rgb pixels of value 0, 90, 90
  89. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
  90. original_rgb.height() / 2), gil::rgb8_pixel_t(90, 0, 0));
  91. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
  92. original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(0, 90, 90));
  93. gil::threshold_binary
  94. (
  95. gil::view(original_rgb),
  96. gil::view(threshold_rgb),
  97. 100,
  98. 90,
  99. gil::threshold_direction::inverse
  100. );
  101. //comparing threshold_rgb view generated by the function with the expected_rgb view
  102. BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
  103. }
  104. int main()
  105. {
  106. fill_original_gray();
  107. fill_original_rgb();
  108. binary_gray_to_gray();
  109. binary_inverse_gray_to_gray();
  110. binary_rgb_to_rgb();
  111. binary_inverse_rgb_to_rgb();
  112. return boost::report_errors();
  113. }