We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
for i in range(image.shape[0] - 1):
for j in range(image.shape[1] - 1):
if image[i][j] == 1:
count += 1
elif (
image[i-1][j] == 1 or
image[i+1][j] == 1 or
image[i][j-1] == 1 or
image[i][j+1] == 1 or
image[i-1][j-1] == 1 or
image[i-1][j+1] == 1 or
image[i+1][j-1] == 1 or
image[i+1][j+1] == 1
):
count += 1
return count
result = dilation(img)
print(int(result))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Morphological Operations: Dilation
You are viewing a single comment's thread. Return to all comments →
import numpy as np
img = np.array([ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ])
def dilation(image): count = 0
result = dilation(img) print(int(result))