我的简单python代码是这样的
import cv2
img=cv2.imread('Materials/shapes.png')
blur=cv2.GaussianBlur(img,(3,3),0)
gray=cv2.cvtColor(blur,cv2.COLOR_BGR2GRAY)
returns,thresh=cv2.threshold(gray,80,255,cv2.THRESH_BINARY)
ret,contours,hierachy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
area=cv2.contourArea(cnt) #contour area
if (area>1220):
cv2.drawContours(img,[cnt],-1,(0,255,0),2)
cv2.imshow('RGB',img)
cv2.waitKey(1000)
print(len(cnt))
import numpy as np
contours=np.array(contours)
print(contours)
Run Code Online (Sandbox Code Playgroud)
这很好.但最近没有我做任何改变.这是扔给我的
RET,轮廓,层次结构= cv2.findContours(THRESH,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
ValueError:没有足够的值来解压缩(预期3,得到2)
帮帮我们
谢谢.
我正在尝试对findContours得到的轮廓进行简单的面积计算.我的openCv版本是3.1.0
我的代码是:
cc = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.contourArea(cc[0])
error: 'C:\\builds\\master_PackSlaveAddon-win32-vc12-static\\opencv\\modules\\imgproc\\src\\shapedescr.cp...: error: (-215) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function cv::contourArea\n'
Run Code Online (Sandbox Code Playgroud)
似乎无法解决它,我感觉它只是类型转换,我希望findContours结果匹配contourArea的类型
谢谢 :)
编辑:结果我需要采取findContours的第二个参数
im2, cc, hierarchy = cv2.findContours(im_bw.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Run Code Online (Sandbox Code Playgroud)