Skip to content

Commit 837f5a5

Browse files
authored
Fix save masks bug (#332)
* Fix save masks bug * Fix save masks bug for SAM 1
1 parent dab8b84 commit 837f5a5

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

samgeo/samgeo.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ def save_masks(
265265

266266
# Generate a mask of objects with unique values
267267
if unique:
268-
# Sort the masks by area in ascending order
269-
sorted_masks = sorted(masks, key=(lambda x: x["area"]), reverse=False)
268+
# Sort the masks by area in descending order
269+
sorted_masks = sorted(masks, key=(lambda x: x["area"]), reverse=True)
270270

271271
# Create an output image with the same size as the input image
272272
objects = np.zeros(
@@ -276,9 +276,10 @@ def save_masks(
276276
)
277277
)
278278
# Assign a unique value to each object
279+
count = len(sorted_masks)
279280
for index, ann in enumerate(sorted_masks):
280281
m = ann["segmentation"]
281-
objects[m] = index + 1
282+
objects[m] = count - index
282283

283284
# Generate a binary mask
284285
else:

samgeo/samgeo2.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ def save_masks(
293293

294294
# Generate a mask of objects with unique values
295295
if unique:
296-
# Sort the masks by area in ascending order
297-
sorted_masks = sorted(masks, key=(lambda x: x["area"]), reverse=False)
296+
# Sort the masks by area in descending order
297+
sorted_masks = sorted(masks, key=(lambda x: x["area"]), reverse=True)
298298

299299
# Create an output image with the same size as the input image
300300
objects = np.zeros(
@@ -304,9 +304,10 @@ def save_masks(
304304
)
305305
)
306306
# Assign a unique value to each object
307+
count = len(sorted_masks)
307308
for index, ann in enumerate(sorted_masks):
308309
m = ann["segmentation"]
309-
objects[m] = index + 1
310+
objects[m] = count - index
310311

311312
# Generate a binary mask
312313
else:

0 commit comments

Comments
 (0)