Created
October 14, 2020 05:26
-
-
Save sharkdeng/1f39732b408023503ded1c5480f21550 to your computer and use it in GitHub Desktop.
crop white of an image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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