cv2.带重音的Imread文件(unicode)



我正在尝试加载以下文件:'data/chapter_1/capd_yard_signs\Dueñas_2020.png'

但是当我这样做时,cv2。imread返回错误:imread_('data/chapter_1/capd_yard_signsDue├▒as_2020.png'): can't open/read file: check file path/integrity load file

当我用os.path指定文件名时。加入,我尝试编码和解码文件

f = os.path.join("data/chapter_1/capd_yard_signs", filename.encode().decode())

但这并没有解决问题。

我错过了什么?

我是这样让它工作的:

from PIL import Image
pil = Image.open(f).convert('RGB') # load the image with pillow and make sure it is in RGB
pilCv = np.array(pil) # convert  the image to an array
img = pilCv[:,:,::-1].copy() # convert the array to be in BGR

这将工作:

import cv2, numpy as np
pic = cv2.imdecode(np.fromfile(os.path.join("data/chapter_1/capd_yard_signs", filename).replace('\','/'), dtype=np.uint8), 0)

np.fromfile首先读取非ascii字符,.replace('\','/')也应该解决你的不均匀/和问题。然后imdecode开始工作。

这是灰度。对于颜色,将0替换为cv2.IMREAD_COLOR

最新更新