Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-OpenCV dilate and erode functions don't modify anything

Tags:

python

opencv

Given the code below, the cv2.dilate and cv2.erode functions in python return the same image I send to it. What am I doing wrong? I am using OpenCV3.0.0. and numpy1.9.0 on iPython 2.7

im = np.zeros((100,100), dtype=np.uint8)
im[50:,50:] = 255
dilated = cv2.dilate(im, (11,11))
print np.array_equal(im, dilated)

Which returns:

True

{Edited} The other dilate post represents a question of kernel datatype. This post actually reflects a function call error.

like image 649
DanGoodrick Avatar asked Mar 18 '15 20:03

DanGoodrick


2 Answers

The function requires a kernel, not a kernel size. So a correct function call would be below.

dilated = cv2.dilate(im, np.ones((11, 11)))
like image 188
DanGoodrick Avatar answered Oct 14 '22 05:10

DanGoodrick


You need to specify a proper kernel. It can be rectangular, circular, etc.

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
im = np.zeros((100,100), dtype=np.uint8)
im[50:,50:] = 255
dilated = cv2.dilate(im, kernel, iterations = 1)
like image 40
Specas Avatar answered Oct 14 '22 04:10

Specas