error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32F || depth ==CV_32S

OpenCV版本升级引起的函数调用报错

  • 问题描述
  • 解决方法

问题描述

最近学习OCR识别项目,运行后出现题目所述错误,定位到错误行发现是因为OpenCV的版本更新导致轮廓检测所用findContours函数的第一个输出参数被新版本删除了:
In OpenCV 3.4:

image, contours, hierarchy=  cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]]) 

In OpenCV 4.0:

contours, hierarchy= findContours(image, mode, method[, contours[, hierarchy[, offset]]]) 

解决方法

所以要解决以上错误,只需要将原版本代码略作修改即可,删除image参数。在唐宇迪的项目代码中则使输出值[1]变为[0],即输出contours。
以下是OCR识别项目中的源代码修改方法:
In OpenCV 3.4:

cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[1]
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]

In OpenCV 4.0:

cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]

问题已解决。虽然很简单,但是作为菜鸟的一点学习记录吧,欢迎大佬进行批评指正。

你可能感兴趣的:(OpenCV,&,TensorFlow,常见错误解决,opencv,计算机视觉,人工智能,python)