Skip to content

Instantly share code, notes, and snippets.

@mowolf
Created April 15, 2020 16:21
Show Gist options
  • Save mowolf/a193377b8a022c5a94364f4a24e96389 to your computer and use it in GitHub Desktop.
Save mowolf/a193377b8a022c5a94364f4a24e96389 to your computer and use it in GitHub Desktop.
def clean_edges(img, pixels_to_ommit=4):
"""
This function removes the first #pixels_to_ommit pixels that are non black from every side of the image
(top, bottom, left, right)
:param img: image, cv2
:param pixels_to_ommit: integer of how many pixels to remove
:return: image
"""
w, h, _ = img.shape
for _k, _j in [[[0, w, 1], [0, h, 1]], [[0, h, 1], [0, w, 1]], [[w - 1, -1, -1], [h - 1, -1, -1]],
[[h - 1, -1, -1], [w - 1, -1, -1]]]:
for k in range(_k[0], _k[1], _k[2]):
passed_non_black_pixels = 0
for j in range(_j[0], _j[1], _j[2]):
if not (img[k, j, :] == [0, 0, 0]).all():
passed_non_black_pixels += 1
if passed_non_black_pixels > pixels_to_ommit:
break
else:
img[k, j, :] = [0, 0, 0]
return img
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment