Ask Your Question

Dilation not working

asked 2017-08-30 04:22:43 -0600

fj_abbasi gravatar image

I have a distance transformed image and I have to return an image in which each pixel is assigned a highest value in the neighbourhood using a 3x3 grid. I am using morphological Dilation for that, but dilation doesn't seem to be working. It returns the same image.

# load the image, convert it to grayscale, and blur it slightly
image = cv2.imread(r'C:\Users\x\Desktop\sampleImg\ecu.tif')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)

#apply Canny edge detection  
canny_img = cv2.Canny(blurred, 150, 200)

#apply Distance Transform
invert_canny= 255- canny_img  
dist_trans= cv2.distanceTransform(invert_canny, cv2.DIST_L2, 3)

#normalize to visualize dist-transformed img 
cv2.normalize(dist_trans, dist_trans, 0.0, 1.0, cv2.NORM_MINMAX)

# apply dilation
kernel=np.ones((3,3), np.uint8)
dilate=cv2.dilate(dist_trans,kernel, iterations=1)
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2017-08-30 08:47:42 -0600

VxW gravatar image

updated 2017-08-30 08:49:03 -0600

so in principle your code is working, but dilation should not be the (only) function of your choice if you would like to have a pixel assignment of the highest value in a 3x3 neighbourhood grid. What you are doing here is a grayscale dilation. If you are using a 3x3 grid the changes are really small.

I guess you will need a maximum Filter. Have a look at:

or maybe this is working for you:

from scipy import ndimage as ndi


image_max = ndi.maximum_filter(dist_trans, size=3, mode='constant') #change size to see diferent results

hope it helps

edit flag offensive delete link more

answered 2017-08-31 09:35:09 -0600

vps gravatar image

3*3 grid id very small. Try with large grid. Also, you can make trackbar for dilation operation. So, you can change the kernel size and type of the mask. See this


edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-08-30 04:22:43 -0600

Seen: 1,710 times

Last updated: Aug 31 '17