cv2.imread()不支持中文路径解决方法
opencv库中imread用于读取图片文件。但这个是不支持中文路径的,为此,需要imdecode方法来读取。
img= cv2.imread(r"C:\Users\Administrator\Desktop\图片1.jpg") #会返回None
#然后将img传给cv2.cvtColor()
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
会报以下错误
cv2.error: OpenCV(4.10.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:196: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
imdecode方法来读取
import cv2
import matplotlib.pyplot as plt
import numpy as npimgPath = r"C:\Users\Administrator\Desktop\图片1.jpg"
img = cv2.imdecode(np.fromfile(imgPath,dtype=np.uint8),cv2.IMREAD_COLOR)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.imshow(img_gray)
plt.show()