Skip to content
Snippets Groups Projects
Commit 6cf4f1c0 authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Overloaded mask_largest_object to include both object maps and binary masks as inputs

parent d2b233d0
No related branches found
No related tags found
No related merge requests found
import numpy as np import numpy as np
from skimage.measure import label, regionprops_table import skimage
from model_server.process import is_mask
def mask_largest_object( def mask_largest_object(
img: np.ndarray, img: np.ndarray,
...@@ -9,14 +10,15 @@ def mask_largest_object( ...@@ -9,14 +10,15 @@ def mask_largest_object(
) -> np.ndarray: ) -> np.ndarray:
""" """
Where more than one connected component is found in an image, return the largest object by area Where more than one connected component is found in an image, return the largest object by area
:param img: (np.ndarray) containing object labels :param img: (np.ndarray) containing object labels or binary mask
:param max_allowed: raise an error if more than this number of objects is found :param max_allowed: raise an error if more than this number of objects is found
:param verbose: print a message each time more than one object is found :param verbose: print a message each time more than one object is found
:return: np.ndarray of same size as img :return: np.ndarray of same size as img
""" """
binary = img > 0 if is_mask(img): # assign object labels
ob_id = label(binary) ob_id = skimage.measure.label(img)
# separate problem for mask and object maps else: # assume img is contains object labels
ob_id = img
# import skimage # import skimage
# from pathlib import Path # from pathlib import Path
...@@ -28,9 +30,11 @@ def mask_largest_object( ...@@ -28,9 +30,11 @@ def mask_largest_object(
if num_obj > 1: if num_obj > 1:
if verbose: if verbose:
print(f'Found {num_obj} nonzero unique values in object map; keeping the one with the largest area') print(f'Found {num_obj} nonzero unique values in object map; keeping the one with the largest area')
pr = regionprops_table(ob_id, properties=['label', 'area']) # pr = regionprops_table(ob_id, properties=['label', 'area'])
idx_max_area = pr['area'].argmax() val, cts = np.unique(ob_id, return_counts=True)
mask = ob_id == pr['label'][idx_max_area] mask = ob_id == val[1 + cts[1:].argmax()]
# idx_max_area = pr['area'].argmax()
# mask = ob_id == pr['label'][idx_max_area]
return mask * img return mask * img
else: else:
return img return img
......
...@@ -5,17 +5,28 @@ import numpy as np ...@@ -5,17 +5,28 @@ import numpy as np
from extensions.chaeo.process import mask_largest_object from extensions.chaeo.process import mask_largest_object
class TestMaskLargestObject(unittest.TestCase): class TestMaskLargestObject(unittest.TestCase):
def test_mask_largest_object(self): def test_mask_largest_touching_object(self):
arr = np.zeros([5, 5]) arr = np.zeros([5, 5], dtype='uint8')
arr[0:3, 0:3] = 2 arr[0:3, 0:3] = 2
arr[4, 2:5] = 4 arr[3:, 2:] = 4
masked = mask_largest_object(arr) masked = mask_largest_object(arr)
self.assertTrue(np.all(np.unique(masked) == [0, 2])) self.assertTrue(np.all(np.unique(masked) == [0, 2]))
self.assertTrue(np.all(masked[4:5, :] == 0)) self.assertTrue(np.all(masked[4:5, 0:2] == 0))
self.assertTrue(np.all(masked[:, 4:5] == 0)) self.assertTrue(np.all(masked[0:3, 3:5] == 0))
def test_no_change(self): def test_no_change(self):
arr = np.zeros([5, 5]) arr = np.zeros([5, 5], dtype='uint8')
arr[0:3, 0:3] = 2 arr[0:3, 0:3] = 2
masked = mask_largest_object(arr) masked = mask_largest_object(arr)
self.assertTrue(np.all(masked == arr)) self.assertTrue(np.all(masked == arr))
def test_mask_multiple_objects_in_binary_maks(self):
arr = np.zeros([5, 5], dtype='uint8')
arr[0:3, 0:3] = 255
arr[4, 2:5] = 255
masked = mask_largest_object(arr)
print(np.unique(masked))
self.assertTrue(np.all(np.unique(masked) == [0, 255]))
self.assertTrue(np.all(masked[:, 3:5] == 0))
self.assertTrue(np.all(masked[3:5, :] == 0))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment