|
14 | 14 |
|
15 | 15 | namespace gil = boost::gil; |
16 | 16 |
|
17 | | -int height = 4; |
18 | | -int width = 4; |
| 17 | +namespace { |
19 | 18 |
|
20 | | -gil::gray8_image_t original_gray(width, height), threshold_gray(width, height), |
21 | | -expected_gray(width, height); |
22 | | - |
23 | | -gil::rgb8_image_t original_rgb(width, height), threshold_rgb(width, height), |
24 | | -expected_rgb(width, height); |
25 | | - |
26 | | - |
27 | | -void fill_original_gray() |
| 19 | +template <typename View, typename Pixel> |
| 20 | +void fill_upper_and_lower_half( |
| 21 | + View const& view, Pixel const& upper_half_pixel, Pixel const& lower_half_pixel) |
28 | 22 | { |
29 | | - //filling original_gray view's upper half part with gray pixels of value 50 |
30 | | - //filling original_gray view's lower half part with gray pixels of value 150 |
31 | | - gil::fill_pixels(gil::subimage_view(gil::view(original_gray), 0, 0, original_gray.width(), |
32 | | - original_gray.height() / 2), gil::gray8_pixel_t(50)); |
33 | | - gil::fill_pixels(gil::subimage_view(gil::view(original_gray), 0, original_gray.height() / 2, |
34 | | - original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(150)); |
| 23 | + fill_pixels( |
| 24 | + subimage_view(view, 0, 0, view.width(), view.height() / 2), |
| 25 | + upper_half_pixel); |
| 26 | + fill_pixels( |
| 27 | + subimage_view(view, 0, view.height() / 2, view.width(), view.height() / 2), |
| 28 | + lower_half_pixel); |
35 | 29 | } |
36 | 30 |
|
37 | | -void fill_original_rgb() |
| 31 | +template <typename Image, typename Pixel, typename ChannelValue> |
| 32 | +void test_threshold( |
| 33 | + gil::threshold_direction direction, |
| 34 | + ChannelValue threshold_value, |
| 35 | + ChannelValue max_value, |
| 36 | + bool set_max_value, |
| 37 | + Pixel const& upper_half_pixel, |
| 38 | + Pixel const& lower_half_pixel, |
| 39 | + Pixel const& expected_upper_half_pixel, |
| 40 | + Pixel const& expected_lower_half_pixel) |
38 | 41 | { |
39 | | - //filling original_rgb view's upper half part with rgb pixels of value 50, 155, 115 |
40 | | - //filling original_rgb view's lower half part with rgb pixels of value 203, 9, 60 |
41 | | - gil::fill_pixels(gil::subimage_view(gil::view(original_rgb), 0, 0, original_rgb.width(), |
42 | | - original_rgb.height() / 2), gil::rgb8_pixel_t(50, 155, 115)); |
43 | | - gil::fill_pixels(gil::subimage_view(gil::view(original_rgb), 0, original_rgb.height() / 2, |
44 | | - original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(203, 9, 60)); |
| 42 | + int const height = 4; |
| 43 | + int const width = 4; |
| 44 | + |
| 45 | + Image original_img(width, height); |
| 46 | + Image threshold_img(width, height); |
| 47 | + Image expected_img(width, height); |
| 48 | + |
| 49 | + auto original_view = gil::view(original_img); |
| 50 | + auto threshold_view = gil::view(threshold_img); |
| 51 | + auto expected_view = gil::view(expected_img); |
| 52 | + |
| 53 | + fill_upper_and_lower_half( |
| 54 | + original_view, upper_half_pixel, lower_half_pixel); |
| 55 | + fill_upper_and_lower_half( |
| 56 | + expected_view, expected_upper_half_pixel, expected_lower_half_pixel); |
| 57 | + |
| 58 | + if (set_max_value) |
| 59 | + { |
| 60 | + threshold_binary( |
| 61 | + original_view, threshold_view, |
| 62 | + threshold_value, max_value, direction); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + threshold_binary(original_view, threshold_view, threshold_value, direction); |
| 67 | + } |
| 68 | + |
| 69 | + BOOST_TEST(equal_pixels(threshold_view, expected_view)); |
45 | 70 | } |
46 | 71 |
|
47 | | -void binary_gray_to_gray() |
48 | | -{ |
49 | | - //expected_gray view after thresholding of the original_gray view with threshold_gray value of 100 |
50 | | - //filling expected_gray view's upper half part with gray pixels of value 0 |
51 | | - //filling expected_gray view's lower half part with gray pixels of value 255 |
52 | | - gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(), |
53 | | - original_gray.height() / 2), gil::gray8_pixel_t(0)); |
54 | | - gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2, |
55 | | - original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(255)); |
56 | | - |
57 | | - gil::threshold_binary(gil::view(original_gray), gil::view(threshold_gray), 100); |
58 | | - |
59 | | - //comparing threshold_gray view generated by the function with the expected_gray view |
60 | | - BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray))); |
61 | | -} |
| 72 | +} // namespace |
62 | 73 |
|
63 | | -void binary_inverse_gray_to_gray() |
| 74 | +void test_threshold_binary() |
64 | 75 | { |
65 | | - //expected_gray view after thresholding of the original_gray view with threshold_gray value of 100 |
66 | | - //filling expected_gray view's upper half part with gray pixels of value 200 |
67 | | - //filling expected_gray view's lower half part with gray pixels of value 0 |
68 | | - gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, 0, original_gray.width(), |
69 | | - original_gray.height() / 2), gil::gray8_pixel_t(200)); |
70 | | - gil::fill_pixels(gil::subimage_view(gil::view(expected_gray), 0, original_gray.height() / 2, |
71 | | - original_gray.width(), original_gray.height() / 2), gil::gray8_pixel_t(0)); |
72 | | - |
73 | | - gil::threshold_binary |
74 | | - ( |
75 | | - gil::view(original_gray), |
76 | | - gil::view(threshold_gray), |
| 76 | + // threshold binary should set all pixels below the threshold to channel min |
| 77 | + // and above the threshold to the given max value or the channel max if no |
| 78 | + // max is supplied |
| 79 | + test_threshold<gil::gray8_image_t>( |
| 80 | + gil::threshold_direction::regular, |
77 | 81 | 100, |
78 | | - 200, |
79 | | - gil::threshold_direction::inverse |
80 | | - ); |
81 | | - |
82 | | - //comparing threshold_gray view generated by the function with the expected_gray view |
83 | | - BOOST_TEST(gil::equal_pixels(gil::view(threshold_gray), gil::view(expected_gray))); |
84 | | -} |
85 | | - |
86 | | -void binary_rgb_to_rgb() |
87 | | -{ |
88 | | - //expected_rgb view after thresholding of the original_rgb view with threshold value of 100 |
89 | | - //filling expected_rgb view's upper half part with rgb pixels of value 0, 165, 165 |
90 | | - //filling expected_rgb view's lower half part with rgb pixels of value 165, 0, 0 |
91 | | - gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(), |
92 | | - original_rgb.height() / 2), gil::rgb8_pixel_t(0, 165, 165)); |
93 | | - gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2, |
94 | | - original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(165, 0, 0)); |
95 | | - |
96 | | - gil::threshold_binary(gil::view(original_rgb), gil::view(threshold_rgb), 100, 165); |
97 | | - |
98 | | - //comparing threshold_rgb view generated by the function with the expected_rgb view |
99 | | - BOOST_TEST(gil::equal_pixels(gil::view(threshold_rgb), gil::view(expected_rgb))); |
| 82 | + 150, true, |
| 83 | + gil::gray8_pixel_t{50}, gil::gray8_pixel_t{170}, |
| 84 | + gil::gray8_pixel_t{0}, gil::gray8_pixel_t{150}); |
| 85 | + test_threshold<gil::gray8_image_t>( |
| 86 | + gil::threshold_direction::regular, |
| 87 | + 100, |
| 88 | + 150, false, |
| 89 | + gil::gray8_pixel_t{50}, gil::gray8_pixel_t{170}, |
| 90 | + gil::gray8_pixel_t{0}, gil::gray8_pixel_t{255}); |
| 91 | + |
| 92 | + test_threshold<gil::gray8s_image_t>( |
| 93 | + gil::threshold_direction::regular, |
| 94 | + 50, |
| 95 | + 100, true, |
| 96 | + gil::gray8s_pixel_t{50}, gil::gray8s_pixel_t{110}, |
| 97 | + gil::gray8s_pixel_t{-128}, gil::gray8s_pixel_t{100}); |
| 98 | + test_threshold<gil::gray8s_image_t>( |
| 99 | + gil::threshold_direction::regular, |
| 100 | + 50, |
| 101 | + 100, false, |
| 102 | + gil::gray8s_pixel_t{50}, gil::gray8s_pixel_t{110}, |
| 103 | + gil::gray8s_pixel_t{-128}, gil::gray8s_pixel_t{127}); |
| 104 | + |
| 105 | + test_threshold<gil::gray32f_image_t>( |
| 106 | + gil::threshold_direction::regular, |
| 107 | + 0.5f, |
| 108 | + 0.6f, true, |
| 109 | + gil::gray32f_pixel_t{0.3f}, gil::gray32f_pixel_t{0.7f}, |
| 110 | + gil::gray32f_pixel_t{0.0f}, gil::gray32f_pixel_t{0.6f}); |
| 111 | + test_threshold<gil::gray32f_image_t>( |
| 112 | + gil::threshold_direction::regular, |
| 113 | + 0.5f, |
| 114 | + 0.6f, false, |
| 115 | + gil::gray32f_pixel_t{0.3f}, gil::gray32f_pixel_t{0.7f}, |
| 116 | + gil::gray32f_pixel_t{0.0f}, gil::gray32f_pixel_t{1.0f}); |
| 117 | + |
| 118 | + test_threshold<gil::rgb8_image_t>( |
| 119 | + gil::threshold_direction::regular, |
| 120 | + 100, |
| 121 | + 165, true, |
| 122 | + gil::rgb8_pixel_t{50, 155, 115}, gil::rgb8_pixel_t{203, 9, 60}, |
| 123 | + gil::rgb8_pixel_t{0, 165, 165}, gil::rgb8_pixel_t{165, 0, 0}); |
| 124 | + test_threshold<gil::rgb8_image_t>( |
| 125 | + gil::threshold_direction::regular, |
| 126 | + 100, |
| 127 | + 165, false, |
| 128 | + gil::rgb8_pixel_t{50, 155, 115}, gil::rgb8_pixel_t{203, 9, 60}, |
| 129 | + gil::rgb8_pixel_t{0, 255, 255}, gil::rgb8_pixel_t{255, 0, 0}); |
100 | 130 | } |
101 | 131 |
|
102 | | -void binary_inverse_rgb_to_rgb() |
| 132 | +void test_threshold_binary_inverse() |
103 | 133 | { |
104 | | - //expected_rgb view after thresholding of the original_rgb view with threshold value of 100 |
105 | | - //filling expected_rgb view's upper half part with rgb pixels of value 90, 0, 0 |
106 | | - //filling expected_rgb view's lower half part with rgb pixels of value 0, 90, 90 |
107 | | - gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, 0, original_rgb.width(), |
108 | | - original_rgb.height() / 2), gil::rgb8_pixel_t(90, 0, 0)); |
109 | | - gil::fill_pixels(gil::subimage_view(gil::view(expected_rgb), 0, original_rgb.height() / 2, |
110 | | - original_rgb.width(), original_rgb.height() / 2), gil::rgb8_pixel_t(0, 90, 90)); |
111 | | - |
112 | | - gil::threshold_binary |
113 | | - ( |
114 | | - gil::view(original_rgb), |
115 | | - gil::view(threshold_rgb), |
| 134 | + // inverse threshold binary should set all pixels above the threshold to channel min |
| 135 | + // and below the threshold to the given max value or the channel max if no |
| 136 | + // max is supplied |
| 137 | + test_threshold<gil::gray8_image_t>( |
| 138 | + gil::threshold_direction::inverse, |
116 | 139 | 100, |
117 | | - 90, |
118 | | - gil::threshold_direction::inverse |
119 | | - ); |
120 | | - |
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))); |
| 140 | + 150, true, |
| 141 | + gil::gray8_pixel_t{50}, gil::gray8_pixel_t{170}, |
| 142 | + gil::gray8_pixel_t{150}, gil::gray8_pixel_t{0}); |
| 143 | + test_threshold<gil::gray8_image_t>( |
| 144 | + gil::threshold_direction::inverse, |
| 145 | + 100, |
| 146 | + 150, false, |
| 147 | + gil::gray8_pixel_t{50}, gil::gray8_pixel_t{170}, |
| 148 | + gil::gray8_pixel_t{255}, gil::gray8_pixel_t{0}); |
| 149 | + |
| 150 | + test_threshold<gil::gray8s_image_t>( |
| 151 | + gil::threshold_direction::inverse, |
| 152 | + 50, |
| 153 | + 100, true, |
| 154 | + gil::gray8s_pixel_t{50}, gil::gray8s_pixel_t{110}, |
| 155 | + gil::gray8s_pixel_t{100}, gil::gray8s_pixel_t{-128}); |
| 156 | + test_threshold<gil::gray8s_image_t>( |
| 157 | + gil::threshold_direction::inverse, |
| 158 | + 50, |
| 159 | + 100, false, |
| 160 | + gil::gray8s_pixel_t{50}, gil::gray8s_pixel_t{110}, |
| 161 | + gil::gray8s_pixel_t{127}, gil::gray8s_pixel_t{-128}); |
| 162 | + |
| 163 | + test_threshold<gil::gray32f_image_t>( |
| 164 | + gil::threshold_direction::inverse, |
| 165 | + 0.5f, |
| 166 | + 0.6f, true, |
| 167 | + gil::gray32f_pixel_t{0.3f}, gil::gray32f_pixel_t{0.7f}, |
| 168 | + gil::gray32f_pixel_t{0.6f}, gil::gray32f_pixel_t{0.0f}); |
| 169 | + test_threshold<gil::gray32f_image_t>( |
| 170 | + gil::threshold_direction::inverse, |
| 171 | + 0.5f, |
| 172 | + 0.6f, false, |
| 173 | + gil::gray32f_pixel_t{0.3f}, gil::gray32f_pixel_t{0.7f}, |
| 174 | + gil::gray32f_pixel_t{1.0f}, gil::gray32f_pixel_t{0.0f}); |
| 175 | + |
| 176 | + test_threshold<gil::rgb8_image_t>( |
| 177 | + gil::threshold_direction::inverse, |
| 178 | + 100, |
| 179 | + 165, true, |
| 180 | + gil::rgb8_pixel_t{50, 155, 115}, gil::rgb8_pixel_t{203, 9, 60}, |
| 181 | + gil::rgb8_pixel_t{165, 0, 0}, gil::rgb8_pixel_t{0, 165, 165}); |
| 182 | + test_threshold<gil::rgb8_image_t>( |
| 183 | + gil::threshold_direction::inverse, |
| 184 | + 100, |
| 185 | + 165, false, |
| 186 | + gil::rgb8_pixel_t{50, 155, 115}, gil::rgb8_pixel_t{203, 9, 60}, |
| 187 | + gil::rgb8_pixel_t{255, 0, 0}, gil::rgb8_pixel_t{0, 255, 255}); |
123 | 188 | } |
124 | 189 |
|
125 | | - |
126 | 190 | int main() |
127 | 191 | { |
128 | | - fill_original_gray(); |
129 | | - fill_original_rgb(); |
130 | | - |
131 | | - binary_gray_to_gray(); |
132 | | - binary_inverse_gray_to_gray(); |
133 | | - binary_rgb_to_rgb(); |
134 | | - binary_inverse_rgb_to_rgb(); |
| 192 | + test_threshold_binary(); |
| 193 | + test_threshold_binary_inverse(); |
135 | 194 |
|
136 | 195 | return boost::report_errors(); |
137 | 196 | } |
0 commit comments