Why does open cv dilation only work downwards?

246 views Asked by At

Initial Question:

I thought the following code should lead to the initial white pixel expanding, but this does not happen. How can I make dilate work in all directions?

img = np.zeros((11, 11), dtype='uint8')
img[5][5] = 255

dilated = cv2.dilate(img, (5, 5), iterations=3)

cv2.imshow("img", img)
cv2.imshow("dilated", dilated)
cv2.waitKey()

I tried running the code above and get a vertical line from the centre down. This seems to conflict with the description of dilate here. Why does this happen?

I am using python 3.9.7 and opencv 4.5.2.

1

There are 1 answers

0
Jason Oliver On

As stated by @beaker in the comment, the problem I was experiencing was that my kernel was incorrectly defined. The following code yields the desired result:

img = np.zeros((11, 11), dtype='uint8')
img[5][5] = 255

kernel = np.ones((3, 3), dtype='uint8')

dilated = cv2.dilate(img, kernel, iterations=3)
cv2.imshow("img", img)
cv2.imshow("dilated", dilated)
cv2.waitKey()