图像处理入门,在做二值化后轮廓识别时出现报错,使用代码见下。
image=cv2.imread(‘pic.png')
imgray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.blur(imgray, (9, 9))
(_, thresh0) = cv2.threshold(blurred, 130, 255, cv2.THRESH_BINARY)_,contours,hierarchy= cv2.findContours(thresh0.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
c = sorted(contours, key=cv2.contourArea, reverse=True)[0]
rect = cv2.minAreaRect(c)
box = np.int0(cv2.boxPoints(rect))
cv2.drawContours(image, [box], -1, (0, 255, 0), 3)Xs = [i[0] for i in box]
Ys = [i[1] for i in box]
x1 = min(Xs)
x2 = max(Xs)
y1 = min(Ys)
y2 = max(Ys)
hight = y2 - y1
width = x2 - x1
imagemidcut = imgray[y1:y1+hight, x1:x1+width]
论坛上的解释为文件路径有问题(使用路径不对或者文件路径中有中文名),检查发现并无类似问题,查看变量,发现y1为一负值(原因现在我也不明白),同一批次其它文件调用时均为出现类似问题。将y1强行修改为一正值后则无报错。
再补充一个帖子,包含error215的
【OpenCV报错 error: (-215) ssize.width > 0 && ssize.height > 0 in function】https://blog.csdn.net/xibeichengf/article/details/50810712
推荐阅读
- 实验环境搭建|OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow
- Opencv|Opencv报错Assertion failed (size.width>0 && size.height>0) in imshow【多个解决方案】
- opencv|OpenCV imread报错 error: (-215) size.width > 0 && size.height > 0 in function
- 程序员|作为一只Python爬虫(如何破解滑动验证码)
- docker|win11安装Docker玩转OpenCV梦的开始
- OpenCV从入门到项目实战|OpenCV-Python实战(番外篇)——利用 SVM 算法识别手写数字
- OpenCV从入门到项目实战|OpenCV-Python实战(番外篇)——基于 Haar 级联的猫脸检测器
- python|OpenCV-Python实战(番外篇)——想要识别猫咪的情绪(从猫脸检测开始)
- ubuntu|Opencv3中SURF算法学习