gpt4 book ai didi

python - OpenCV3.3.0 findContours错误

转载 作者:行者123 更新时间:2023-12-02 16:54:52 29 4
gpt4 key购买 nike

我今天重新安装了opencv,并运行我之前编写的代码。
我得到了错误:

OpenCV Error: Assertion failed (_contours.empty() || (_contours.channels() == 2 && _contours.depth() == CV_32S)) in findContours, file /tmp/opencv-20170916-87764-1y5vj25/opencv-3.3.0/modules/imgproc/src/contours.cpp, line 1894 Traceback (most recent call last): File "pokedex.py", line 12, in (cnts, _) = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE, (2,2)) cv2.error: /tmp/opencv-20170916-87764-1y5vj25/opencv-3.3.0/modules/imgproc/src/contours.cpp:1894: error: (-215) _contours.empty() || (_contours.channels() == 2 &&_contours.depth() == CV_32S) in function findContours



该代码在opencv2.41.3.3。中正常工作。

码:
image = cv2.imread("test.jpg")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) // `len(gray.shape)` is 2.

(cnts, _) = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE, (2,2))

版本信息:opencv 3.3.0,python 2.7.13,macOS 10.13

最佳答案

  • 什么是(2,2)findContours()的第四个位置参数是输出contours数组。但是,您没有为contours数组(这是点数组)传递有效格式。如果应该使用offset,而又不想提供其他位置参数,则需要使用像offset=(2,2)这样的关键字来调用它。这就是实际错误的原因。我不确定为什么它在以前的版本中有效,因为它以相同的顺序接受了相同的参数,而Python一直都是这样。如果参数是可选的,则需要提供足够的位置参数,直到参数为止,或者为其提供关键字。
  • findContours()在OpenCV 3中返回三个值(在OpenCV 2中,它只是两个值),contours是第二个返回值;应该
    _, contours, _ = findContours(...) 

    另外,您不必包装在python中的tuple中进行分配,您只需执行x, y, z = fun()即可,而无需执行(x, y, z) = fun()。另外,您可以通过索引结果来要求第二个返回值,例如
    contours = cv2.findContours(...)[1]

  • 因此,这应该使您神清气爽:
    cnts = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE, offset=(2,2))[1]

    These docs for OpenCV 3具有Python语法,因此如果其他任何先前的代码中断,则可以浏览那里,并查看语法是否已更改。

    关于python - OpenCV3.3.0 findContours错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46462198/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com