2

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.

Jason Oliver
  • 21
  • 1
  • 4
  • Your kernel looks suspicious. `kernel = np.ones((3, 3), dtype='uint8')` should give you better results. – beaker Mar 22 '22 at 17:35
  • It's better that you share an image of your output. – Esraa Abdelmaksoud Mar 22 '22 at 22:58
  • @EsraaAbdelmaksoud Sadly my dataset contains images which may not be publicly shared. – Jason Oliver Mar 23 '22 at 09:55
  • 1
    Jason, I don't think that @EsraaAbdelmaksoud was asking for you to post proprietary data. I think you did a great job of posting a reproducible example without using your actual data. I think what Esraa was asking for was the resulting image from your code, which is generally a really good thing to have. In this case I don't think it was necessary because it was fairly simple to reproduce, but that's just my opinion. Also, you really shouldn't add solutions to the question itself. It would be better to post the solution as an answer below. – beaker Mar 23 '22 at 14:26
  • Thanks for the feedback, I will add a picture of the sample data next time. Same goes for the solution. – Jason Oliver Mar 30 '22 at 08:38

1 Answers1

0

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()
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Jason Oliver
  • 21
  • 1
  • 4