使用 drawContours() 厚度 =-1 无法填充轮廓

Ish*_*oon 1 opencv computer-vision python-3.x opencv3.0 opencv-contour

我正在尝试填充通过单独阈值处理 3 个颜色通道获得的轮廓。

image_original = cv2.imread(original_image_path)
image_contours = np.zeros((image_original.shape[0], image_original.shape[1], 1), dtype=np.uint8)
image_contour = np.zeros((image_original.shape[0], image_original.shape[1], 1), dtype=np.uint8)
image_binary = np.zeros((image_original.shape[0], image_original.shape[1], 1), dtype=np.uint8)
image_area = image_original.shape[0] * image_original.shape[1]
for channel in range(image_original.shape[2]):
    ret, image_thresh = cv2.threshold(image_original[:, :, channel], 120, 255, cv2.THRESH_OTSU)
    _, contours, hierarchy = cv2.findContours(image_thresh, 1, 1)
    for index, contour in enumerate(contours):
        if( cv2.contourArea( contour )  > image_area * background_remove_offset ):
            del contours[index]
    cv2.drawContours(image_contours, contours, -1, (255,255,255), 3)
_, contours, hierarchy = cv2.findContours(image_contours,  cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(image_contour, max(contours, key = cv2.contourArea), -1, (255, 255, 255), 1)
cv2.imwrite(output_contour_image_path, image_contour)
cv2.drawContours(image_binary, max(contours, key = cv2.contourArea), -1, (255, 255, 255), thickness=-1)
cv2.imwrite(output_binary_image_path, image_binary)
cv2.imshow("binary", image_binary)
Run Code Online (Sandbox Code Playgroud)

这应该通过设置厚度 = -1 来工作,但它只在下一行中绘制与厚度 = 1 相同的 1 个厚度的轮廓。

cv2.drawContours(image_binary, max(contours, key = cv2.contourArea), -1, (255, 255, 255), thickness=-1)
Run Code Online (Sandbox Code Playgroud)

结果如下,

在此处输入图片说明 在此处输入图片说明

哪个应该提出一个二进制填充图像,而不是仅具有厚度 = 1 的轮廓的图像

Ish*_*oon 8

好吧,解决了它似乎 drawContours() 函数需要轮廓作为列表类型,只需更改行

cv2.drawContours(image_binary, max(contours, key = cv2.contourArea), -1, 255, thickness=-1)
Run Code Online (Sandbox Code Playgroud)

cv2.drawContours(image_binary, [max(contours, key = cv2.contourArea)], -1, 255, thickness=-1)
Run Code Online (Sandbox Code Playgroud)

解决它。

在此处输入图片说明