本文介绍了opencv 3 beta/python 中的 findContours 和 drawContours 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从 此处.

import numpy as np
import cv2
img = cv2.imread('final.jpg')
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0,255,0), 3)

错误是

 Traceback (most recent call last):
   File "E:\PC\opencv3Try\findCExample.py", line 7, in <module>
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
 ValueError: too many values to unpack (expected 2)

如果我删除层次结构",drawContours 中就会出现错误:

If I delete "hierarchy" the error arises in drawContours:

TypeError: contours is not a numpy array, neither a scalar

如果我在 drawContours 中使用轮廓 [0]

If I use contours[0] in drawContours

cv2.error: E:\opencv\opencv\sources\modules\imgproc\src\drawing.cpp:2171: error: (-215) npoints > 0 in function cv::drawContours

这里可能有什么问题?

推荐答案

opencv 3 有一个稍微修改了语法,返回值不同:

opencv 3 has a slightly changed syntax here, the return values differ:

cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) → image, contours, hierarchy

这篇关于opencv 3 beta/python 中的 findContours 和 drawContours 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 12:28