Created
June 16, 2018 18:47
-
-
Save manashmandal/dc9d3376a5a7c8def2686d5fba380084 to your computer and use it in GitHub Desktop.
This file contains 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
def crop(image, ws=2, margin=7, scale_factor=0.5): | |
actual_border = None | |
img = np.copy(image) | |
hist = np.sum(img, axis=0) | |
# Keep only left half | |
hist = hist[ : len(hist) // 3 ] | |
border = np.argmax(hist) | |
# Applying NMS | |
hist[ border - ws: border ] = 0 | |
hist[ border + 1: border + ws] = 0 | |
# Finding the second border if exists | |
inner_border = np.argsort(hist)[::-1][1] | |
print("INNER BORDER : {} - BORDER : {} - HEIGHT INNER BORDER : {} - HEIGHT BORDER: {}".format( inner_border, border, hist[inner_border], hist[border] )) | |
# Get the border range | |
border_range = np.abs(border - inner_border) | |
if (hist[border] >= hist[inner_border]) and (border > inner_border): | |
return border | |
else: | |
return inner_border | |
if (hist[inner_border] >= hist[border]) and ( border_range <= margin ): | |
return border | |
else: | |
return inner_border | |
if hist[inner_border] >= hist[border] * scale_factor: | |
return inner_border |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment