Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SVivdich02 committed Mar 5, 2024
1 parent 2e8ffff commit 0013a97
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/services/preprocessing/init/instances_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ def masks_to_image(self, masks):
image_labels[mask["segmentation"]] = i + 1
return image_labels

def reduce_detail(self, masks, intersection_to_union_threshold=0.35, intersection_to_mask_threshold=0.6):
def reduce_detail(
self,
masks,
int_to_union_threshold=0.35,
int_to_mask_threshold=0.6,
):
"""Reducing the detail of masks by combining several masks into one
Parameters
Expand All @@ -130,19 +135,19 @@ def reduce_detail(self, masks, intersection_to_union_threshold=0.35, intersectio
"area" : int
the area in pixels of the mask
intersection_to_union_threshold : float
int_to_union_threshold : float
two masks whose ratio of the intersection area of the bounding boxes
to the union area is greater than or equal this threshold will be merged into one mask
intersection_to_mask_threshold : float
int_to_mask_threshold : float
threshold for the ratio of the number of pixels in the intersection of masks
to the number of pixels in one mask
for example, if half the pixels of a mask belong to the intersection
with another mask, then combine them into one mask
"""

merged_mask = []
merged_indices = [] #indices in the original masks that were merged
merged_indices = [] # indices in the original masks that were merged

for i in range(len(masks)):
if i in merged_indices:
Expand All @@ -167,15 +172,16 @@ def reduce_detail(self, masks, intersection_to_union_threshold=0.35, intersectio
IU_ratio = area_bbox_intersection / area_bbox_union

if (
IU_ratio >= intersection_to_union_threshold
or area_intersection / masks[i]["area"] >= intersection_to_mask_threshold
or area_intersection / masks[j]["area"] >= intersection_to_mask_threshold
IU_ratio >= int_to_union_threshold
or area_intersection / masks[i]["area"] >= int_to_mask_threshold
or area_intersection / masks[j]["area"] >= int_to_mask_threshold
):
masks[i] = find_union_mask(masks[i], masks[j]) #the union is placed in the masks[i]
masks[i] = find_union_mask(masks[i], masks[j])
indices_merged_with_i.append(j)

if indices_merged_with_i:
merged_mask.append(masks[i]) #the final result of the union is extracted from the masks[i]
# the final result of the union is extracted from the masks[i]
merged_mask.append(masks[i])

merged_indices.append(i)
for ind in indices_merged_with_i:
Expand All @@ -184,7 +190,7 @@ def reduce_detail(self, masks, intersection_to_union_threshold=0.35, intersectio
masks_result = []
for ind, mask in enumerate(masks):
if ind not in merged_indices:
masks_result.append(mask) #save it unchanged
masks_result.append(mask) # save it unchanged

for mask in merged_mask:
masks_result.append(mask)
Expand Down

0 comments on commit 0013a97

Please sign in to comment.