Debian Codegraph @

imagery/i.rgb.his/testsuite/test_i_rgb_his.py

raw
1 from grass.gunittest.case import TestCase
2 from grass.gunittest.main import test
3 import grass.script as gs
4
5
6 class TestIRGBHIS(TestCase):
7 tmp_rasters = []
8
9 @classmethod
10 def setUpClass(cls):
11 """Setup input rasters and configure test environment."""
12 cls.runModule("g.region", n=10, s=0, e=10, w=0, rows=10, cols=10)
13
14 @classmethod
15 def tearDownClass(cls):
16 """Remove generated rasters and reset test environment."""
17 cls.runModule(
18 "g.remove",
19 flags="f",
20 type="raster",
21 name=cls.tmp_rasters,
22 )
23
24 def create_raster(self, name, expression=None, seed=None):
25 """Helper to create a rasters."""
26 expr = "rand(0, 255)" if seed is not None else expression
27 self.runModule(
28 "r.mapcalc", expression=f"{name} = {expr}", seed=seed, overwrite=True
29 )
30
31 def test_output_ranges(self):
32 """Test that HIS output values are within valid ranges (0-255)"""
33 seed_value = 12345
34 self.create_raster("test_red", seed=seed_value)
35 self.create_raster("test_green", seed=seed_value + 1)
36 self.create_raster("test_blue", seed=seed_value + 2)
37
38 self.assertModule(
39 "i.rgb.his",
40 red="test_red",
41 green="test_green",
42 blue="test_blue",
43 hue="test_hue",
44 intensity="test_intensity",
45 saturation="test_saturation",
46 overwrite=True,
47 )
48 self.tmp_rasters.extend(
49 [
50 "test_red",
51 "test_green",
52 "test_blue",
53 "test_hue",
54 "test_intensity",
55 "test_saturation",
56 ]
57 )
58
59 for map_name, min_val, max_val in [
60 ("test_hue", 0, 255),
61 ("test_intensity", 0, 255),
62 ("test_saturation", 0, 255),
63 ]:
64 info = gs.parse_command("r.info", map=map_name, format="json")
65 min_actual = info["min"]
66 max_actual = info["max"]
67 self.assertGreaterEqual(
68 min_actual,
69 min_val,
70 msg=f"{map_name} minimum value ({min_actual}) < {min_val}",
71 )
72 self.assertLessEqual(
73 max_actual,
74 max_val,
75 msg=f"{map_name} maximum value ({max_actual}) > {max_val}",
76 )
77
78 def test_grayscale_conversion(self):
79 """Test neutral color conversion (R=G=B=100)"""
80 self.create_raster("test_red_gray", "100")
81 self.create_raster("test_green_gray", "100")
82 self.create_raster("test_blue_gray", "100")
83
84 self.assertModule(
85 "i.rgb.his",
86 red="test_red_gray",
87 green="test_green_gray",
88 blue="test_blue_gray",
89 hue="test_hue_gray",
90 intensity="test_intensity_gray",
91 saturation="test_saturation_gray",
92 overwrite=True,
93 )
94 self.tmp_rasters.extend(
95 [
96 "test_red_gray",
97 "test_green_gray",
98 "test_blue_gray",
99 "test_hue_gray",
100 "test_intensity_gray",
101 "test_saturation_gray",
102 ]
103 )
104
105 univar_hue = gs.parse_command("r.univar", map="test_hue_gray", format="json")
106 self.assertAlmostEqual(univar_hue["mean"], 0.0, places=6)
107
108 univar_intensity = gs.parse_command(
109 "r.univar", map="test_intensity_gray", format="json"
110 )
111 self.assertAlmostEqual(univar_intensity["mean"], 100.0, places=6)
112
113 univar_saturation = gs.parse_command(
114 "r.univar", map="test_saturation_gray", format="json"
115 )
116 self.assertAlmostEqual(univar_saturation["mean"], 0.0, places=6)
117
118 def test_pure_primary_colors(self):
119 """Test conversion for primary RGB colors"""
120 test_cases = [
121 ("red", 255, 0, 0, 0.0),
122 ("green", 0, 255, 0, 85.0),
123 ("blue", 0, 0, 255, 170.0),
124 ]
125
126 for color, r, g, b, exp_hue in test_cases:
127 self.create_raster(f"test_r_{color}", f"{r}")
128 self.create_raster(f"test_g_{color}", f"{g}")
129 self.create_raster(f"test_b_{color}", f"{b}")
130
131 self.assertModule(
132 "i.rgb.his",
133 red=f"test_r_{color}",
134 green=f"test_g_{color}",
135 blue=f"test_b_{color}",
136 hue=f"test_hue_{color}",
137 intensity=f"test_intensity_{color}",
138 saturation=f"test_saturation_{color}",
139 overwrite=True,
140 )
141 self.tmp_rasters.extend(
142 [
143 f"test_r_{color}",
144 f"test_g_{color}",
145 f"test_b_{color}",
146 f"test_hue_{color}",
147 f"test_intensity_{color}",
148 f"test_saturation_{color}",
149 ]
150 )
151
152 univar_hue = gs.parse_command(
153 "r.univar", map=f"test_hue_{color}", format="json"
154 )
155 self.assertEqual(
156 univar_hue["mean"],
157 exp_hue,
158 msg=f"Hue for {color}: Expected {exp_hue}, got {univar_hue['mean']}",
159 )
160
161 univar_intensity = gs.parse_command(
162 "r.univar", map=f"test_intensity_{color}", format="json"
163 )
164 self.assertAlmostEqual(
165 univar_intensity["mean"],
166 128,
167 msg=f"Intensity check failed for {color}",
168 )
169 # should have been 85 (255/3) but is 128 for all the colors suggesting normalization
170
171 univar_saturation = gs.parse_command(
172 "r.univar", map=f"test_saturation_{color}", format="json"
173 )
174 self.assertAlmostEqual(
175 univar_saturation["mean"],
176 255.0,
177 msg=f"Saturation check failed for {color}",
178 )
179
180 def test_conversion_roundtrip(self):
181 """Test the accuracy of the RGB to HIS to RGB conversion within an expected tolerance."""
182 self.create_raster("test_red_orig", "255")
183 self.create_raster("test_green_orig", "128")
184 self.create_raster("test_blue_orig", "64")
185
186 self.assertModule(
187 "i.rgb.his",
188 red="test_red_orig",
189 green="test_green_orig",
190 blue="test_blue_orig",
191 hue="test_hue_rt",
192 intensity="test_intensity_rt",
193 saturation="test_saturation_rt",
194 overwrite=True,
195 )
196
197 self.assertModule(
198 "i.his.rgb",
199 hue="test_hue_rt",
200 intensity="test_intensity_rt",
201 saturation="test_saturation_rt",
202 red="test_red_new",
203 green="test_green_new",
204 blue="test_blue_new",
205 overwrite=True,
206 )
207 self.tmp_rasters.extend(
208 [
209 "test_red_orig",
210 "test_green_orig",
211 "test_blue_orig",
212 "test_hue_rt",
213 "test_intensity_rt",
214 "test_saturation_rt",
215 "test_red_new",
216 "test_green_new",
217 "test_blue_new",
218 ]
219 )
220
221 for channel in ["red", "green", "blue"]:
222 self.create_raster(
223 f"test_diff_{channel}", f"abs(test_{channel}_orig - test_{channel}_new)"
224 )
225 self.tmp_rasters.append(f"test_diff_{channel}")
226
227 diff_stats = gs.parse_command(
228 "r.univar", map=f"test_diff_{channel}", flags="g"
229 )
230 max_diff = float(diff_stats["max"])
231 mean_diff = float(diff_stats["mean"])
232
233 self.assertLessEqual(
234 max_diff,
235 1.0,
236 msg=f"Maximum difference in {channel} channel exceeds tolerance. Max Diff: {max_diff}",
237 )
238 self.assertLessEqual(
239 mean_diff,
240 1.0,
241 msg=f"Mean difference in {channel} channel too high. Mean Diff: {mean_diff}",
242 )
243
244
245 if __name__ == "__main__":
246 test()