python - 轮廓元组的长度必须为 2 或 3,否则 opencv 会再次更改其 cv.findcontours 签名
问题描述
运行我的代码后,我收到错误消息 contours tuple must have length 2 或 3,否则 opencv 再次更改了它们的返回签名。我目前正在运行 3.4.3.18 版的 opencv。当我抓取运行 imutils 版本 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]
推荐阅读
- php - 如何使用 lastinsertid() 函数
- android - 使用android约束布局2.0.0 Flow将项目放置一行
- php - 消息:非法字符串偏移量从 Codeigniter2x 版本升级到 Codeigniter 3x 版本
- c++ - 使用 Poco:Condition 唤醒两个线程
- c++ - 如何在安装了 Visual Studio 2017 的 Windows 10 上使用第三方实用程序(包括 swiftshader 和 node 和 node_modules)构建 CEF
- azure - 无法在我使用 Graph API 获得的 Azure AD 应用程序列表中检索我的应用程序之一
- here-api - 匹配路由 API 的 KML 示例
- react-native - 发出一个事件会导致其他事件在本机反应中触发
- transactions - 如何在不完全同步的情况下仅从区块链接收有关特定地址集的交易?
- apache-flink - 如何在不停机的情况下部署新工作