Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / gil / test / core / image_processing / threshold_otsu.cpp
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
14
15 namespace gil = boost::gil;
16
17 int height = 2;
18 int width = 2;
19
20 gil::gray8_image_t original_gray(width, height), otsu_gray(width, height),
21 expected_gray(width, height);
22
23 gil::rgb8_image_t original_rgb(width, height), otsu_rgb(width, height), expected_rgb(width, height);
24
25 void fill_gray()
26 {
27     gil::view(original_gray)(0, 0) = gil::gray8_pixel_t(56);
28     gil::view(original_gray)(1, 0) = gil::gray8_pixel_t(89);
29     gil::view(original_gray)(0, 1) = gil::gray8_pixel_t(206);
30     gil::view(original_gray)(1, 1) = gil::gray8_pixel_t(139);
31 }
32
33 void fill_rgb()
34 {
35     gil::view(original_rgb)(0, 0) = gil::rgb8_pixel_t(15, 158, 150);
36     gil::view(original_rgb)(1, 0) = gil::rgb8_pixel_t(200, 175, 150);
37     gil::view(original_rgb)(0, 1) = gil::rgb8_pixel_t(230, 170, 150);
38     gil::view(original_rgb)(1, 1) = gil::rgb8_pixel_t(25, 248, 150);
39 }
40
41 void test_gray_regular()
42 {
43     gil::view(expected_gray)(0, 0) = gil::gray8_pixel_t(0);
44     gil::view(expected_gray)(1, 0) = gil::gray8_pixel_t(0);
45     gil::view(expected_gray)(0, 1) = gil::gray8_pixel_t(255);
46     gil::view(expected_gray)(1, 1) = gil::gray8_pixel_t(255);
47
48     gil::threshold_optimal(
49         gil::view(original_gray),
50         gil::view(otsu_gray),
51         gil::threshold_optimal_value::otsu
52     );
53
54     BOOST_TEST(gil::equal_pixels(gil::view(otsu_gray), gil::view(expected_gray)));
55 }
56
57 void test_gray_inverse()
58 {
59     gil::view(expected_gray)(0, 0) = gil::gray8_pixel_t(255);
60     gil::view(expected_gray)(1, 0) = gil::gray8_pixel_t(255);
61     gil::view(expected_gray)(0, 1) = gil::gray8_pixel_t(0);
62     gil::view(expected_gray)(1, 1) = gil::gray8_pixel_t(0);
63
64     gil::threshold_optimal(
65         gil::view(original_gray),
66         gil::view(otsu_gray),
67         gil::threshold_optimal_value::otsu,
68         gil::threshold_direction::inverse
69     );
70
71     BOOST_TEST(gil::equal_pixels(gil::view(otsu_gray), gil::view(expected_gray)));
72 }
73
74 void test_rgb_regular()
75 {
76     gil::view(expected_rgb)(0, 0) = gil::rgb8_pixel_t(0, 0, 255);
77     gil::view(expected_rgb)(1, 0) = gil::rgb8_pixel_t(255, 0, 255);
78     gil::view(expected_rgb)(0, 1) = gil::rgb8_pixel_t(255, 0, 255);
79     gil::view(expected_rgb)(1, 1) = gil::rgb8_pixel_t(0, 255, 255);
80
81     gil::threshold_optimal(
82         gil::view(original_rgb),
83         gil::view(otsu_rgb),
84         gil::threshold_optimal_value::otsu
85     );
86
87     BOOST_TEST(gil::equal_pixels(gil::view(otsu_rgb), gil::view(expected_rgb)));
88 }
89
90 void test_rgb_inverse()
91 {
92     gil::view(expected_rgb)(0, 0) = gil::rgb8_pixel_t(255, 255, 0);
93     gil::view(expected_rgb)(1, 0) = gil::rgb8_pixel_t(0, 255, 0);
94     gil::view(expected_rgb)(0, 1) = gil::rgb8_pixel_t(0, 255, 0);
95     gil::view(expected_rgb)(1, 1) = gil::rgb8_pixel_t(255, 0, 0);
96
97     gil::threshold_optimal(
98         gil::view(original_rgb),
99         gil::view(otsu_rgb),
100         gil::threshold_optimal_value::otsu,
101         gil::threshold_direction::inverse
102     );
103
104     BOOST_TEST(gil::equal_pixels(gil::view(otsu_rgb), gil::view(expected_rgb)));
105 }
106
107 int main()
108 {
109     fill_gray();
110     fill_rgb();
111
112     test_gray_regular();
113     test_gray_inverse();
114     test_rgb_regular();
115     test_rgb_inverse();
116
117     return boost::report_errors();
118 }