【发布时间】:2021-07-19 04:15:00
【问题描述】:
我有一个图像,我想使用 cv.drawContours 显示所有轮廓,但它错过了两个重叠单元格之间的一个,即使 cv2.Canny 清楚地显示了所有轮廓,我也不知道为什么。 这是我使用的代码:
import cv2
import imageio
from skimage import io, color
import numpy as np
from google.colab.patches import cv2_imshow
image = imageio.imread(path+'3.png')
print(image.shape)
image = color.gray2rgb(image)
print(image.shape)
cv2_imshow(image)
cv2.waitKey(0)
# Grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find Canny edges
edged = cv2.Canny(gray, 30, 200)
cv2.waitKey(0)
# Finding Contours
# Use a copy of the image e.g. edged.copy()
# since findContours alters the image
contours, hierarchy = cv2.findContours(edged.copy(),
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2_imshow(edged)
cv2.waitKey(0)
print("Number of Contours found = " + str(len(contours)))
# Draw all contours
# -1 signifies drawing all contours
img2=cv2.drawContours(image, contours, -1, (100,0,0), 2)
print(len(contours))
print(img2.shape)
cv2_imshow(image)
cv2.waitKey(0)
cv2.destroyAllWindows()
这是输入图像:input image,这是精巧磨边后的图像edged,这是 img2 在 cv2.drawCONtours img2 之后的最终图像,你可以看到它错过了 2 之间的轮廓中间有重叠的单元格。如果您能帮我解决这个问题,我将不胜感激。
【问题讨论】:
-
canny也有差距。我不明白这个问题。