Skip to content

Instantly share code, notes, and snippets.

@ameenkhan07
Created December 3, 2018 02:39
Show Gist options
  • Save ameenkhan07/eeaa6340eb26bd0373c4d96e8d2f0037 to your computer and use it in GitHub Desktop.
Save ameenkhan07/eeaa6340eb26bd0373c4d96e8d2f0037 to your computer and use it in GitHub Desktop.
AISHACK Morph
def _dilate(self, img, str_img=[]):
'''Expands the poindary of img
'''
w, h = img.shape
res = np.asarray([[0 for _ in range(h)] for _ in range(w)])
# Iterating the anchor of the structure image over the original image
for i, im_row in enumerate(img):
for j, im_ele in enumerate(im_row):
if img[i][j]:
for k in range(-1, 2):
for l in range(-1, 2):
if (i+k) >= w or (i+k) < 0 or (l+j) >= h or (l+j) < 0:
continue
else:
res[i+k][j+l] = 255
return(res)
def _erode(self, img, str_img=[]):
'''Contracts the boundary of img
'''
w, h = img.shape
se_w, se_h = str_img.shape
res = np.asarray([[0 for _ in range(h)] for _ in range(w)])
for i, im_row in enumerate(img):
for j, im_ele in enumerate(im_row):
flag = True
for k in range(-1, 2):
for l in range(-1, 2):
if (i+k) >= w or (i+k) < 0 or (l+j) >= h or (l+j) < 0:
continue
if str_img[k][l] != 0 and img[i+k][j+l] == 0:
flag = False
if flag:
res[i][j] = 255
return(np.asarray(res))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment