opencv库中imread用于读取图片文件。但这个是不支持中文路径的,为此,需要imdecode方法来读取。
import numpy as np
import cv2
image = cv2.imdecode(np.fromfile(imgPath,dtype=np.uint8),CV_LOAD_IMAGE_COLOR)
关于imread和imdecode的帮助见:
https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#cv2.imread
注意,这两个函数中的flages参数。决定了读进来的图片的格式,灰度图(1通道)、颜色图()、alpha图。
Flags specifying the color type of a loaded image:
CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
0 Return a 3-channel color image.
Note In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.
=0 Return a grayscale image.
<0 Return the loaded image as is (with alpha channel).
关于支持中文路径的其他方法,可以参见:
【1】https://blog.csdn.net/liuqinshouss/article/details/78696032
【2】https://www.zhihu.com/question/67157462/answer/251754530
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)