1 环境介绍
Windows7 x64,PyCharm 2017.3 Community,Python 3.6,OpenCV 3.4。
2 问题描述
在PyCharm环境,使用OpenCV的cv2.imread(filename)方法读取图像。当filename 中的路径或者图像文件名包含汉字的时候,cv2.imread(filename)读取不到图像数据,导致后续报错:NoneType。
3 示例
- 示例代码
以下示例代码运行会报错。
import cv2
# 读取车牌图像
test_sample_file="云A526EG.jpg"
image_origin = cv2.imread(test_sample_file)
print(image_origin.shape[0])
报错信息:
Traceback (most recent call last): File "E:/python/test/demo.py", line 25, in print(image.shape[0])
AttributeError: 'NoneType' object has no attribute 'shape'
2.原因分析
opencv的Python版本,cv2.imread(filename)方法不支持中文路径的文件读入。
3.解决方法
使用OpenCV的cv2.imdecode(buf, flags)方法。代码如下:
import cv2
import numpy as np
# 读取车牌图像
test_sample_file="云A526EG.jpg"
image_origin = cv2.imdecode(np.fromfile(test_sample_file, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
print(image_origin.shape[0])
参考列表:
1.opencv官方文档
网友评论