使用cv2.findContours()与python时出错 [英] Error using cv2.findContours(), with python

查看:4178
本文介绍了使用cv2.findContours()与python时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始在Python上学习OpenCV。

I've recently started learning OpenCV on Python.

我指的是这个教程,以获得一些关于获取图像轮廓的帮助。

I'm referring to this tutorial here, to get some help on getting the contours of an image.

我的代码是 -

import cv2
import numpy as np

img = cv2.imread('shapes.jpg', 0)
img = cv2.medianBlur(img, 5)
thresh =     cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY,11,2)

cv2.imshow('Thresh', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

image, contours, hierarchy =   cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(image, countours, -1, (0,255,0), 3)
cv2.imshow('Contours', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

出现第一个阈值图像,bu之后我收到一条错误消息

The first thresholded image is appearing, but after that I get an error message as

Traceback (most recent call last):
  File "contours.py", line 21, in <module>
    image, contours, hierarchy =     cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
ValueError: need more than 2 values to unpack

我们将不胜感激任何解决此问题的帮助。

Any help to resolve this issue will be appreciated.

推荐答案

请参阅示例。

cv2.findContours(...)

只返回两个对象,你试图将它解压缩为三个。

only returns two objects, you're trying to unpack it into three.

将该行更改为:

contours, hierarchy =   cv2.findContours(thresh.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

它应该有效。

这篇关于使用cv2.findContours()与python时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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