问题

python-OpenCV读取图像时,如果图像路径包含中文,会读取失败:

import cv2

img = cv2.imread("./中文路径/test.jpg", cv2.IMREAD_UNCHANGED)
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow("img", img)
cv2.waitKey(0)
print("read success")

cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-9d_dfo3_\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘cv::imshow’

解决方法

import cv2
import numpy as np

img = cv2.imdecode(np.fromfile("./中文路径/test.jpg", dtype=np.uint8), cv2.IMREAD_UNCHANGED)
cv2.namedWindow("img", cv2.WINDOW_NORMAL)
cv2.imshow("img", img)
cv2.waitKey(0)
print("read success")