【问题标题】:Python drawContours method does not anything on the image applied (OpenCV)Python drawContours 方法对应用的图像没有任何作用(OpenCV)
【发布时间】:2023-04-04 09:17:01
【问题描述】:

我正在尝试在我的测试图像周围绘制轮廓。我在后台使用精明的边缘检测。

findContours 方法适用于我的图像,但是当我尝试对该图像执行 drawContours 方法时。它不显示任何内容。

这是我尝试过的

import cv2
import numpy as np

image = cv2.imread('/path/to/image.jpg')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (11, 11), 0)
cv2.imshow("blurred", blurred)

canny = cv2.Canny(blurred, 30, 150)

(_, cnts, _) = cv2.findContours(canny.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

print "Contours in the image, %d" % (len(cnts))

shape = image.copy()
cv2.drawContours(shape.copy(), cnts, -1, (0, 255, 0), 2)
cv2.imshow("Edges", shape)

根据我从文档中收集到的信息,drawContours 方法的第四个参数将用于指定要绘制的边缘的颜色。但它没有显示任何东西,而不是我期待的绿色边缘。

len(cnts) 为我返回 2

这是我正在尝试的图像

我使用的是opencv 3.0.0版

相关SO question

编辑:将cv2.findContours() 的第三个参数更改为cv2.CHAIN_APPROX_NONE 后,它仍然没有在最终的cv2.imshow("Edges", shape) 图像上显示最终的绿色边缘(或任何颜色) .这是我从精明的边缘图像中得到的结果

【问题讨论】:

  • 试试cv2.CHAIN_APPROX_NONE
  • @ZdaR 请检查已编辑的问题

标签:
python
opencv
image-processing
edge-detection
opencv-contour