Skip to content

Instantly share code, notes, and snippets.

@bodokaiser
Created March 6, 2017 08:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bodokaiser/b5697f95336be11aa09f6e246901e0d4 to your computer and use it in GitHub Desktop.
Save bodokaiser/b5697f95336be11aa09f6e246901e0d4 to your computer and use it in GitHub Desktop.
Crops a pair of MRI and US images such that there are no zero areas.
import numpy as np
from skimage.filters import threshold_otsu
class RegionCrop(object):
def __call__(self, mr: np.ndarray, us: np.ndarray):
if np.any(mr) and np.any(us):
mask = us > threshold_otsu(us)
x = np.where(np.any(mask, 0))[0][[0, -1]]
y = np.where(np.any(mask, 1))[0][[0, -1]]
if np.abs(np.diff(x)[0]) < 10 or np.abs(np.diff(y)[0]) < 10:
# "mark" samples which are too small to be filtered
return np.zeros_like(us), np.zeros_like(mr)
mr = mr[y[0]:y[1], x[0]:x[1]]
us = us[y[0]:y[1], x[0]:x[1]]
return mr, us
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment