【问题标题】:pyopencv drawContourspyopencv drawContours
【发布时间】:2011-03-28 01:46:54
【问题描述】:

我使用 pyopencv 查找轮廓,但无法绘制找到的轮廓。我得到了错误:

 23 color = Scalar(255)
 24 print type(color)

---> 25 drawContours(img, list(contours), -1, color) 26 27 imshow('Xe may - 0', img)

ArgumentError:Python 参数类型 在 pyopencv.pyopencvext.drawContours(垫, list, int, Scalar) 与 C++ 不匹配 签名: drawContours(cv::Mat {lvalue} 图像, 标准::向量, 标准::分配器 > >, 标准::分配器, 标准::分配器 > > > > 轮廓,int轮廓Idx, cv::Scalar_ 颜色,整数 厚度=1,int lineType=8, 标准::向量, 标准::分配器 >> 层次结构=vector_Vec4i(len=0, []), int maxLevel=2147483647, cv::Point_ 偏移量=Point2i(x=0, y=0)) 警告: 执行文件失败:

这是我的代码

# load image
img = imread('37S2231.jpg')
# gray scale
out = img.clone()
cvtColor(img, out, CV_RGB2GRAY)
# equalizes the histogram of a grayscale image
# increases the contrast of the image
out2 = out.clone()
equalizeHist(out, out2)
# canny to extract edges
out3 = out2.clone()
Canny(out2, out3, 150, 200)
# threshold
out4 = out3.clone()
threshold(out3, out4, 254, 255, THRESH_BINARY)
# contours
contours = findContours(out4, 1, 1)
print type(contours)
color = Scalar(255)
print type(color)
drawContours(img, list(contours), -1, color)

我检查了http://packages.python.org/pyopencv/2.1.0.wr1.0.2/ 的drawContours 函数,但它看起来与我的代码相似。我是不是做错了什么?

谢谢

【问题讨论】:

    标签: opencv


    【解决方案1】:

    首先感谢这个例子,这是我发现的唯一一个说明 pyopencv.findContours 用法的例子。对于您的问题:使用轮廓 [0] 而不是列表(轮廓)!所以,把最后一行改成

    drawContours(img, contours[0], -1, color)
    

    【讨论】:

    • 在这种情况下,对于 OpenCV 3.x 和 4.0,contours 是一个元组,因为 findContours 返回三个参数:findContours(image, mode, method[, contours[, hierarchy[, offset]]]) ->图像,轮廓,层次结构因为轮廓不是第一个返回的参数,而是第二个:drawContours(img, contours[1], -1, color)
    猜你喜欢
    • 2012-10-24
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 2019-02-01
    • 2018-03-10
    • 2015-10-07
    相关资源
    最近更新 更多