绘制图像轮廓时如何解决OpenCV(python)Valueerror

时间:2020-05-11 06:35:19

标签: python opencv

我正在尝试在图像(初学者项目)上的对象上绘制轮廓。我在cv2.findCountours()中遇到了valueerror。我检查了我的代码,这似乎是正确的。代码如下。

import numpy as np
import cv2

img = cv2.imread('detect_blob.png',1)
# First convert the image to a gray scale image 
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

# Create a threshold (Adaptive
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
    cv2.THRESH_BINARY, 115, 1)
# Display threshold
cv2.imshow('Binary', thresh)


#### Create Contours ####
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

### Formulate the draw command

# make a copy of the image 
img2 = img.copy()
# Index of the contours (-1 ---> all the contours)
index = -1
thinkness = 4
color = (255,0,255) #pink 

# Draw the contours 
cv2.drawContours(img2,contours, index,color,thinkness)
# Display the image 
cv2.imshow("Contours",img2)


cv2.waitKey(0)
cv2.destroyAllWindows()

错误消息是: 追溯(最近一次通话): 在第23行的文件“ 03_06.py” _,轮廓,层次结构= cv2.findContours(阈值,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) ValueError:没有足够的值可解包(预期3,得到2)

感谢您的帮助。

0 个答案:

没有答案
相关问题