错误:(-215:断言失败)在使用OpenCV处理轮廓时,npoints> 0。

问题描述 投票:1回答:2
当我运行此代码时:

import cv2 image=cv2.imread('screenshoot10.jpg') cv2.imshow('input image', image) gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) edged=cv2.Canny(gray,30,200) cv2.imshow('canny edges',edged) _, contours = cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) cv2.imshow('canny edges after contouring', edged) print(contours) print('Numbers of contours found=' + str(len(contours))) cv2.drawContours(image,contours,-1,(0,255,0),3) cv2.imshow('contours',image) cv2.waitKey(0) cv2.destroyAllWindows()

我收到此错误:

OpenCV(4.1.1)C:\ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ drawing.cpp:2509:错误:(-215:声明失败)函数中的npoints> 0'cv :: drawContours'

我在做什么错?

当我运行此代码时:导入cv2 image = cv2.imread('screenshoot10.jpg')cv2.imshow('input image',image)gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)edged = cv2.Canny (灰色,30,200)cv2.imshow('canny ...

python opencv contour
2个回答
0
投票
根据findContours的documentation,该方法返回(轮廓,层次结构,所以我认为代码应该是:

0
投票
取决于OpenCV版本,cv2.findContours()具有不同的返回签名。在v3.4.X中,返回三个项目。在v2.Xv4.1.X中,返回两个项目。无论使用哪种版本,都可以轻松获取轮廓。

cnts = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if len(cnts) == 2 else cnts[1] for c in cnts: ...

© www.soinside.com 2019 - 2024. All rights reserved.