how do I separate contours of touching objects using erode?

I know this is an old question, but I had similar problems, and found this problem via Google.

As far as I know cv2.erode() doesn't change the source image, instead it returns a new image with the change applied.

changing your line containing the erode call to:

b = cv2.erode(b,element)

should let you see the changes when you call the cv2.imshow(...,b)


Looking at your image, it's possible that a 3x3 cross mask will always stay within the thresholded area. Rather than using MORPH_CROSS, use MORPH_ELLIPSE.

If the coins are still "touching" after one call, you could always run multiple calls to erode, but be warned that this will have a destructive effect on your image.


I know this is a very old thread but I thought my answer can help someone. I was facing the same problem. There is always loss of shape property information if erode is used to separate contours. I followed this link: watershed segmentation OpenCV where Watershed segmentation is used to separate the contours and the results are quite accurate.