Home > OS >  Why contours found using findContours() are all broken up into pieces when the Canny Image is so cle
Why contours found using findContours() are all broken up into pieces when the Canny Image is so cle

Time:11-04

On the left is my Canny Image. On the right is my Original Image with the contours marked with different colors.

This is bugging me... Why when the Canny Image is so clear, the cv2.findContours() function is still not giving me a single enclosing contour that wraps the big rectangle ??

enter image description here

Original Input Image: enter image description here

My code:

# [get colored raw image]
rawIm = cv2.imread("sample1.jpg");

# [gray]
print("--------- Image Modification: Gray and Invert --------")
grayIm = cv2.cvtColor(rawIm, cv2.COLOR_BGR2GRAY);
grayIm = inverte(grayIm) # invert the black to white

# [blurring]
print("--------- Image Modification: Blur --------")
blurredIm = cv2.medianBlur(grayIm, 5);

# [dilate]
print("--------- Image Modification: Dilated --------")
kernel = np.ones((7,7), np.uint8)
dilatedIm = cv2.dilate(blurredIm, kernel, iterations=1)

# [get edges]
cannyIm = cv2.Canny(dilatedIm, lowThreshold, lowThreshold*cannyThresholdRatio)

# [FIND CONTOURS]
cnts, hier = cv2.findContours(cannyIm.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

# [DRAW CONTOURS]
for cnt in cnts:
        color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
        cv2.drawContours(rawIm, [cnt], -1, color , 6)

cv2.imshow("CANNY", resize(cannyIm, percentageShrink))
cv2.imshow("CONTOURS", resize(rawIm, percentageShrink))

Is this problem?

cv2.findContours(cannyIm.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

Must I use different parameters, other than "cv2.RETR_LIST" and "cv2.CHAIN_APPROX_SIMPLE"?

CodePudding user response:

Ok. Dilation works. Adding cannyIm = cv2.dilate(cannyIm, kernel, iterations=1) after the Canny process did the trick.

Full Code

# [get colored raw image]
rawIm = cv2.imread("sample1.jpg");

# [gray]
print("--------- Image Modification: Gray and Invert --------")
grayIm = cv2.cvtColor(rawIm, cv2.COLOR_BGR2GRAY);
grayIm = inverte(grayIm) # invert the black to white

# [blurring]
print("--------- Image Modification: Blur --------")
blurredIm = cv2.medianBlur(grayIm, 5);

# [dilate]
print("--------- Image Modification: Dilated --------")
kernel = np.ones((7,7), np.uint8)
dilatedIm = cv2.dilate(blurredIm, kernel, iterations=1)

# [get edges]
cannyIm = cv2.Canny(dilatedIm, lowThreshold, lowThreshold*cannyThresholdRatio)
cannyIm = cv2.dilate(cannyIm, kernel, iterations=1)

# [FIND CONTOURS]
cnts, hier = cv2.findContours(cannyIm.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

# [DRAW CONTOURS]
for cnt in cnts:
        color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
        cv2.drawContours(rawIm, [cnt], -1, color , 6)

cv2.imshow("CANNY", resize(cannyIm, percentageShrink))
cv2.imshow("CONTOURS", resize(rawIm, percentageShrink))

Thank you @fmw42

enter image description here

  • Related