Skip to content

Instantly share code, notes, and snippets.

@dnlcrl
Created January 25, 2018 08:56
Show Gist options
  • Save dnlcrl/622dcf4be0403345dab7bd19a6998064 to your computer and use it in GitHub Desktop.
Save dnlcrl/622dcf4be0403345dab7bd19a6998064 to your computer and use it in GitHub Desktop.
def bbox(img):
rows = np.any(img, axis=1)
cols = np.any(img, axis=0)
rmin, rmax = np.where(rows)[0][[0, -1]]
cmin, cmax = np.where(cols)[0][[0, -1]]
return slice(rmin, rmax), slice(cmin, cmax)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment