python - 使用 drawContours() thickness=-1 填充轮廓将不起作用

标签 python opencv image-processing computer-vision contour

<分区>

我正在尝试填充通过分别对 3 个颜色 channel 进行阈值处理而获得的轮廓。

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)

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

cv2.drawContours(image_binary, max(contours, key = cv2.contourArea), -1, (255, 255, 255), thickness=-1)

结果如下,

enter image description here enter image description here

除了轮廓厚度为 1 的二值填充图像之外,它应该提出一个二进制填充图像

最佳答案

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

cv2.drawContours(image_binary, max(contours, key = cv2.contourArea), -1, 255, thickness=-1)

cv2.drawContours(image_binary, [max(contours, key = cv2.contourArea)], -1, 255, thickness=-1)

解决了。

enter image description here

关于python - 使用 drawContours() thickness=-1 填充轮廓将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55057405/

相关文章:

python - 标准Django/python单位转换包

python - 奇怪的 MySQL Python mod_wsgi Can't connect to MySQL server on 'localhost' (49) 问题

c++ - Cocoa 应用程序(OS X 应用程序)中的 OpenCV

opencv - 使用opencv删除硬币阴影

android - QR 码的定位角

python - 在图像上找到一个 3x3 的滑动窗口

python - 使用另一个数据帧中的行号从现有数据帧创建新的 pandas 数据帧

python - 使用Vue + Django模板语言时如何解决双花括号问题?

python - 使用 cv2 时删除 [h264 @ xxx] 错误控制台输出

opencv - OpenCV cv::resize()可以更改图像的最小值和最大值吗? (使用INTER_LINEAR方法)