threshold_truncate.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 view's upper half part with gray pixels of value 50
  23. //filling original 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, 85, 135
  32. //filling original_rgb view's lower half part with rgb pixels of value 150, 205, 106
  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, 85, 135));
  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(150, 205, 106));
  37. }
  38. void threshold_gray_to_gray()
  39. {
  40. //expected view after thresholding of the original view with threshold value of 100
  41. //filling expected view's upper half part with gray pixels of value 50
  42. //filling expected view's lower half part with gray pixels of value 100
  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(50));
  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(100));
  47. gil::threshold_truncate(gil::view(original_gray), gil::view(threshold_gray), 100);
  48. //comparing threshold view generated by the function with the expected view
  49. BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
  50. }
  51. void threshold_inverse_gray_to_gray()
  52. {
  53. //expected view after thresholding of the original view with threshold value of 100
  54. //filling expected view's upper half part with gray pixels of value 100
  55. //filling expected view's lower half part with gray pixels of value 150
  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(100));
  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(150));
  60. gil::threshold_truncate
  61. (
  62. gil::view(original_gray),
  63. gil::view(threshold_gray),
  64. 100,
  65. gil::threshold_truncate_mode::threshold,
  66. gil::threshold_direction::inverse
  67. );
  68. //comparing threshold view generated by the function with the expected view
  69. BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
  70. }
  71. void zero_gray_to_gray()
  72. {
  73. //expected view after thresholding of the original view with threshold value of 100
  74. //filling expected view's upper half part with gray pixels of value 0
  75. //filling expected view's lower half part with gray pixels of value 150
  76. gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(),
  77. original_gray.height() / 2), gil::gray8_pixel_t(0));
  78. gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2,
  79. original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(150));
  80. gil::threshold_truncate
  81. (
  82. gil::view(original_gray),
  83. gil::view(threshold_gray),
  84. 100,
  85. gil::threshold_truncate_mode::zero,
  86. gil::threshold_direction::regular
  87. );
  88. //comparing threshold view generated by the function with the expected view
  89. BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
  90. }
  91. void zero_inverse_gray_to_gray()
  92. {
  93. //expected view after thresholding of the original view with threshold value of 100
  94. //filling expected view's upper half part with gray pixels of value 50
  95. //filling expected view's lower half part with gray pixels of value 0
  96. gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(),
  97. original_gray.height() / 2), gil::gray8_pixel_t(50));
  98. gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2,
  99. original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(0));
  100. gil::threshold_truncate
  101. (
  102. gil::view(original_gray),
  103. gil::view(threshold_gray),
  104. 100,
  105. gil::threshold_truncate_mode::zero,
  106. gil::threshold_direction::inverse
  107. );
  108. //comparing threshold view generated by the function with the expected view
  109. BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray)));
  110. }
  111. void threshold_rgb_to_rgb()
  112. {
  113. //expected view after thresholding of the original view with threshold value of 100
  114. //filling expected_rgb view's upper half part with rgb pixels of value 50
  115. //filling expected_rgb view's lower half part with rgb pixels of value 100
  116. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
  117. original_rgb.height() / 2), gil::rgb8_pixel_t(50, 85, 100));
  118. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
  119. original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(100, 100, 100));
  120. gil::threshold_truncate(gil::view(original_rgb), gil::view(threshold_rgb), 100);
  121. //comparing threshold_rgb view generated by the function with the expected_rgb view
  122. BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
  123. }
  124. void threshold_inverse_rgb_to_rgb()
  125. {
  126. //expected view after thresholding of the original view with threshold value of 100
  127. //filling expected_rgb view's upper half part with rgb pixels of value 103, 59, 246
  128. //filling expected_rgb view's lower half part with rgb pixels of value 150
  129. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
  130. original_rgb.height() / 2), gil::rgb8_pixel_t(100, 100, 135));
  131. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
  132. original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(150, 205, 106));
  133. gil::threshold_truncate
  134. (
  135. gil::view(original_rgb),
  136. gil::view(threshold_rgb),
  137. 100,
  138. gil::threshold_truncate_mode::threshold,
  139. gil::threshold_direction::inverse
  140. );
  141. //comparing threshold_rgb view generated by the function with the expected_rgb view
  142. BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
  143. }
  144. void zero_rgb_to_rgb()
  145. {
  146. //expected view after thresholding of the original view with threshold value of 100
  147. //filling expected_rgb view's upper half part with rgb pixels of value 0
  148. //filling expected_rgb view's lower half part with rgb pixels of value 150
  149. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
  150. original_rgb.height() / 2), gil::rgb8_pixel_t(0, 0, 135));
  151. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
  152. original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(150, 205, 106));
  153. gil::threshold_truncate
  154. (
  155. gil::view(original_rgb),
  156. gil::view(threshold_rgb),
  157. 100,
  158. gil::threshold_truncate_mode::zero,
  159. gil::threshold_direction::regular
  160. );
  161. //comparing threshold_rgb view generated by the function with the expected_rgb view
  162. BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
  163. }
  164. void zero_inverse_rgb_to_rgb()
  165. {
  166. //expected view after thresholding of the original view with threshold value of 100
  167. //filling expected_rgb view's upper half part with rgb pixels of value 50
  168. //filling expected_rgb view's lower half part with rgb pixels of value 0
  169. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(),
  170. original_rgb.height() / 2), gil::rgb8_pixel_t(50, 85, 0));
  171. gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2,
  172. original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(0, 0, 0));
  173. gil::threshold_truncate
  174. (
  175. gil::view(original_rgb),
  176. gil::view(threshold_rgb),
  177. 100,
  178. gil::threshold_truncate_mode::zero,
  179. gil::threshold_direction::inverse
  180. );
  181. //comparing threshold_rgb view generated by the function with the expected_rgb view
  182. BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb)));
  183. }
  184. int main()
  185. {
  186. fill_original_gray();
  187. fill_original_rgb();
  188. threshold_gray_to_gray();
  189. threshold_inverse_gray_to_gray();
  190. zero_gray_to_gray();
  191. zero_inverse_gray_to_gray();
  192. threshold_rgb_to_rgb();
  193. threshold_inverse_rgb_to_rgb();
  194. zero_rgb_to_rgb();
  195. zero_inverse_rgb_to_rgb();
  196. return boost::report_errors();
  197. }