11import numpy as np
22from numpy .typing import NDArray
33
4+ from labelformat .model import binary_mask_segmentation
45from labelformat .model .binary_mask_segmentation import (
56 BinaryMaskSegmentation ,
67 RLEDecoderEncoder ,
@@ -22,6 +23,48 @@ def test_from_binary_mask(self) -> None:
2223 assert binary_mask_segmentation .bounding_box == bounding_box
2324 assert np .array_equal (binary_mask_segmentation .get_binary_mask (), binary_mask )
2425
26+ def test_from_rle (self ) -> None :
27+ binary_mask_segmentation = BinaryMaskSegmentation .from_rle (
28+ rle_row_wise = [1 , 1 , 4 , 2 , 1 , 3 , 2 , 1 , 5 ],
29+ width = 5 ,
30+ height = 4 ,
31+ bounding_box = None ,
32+ )
33+ assert binary_mask_segmentation .width == 5
34+ assert binary_mask_segmentation .height == 4
35+ assert binary_mask_segmentation .bounding_box == BoundingBox (0 , 0 , 4 , 2 )
36+ expected : NDArray [np .int_ ] = np .array (
37+ [
38+ [0 , 1 , 0 , 0 , 0 ],
39+ [0 , 1 , 1 , 0 , 1 ],
40+ [1 , 1 , 0 , 0 , 1 ],
41+ [0 , 0 , 0 , 0 , 0 ],
42+ ],
43+ dtype = np .int_ ,
44+ )
45+ assert np .array_equal (binary_mask_segmentation .get_binary_mask (), expected )
46+
47+ # Test with provided bounding box
48+ # The box is larger than the actual mask, but should be preserved
49+ binary_mask_segmentation = BinaryMaskSegmentation .from_rle (
50+ rle_row_wise = [6 , 3 ],
51+ width = 3 ,
52+ height = 3 ,
53+ bounding_box = BoundingBox (0 , 0 , 2 , 2 ),
54+ )
55+ assert binary_mask_segmentation .width == 3
56+ assert binary_mask_segmentation .height == 3
57+ assert binary_mask_segmentation .bounding_box == BoundingBox (0 , 0 , 2 , 2 )
58+ expected = np .array (
59+ [
60+ [0 , 0 , 0 ],
61+ [0 , 0 , 0 ],
62+ [1 , 1 , 1 ],
63+ ],
64+ dtype = np .int_ ,
65+ )
66+ assert np .array_equal (binary_mask_segmentation .get_binary_mask (), expected )
67+
2568
2669class TestRLEDecoderEncoder :
2770 def test_encode_row_wise_rle (self ) -> None :
@@ -79,3 +122,41 @@ def test_inverse__column_wise(self) -> None:
79122 rle , mask .shape [0 ], mask .shape [1 ]
80123 )
81124 assert np .array_equal (mask , mask_inverse_column_wise )
125+
126+
127+ def test_compute_bbox_from_rle () -> None :
128+ # 0011
129+ # 1111
130+ # 1100
131+ bbox = binary_mask_segmentation ._compute_bbox_from_rle (
132+ rle_row_wise = [2 , 8 , 2 ],
133+ width = 4 ,
134+ height = 3 ,
135+ )
136+ assert bbox == BoundingBox (xmin = 0 , ymin = 0 , xmax = 3 , ymax = 2 )
137+
138+ # 0011
139+ # 0000
140+ bbox = binary_mask_segmentation ._compute_bbox_from_rle (
141+ rle_row_wise = [2 , 2 , 4 ],
142+ width = 4 ,
143+ height = 2 ,
144+ )
145+ assert bbox == BoundingBox (xmin = 2 , ymin = 0 , xmax = 3 , ymax = 0 )
146+
147+ # 0011
148+ # 1000
149+ bbox = binary_mask_segmentation ._compute_bbox_from_rle (
150+ rle_row_wise = [2 , 3 , 3 ],
151+ width = 4 ,
152+ height = 2 ,
153+ )
154+ assert bbox == BoundingBox (xmin = 0 , ymin = 0 , xmax = 3 , ymax = 1 )
155+
156+ # 1111
157+ bbox = binary_mask_segmentation ._compute_bbox_from_rle (
158+ rle_row_wise = [0 , 4 ],
159+ width = 4 ,
160+ height = 1 ,
161+ )
162+ assert bbox == BoundingBox (xmin = 0 , ymin = 0 , xmax = 3 , ymax = 0 )
0 commit comments