我正在运行此代码,但出现错误
cv2.drawContours(image,contours,-1,(0,255,0),3)
error: OpenCV(3.4.3) D:\Build\OpenCV\opencv-3.4.3\modules\imgproc\src\drawing.cpp:2511: error: (-215:Assertion failed) npoints > 0 in function 'cv::drawContours'
import cv2
import numpy as np
image=cv2.imread("C:/Users/AnushkaGupta/Desktop/image database/input1.jpg")
cv2.imshow("original image",image)
cv2.waitKey(0)
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
edged=cv2.Canny(gray,30,200)
cv2.imshow("Canny Edges",edged)
cv2.waitKey(0)
contours,hierarchy, _=cv2.findContours(edged,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
cv2.imshow("canny Edges After Contouring",edged)
cv2.waitKey(0)
print("Number of Contours found ="+str(len(contours)))
cv2.drawContours(image,contours,-1,(0,255,0),3)
cv2.imshow("Contours",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
答案 0 :(得分:0)
当您收集findContours()的输出时,顺序应如下:
image, contours, hierarchy = cv2.findContours(...)
在您的特定情况下,您应该写(我省略了imshow步骤):
import cv2
import numpy as np
image = cv2.imread("./input1.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(gray, 30, 200)
_, contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
print(f"Number of Contours found = {len(contours)}")
cv2.drawContours(image, contours, -1, (0,255,0), 3)