报错信息:cv2.drawContours(thresh_Contours, cnts, -1, (0, 0, 255), 3)
cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\drawing.cpp:2502: error: (-215:Assertion failed) npoints > 0 in function ‘cv::drawContours’
原因:Opencv版本不同导致报错
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[1]
cv2.drawContours(thresh_Contours, cnts, -1, (0, 0, 255), 3)
改正方法:将“1”改成“0”
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)[0]
cv2.drawContours(thresh_Contours, cnts, -1, (0, 0, 255), 3)
在使用OpenCV进行图像轮廓处理时遇到了一个错误:cv2.error:OpenCV(4.5.5)…错误源于OpenCV版本差异。原来代码中findContours的返回值索引错误,由`[1]`更改为`[0]`后问题得到解决。修复后的代码是`cnts=cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]`,确保了轮廓绘制的正常进行。
2929

被折叠的 条评论
为什么被折叠?



