查找轮廓函数中的 opencv ValueError - 其他解决方案不起作用

发布于 2025-01-17 07:09:35 字数 1071 浏览 0 评论 0原文

我一直在使用此处的一个小Python程序。 这一直工作得很好,直到最近我不得不擦除并重新安装我的 RaspberryPi,现在重新安装了包括 Python 和 opencv 在内的所有内容后,我似乎遇到了一些向后兼容性问题,代码中的以下行失败并抛出以下错误:

Code

thresh = cv2.dilate(thresh, None, iterations=2)
(_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

This扩大阈值图像以填充任何孔,然后在阈值图像上查找轮廓

错误

File "/home/pi/carspeed/carspeed.py", line 228, in <module>
    (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)

我尝试了在网上找到的不同方法,包括将行更改为此,但无济于事

(_, cnts, _) = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

(我在网上找到的答案是:

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

但是由于原始代码正在设置(_, cnts, _) 变量,我以为我别无选择并使用了相同的。)

I've been using a little Python program from here.
This has been working great until recently I had to wipe and reinstall my RaspberryPi, and now after reinstalling everything including Python and opencv, I seem to have some backward compatibility issue, the following line in the code fails and throwing the error below:

Code

thresh = cv2.dilate(thresh, None, iterations=2)
(_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

This to dilate the thresholded image to fill in any holes, then find contours on thresholded image

Error

File "/home/pi/carspeed/carspeed.py", line 228, in <module>
    (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
ValueError: not enough values to unpack (expected 3, got 2)

I tried different things I found online, including changing the lines to this, but to no avail

(_, cnts, _) = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

(The answer I found online is:

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

But since the original code is setting the (_, cnts, _) variable, I thought I had no choice and used the same.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2025-01-24 07:09:35

您正在访问的存储库正在使用 OpenCV 3.x。但是您使用的是 OpenCV 4.x。 OpenCV 4 中 findContours 的输出发生了变化

OpenCV4 中缺少什么变量?

如所述文档,OpenCV3 的 findContours 方法也用于返回源图像本身,这在您的情况下也是不必要的。如果您想再次使用源图像,只需创建一个新变量并调用copy = img.copy即可。

OpenCV3.x 的 findContours 用法示例

OpenCV4.x 的 findContours 用法示例

The repository you're addressing is using OpenCV 3.x. However You're using OpenCV 4.x. Output of findContours is changed in OpenCV 4

What variable is missing in OpenCV4?

As stated in the documentation, OpenCV3's findContours method used to return the source image itself too, which also is unnecessary in your case. If you want to use source image again, you can just create a new variable and call copy = img.copy.

Example findContours usage for OpenCV3.x

Example findContours usage for OpenCV4.x

苏别ゝ 2025-01-24 07:09:35

这已经解决了——
最新版本的opencv的新代码应该是这样的:

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

This is solved -
The new code for the latest version of opencv should like this:

(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文