I'm getting this problem:
I'm using Python and OpenCV.
I'm trying to separate the contours of the touching coins using erode.
I thresholded the image and then tried to apply the erode but nothing happened. I've read the documentation and still don't understand very well how the getStruturingElement
and erode works.
I've thresholded the image.
used erode on the thresholded image.
and still nothing. What am I using wrong here?
Here's part of the code:
import cv2, numpy as np
#1.Reads Image
objectImage = cv2.imread('P1000713s.jpg')
#2.Converts to Gray level
cvtcolorImage = cv2.cvtColor(objectImage,cv2.COLOR_RGB2GRAY)
#3.Thresholds
imgSplit = cv2.split(objectImage)
flag,b = cv2.threshold(imgSplit[2],0,255,cv2.THRESH_OTSU)
#4.Erodes the Thresholded Image
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
cv2.erode(b,element)
cv2.imshow('Eroded',b)
findContours() function, first one is source image, second is contour retrieval mode, third is contour approximation method. And it outputs a modified image, the contours and hierarchy. contours is a Python list of all the contours in the image.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With