Skip to content

Commit 6fabdf2

Browse files
Fix bounding box computation for segmentations (#62)
1 parent 3e207bd commit 6fabdf2

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/labelformat/model/binary_mask_segmentation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ def _compute_bbox_from_rle(
172172

173173
# Update bounding box
174174
ymin = min(ymin, y)
175-
ymax = max(ymax, run_end_y)
175+
ymax = max(ymax, run_end_y + 1)
176176
if run_end_y > y:
177177
xmin = 0
178-
xmax = width - 1
178+
xmax = width
179179
else:
180180
xmin = min(xmin, x)
181-
xmax = max(xmax, run_end_x)
181+
xmax = max(xmax, run_end_x + 1)
182182

183183
# Compute coordinates for the start of the next run
184184
x += run_length

tests/unit/model/test_binary_mask_segmentation.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
BinaryMaskSegmentation,
77
RLEDecoderEncoder,
88
)
9-
from labelformat.model.bounding_box import BoundingBox
9+
from labelformat.model.bounding_box import BoundingBox, BoundingBoxFormat
1010

1111

1212
class TestBinaryMaskSegmentation:
@@ -25,20 +25,23 @@ def test_from_binary_mask(self) -> None:
2525

2626
def test_from_rle(self) -> None:
2727
binary_mask_segmentation = BinaryMaskSegmentation.from_rle(
28-
rle_row_wise=[1, 1, 4, 2, 1, 3, 2, 1, 5],
28+
rle_row_wise=[6, 1, 4, 2, 1, 3, 2, 1],
2929
width=5,
3030
height=4,
3131
bounding_box=None,
3232
)
3333
assert binary_mask_segmentation.width == 5
3434
assert binary_mask_segmentation.height == 4
35-
assert binary_mask_segmentation.bounding_box == BoundingBox(0, 0, 4, 2)
35+
assert binary_mask_segmentation.bounding_box == BoundingBox(0, 1, 5, 4)
36+
assert binary_mask_segmentation.bounding_box.to_format(
37+
format=BoundingBoxFormat.XYWH
38+
) == [0, 1, 5, 3]
3639
expected: NDArray[np.int_] = np.array(
3740
[
41+
[0, 0, 0, 0, 0],
3842
[0, 1, 0, 0, 0],
3943
[0, 1, 1, 0, 1],
4044
[1, 1, 0, 0, 1],
41-
[0, 0, 0, 0, 0],
4245
],
4346
dtype=np.int_,
4447
)
@@ -142,7 +145,7 @@ def test_compute_bbox_from_rle() -> None:
142145
width=4,
143146
height=3,
144147
)
145-
assert bbox == BoundingBox(xmin=0, ymin=0, xmax=3, ymax=2)
148+
assert bbox == BoundingBox(xmin=0, ymin=0, xmax=4, ymax=3)
146149

147150
# 0011
148151
# 0000
@@ -151,7 +154,7 @@ def test_compute_bbox_from_rle() -> None:
151154
width=4,
152155
height=2,
153156
)
154-
assert bbox == BoundingBox(xmin=2, ymin=0, xmax=3, ymax=0)
157+
assert bbox == BoundingBox(xmin=2, ymin=0, xmax=4, ymax=1)
155158

156159
# 0011
157160
# 1000
@@ -160,12 +163,12 @@ def test_compute_bbox_from_rle() -> None:
160163
width=4,
161164
height=2,
162165
)
163-
assert bbox == BoundingBox(xmin=0, ymin=0, xmax=3, ymax=1)
166+
assert bbox == BoundingBox(xmin=0, ymin=0, xmax=4, ymax=2)
164167

165168
# 1111
166169
bbox = binary_mask_segmentation._compute_bbox_from_rle(
167170
rle_row_wise=[0, 4],
168171
width=4,
169172
height=1,
170173
)
171-
assert bbox == BoundingBox(xmin=0, ymin=0, xmax=3, ymax=0)
174+
assert bbox == BoundingBox(xmin=0, ymin=0, xmax=4, ymax=1)

0 commit comments

Comments
 (0)