Skip to content

Instantly share code, notes, and snippets.

@louismullie
Last active April 3, 2020 20:26
Show Gist options
  • Save louismullie/3f9f7550f7a3a7738b497e37059d64a7 to your computer and use it in GitHub Desktop.
Save louismullie/3f9f7550f7a3a7738b497e37059d64a7 to your computer and use it in GitHub Desktop.
from scipy import ndimage
from geometric_utils import find_largest_object
def extract_body_mask(im):
im_thresholded = im > 90
im_filled = ndimage.binary_erosion(im_thresholded, iterations=1)
im_filled = ndimage.binary_fill_holes(im_filled)
im_filled = ndimage.binary_erosion(im_filled, iterations=3)
body_mask = find_largest_object(im_filled)
return body_mask.astype(int)
def find_largest_object(im):
all_labels = measure.label(im)
max_size, max_region = 0, None
for i in range(0,np.unique(all_labels).shape[0]):
size = im[all_labels == i].shape[0]
if size > max_size and i != im[0,0]:
max_region = i
max_size = size
region = np.zeros(im.shape)
region[all_labels == max_region] = 1
return region
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment