调用 cv2.findContours 解压的值太多 [英] too many values to unpack calling cv2.findContours

查看:46
本文介绍了调用 cv2.findContours 解压的值太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 初学者.我试图运行此代码:

#applying 关闭函数内核 = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))关闭 = cv2.morphologyEx(th3, cv2.MORPH_CLOSE, 内核)#finding_contours(cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)对于 cnts 中的 c:peri = cv2.arcLength(c, True)approx = cv2.approxPolyDP(c, 0.02 * peri, True)cv2.drawContours(frame, [approx], -1, (0, 255, 0), 2)

当我召唤 mask.py 时,我得到了这个 ValueError :

回溯(最近一次调用最后一次):文件mask.py",第 22 行,在 <module> 中(cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)ValueError:解包的值太多

这段代码有什么问题?

解决方案

看来您正在使用 OpenCV 3.x 版,同时编写适用于 2.x 分支的代码.这两个分支之间有一些 API 更改.由于您使用的是 Python,因此您可以使用方便的帮助 - 请务必使用它以及文档.

OpenCV 2.x:

<预><代码>>>>导入 cv2>>>帮助(cv2.findContours)模块 cv2 中内置函数 findContours 的帮助:findContours(...)findContours(图像,模式,方法[,轮廓[,层次[,偏移]]])->轮廓,层次

OpenCV 3.x:

<预><代码>>>>导入 cv2>>>帮助(cv2.findContours)内置函数 findContours 的帮助:findContours(...)findContours(图像,模式,方法[,轮廓[,层次[,偏移]]])->图像、轮廓、层次

这意味着在您的脚本中使用 OpenCV 3.x 时调用 findContours 的正确方法类似于

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

<小时>

更新(2018 年 12 月)

在 OpenCV 4.x 中,findContours 只返回 2 个值.

<预><代码>>>>帮助(cv2.findContours)内置函数 findContours 的帮助:findContours(...)findContours(图像,模式,方法[,轮廓[,层次[,偏移]]])->轮廓,层次.@brief 在二值图像中查找轮廓.

I am a python beginner . I was trying to run this code :

#applying closing function 
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
closed = cv2.morphologyEx(th3, cv2.MORPH_CLOSE, kernel)

#finding_contours 
(cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    cv2.drawContours(frame, [approx], -1, (0, 255, 0), 2)

when I summon the mask.py I got this ValueError :

Traceback (most recent call last):
  File "mask.py", line 22, in <module>
    (cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
ValueError: too many values to unpack

what is wrong with this code ?

解决方案

It appears that you're using OpenCV version 3.x, while writing code intended for the 2.x branch. There were some API changes between those two branches. Since you're using Python, you have a handy help available -- make sure to use it, along with the documentation.

OpenCV 2.x:

>>> import cv2
>>> help(cv2.findContours)
Help on built-in function findContours in module cv2:

findContours(...)
    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy

OpenCV 3.x:

>>> import cv2
>>> help(cv2.findContours)
Help on built-in function findContours:

findContours(...)
    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> image, contours, hierarchy

This means that in your script the correct way to call findContours when using OpenCV 3.x would be something like

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


UPDATE (Dec 2018)

In OpenCV 4.x, findContours returns 2 values only.

>>> help(cv2.findContours)
Help on built-in function findContours:

findContours(...)
    findContours(image, mode, method[, contours[, hierarchy[, offset]]]) -> contours, hierarchy
    .   @brief Finds contours in a binary image.

这篇关于调用 cv2.findContours 解压的值太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆