Skip to content

Instantly share code, notes, and snippets.

@sharkdeng
Created October 14, 2020 05:26
Show Gist options
  • Save sharkdeng/1f39732b408023503ded1c5480f21550 to your computer and use it in GitHub Desktop.
Save sharkdeng/1f39732b408023503ded1c5480f21550 to your computer and use it in GitHub Desktop.
crop white of an image
# Source: https://www.kaggle.com/lopuhin/panda-2020-level-1-2
def crop_white(image: np.ndarray, value: int = 255) -> np.ndarray:
assert image.shape[2] == 3
assert image.dtype == np.uint8
ys, = (image.min((1, 2)) < value).nonzero()
xs, = (image.min(0).min(1) < value).nonzero()
if len(xs) == 0 or len(ys) == 0:
return image
return image[ys.min():ys.max() + 1, xs.min():xs.max() + 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment