gpt4 book ai didi

python - 轮廓元组的长度必须为 2 或 3,否则 opencv 会再次更改其 cv.findcontours 签名

转载 作者:太空宇宙 更新时间:2023-11-03 21:40:17 25 4
gpt4 key购买 nike

运行我的代码后,我收到错误消息轮廓元组的长度必须为 2 或 3,否则 opencv 再次更改了它们的返回签名。我目前正在运行 3.4.3.18 版的 opencv。当我捕获轮廓运行 imutils ver 0.5.2

代码找到计数并返回在进行一些边缘检测后找到的轮廓。该算法然后使用 imutils 来抓取轮廓。这是正确的方法还是有一些最新的方法轮廓而不是使用 imutils?

请看下面的例子:

image, contours, hier = cv.findContours(edged.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)


cnts = imutils.grab_contours(contours)

cnts = sorted(contours, key = cv.contourArea, reverse = True)[:5]

最佳答案

根据 OpenCV 版本,findContours() 具有不同的返回签名。

在 OpenCV 3.4.X 中,findContours()返回 3 个项目

image, contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])

在 OpenCV 4.1.X 中,findContours()返回 2 个项目

contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset]]])

要在不使用 imutils 的情况下手动获取轮廓,您可以检查返回的元组中的项目数量

items = cv.findContours(edged.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
contours = items[0] if len(items) == 2 else items[1]

关于python - 轮廓元组的长度必须为 2 或 3,否则 opencv 会再次更改其 cv.findcontours 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56158518/

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