python 错误代码:OpenCV(4.7.0):-1:错误:(-5:参数错误)在函数“imwrite”中不支持img数据类型= 17

1l5u6lss  于 2023-01-08  发布在  Python
关注(0)|答案(1)|浏览(2317)

我试图使用一张图片来创建一个直方图均衡。我已经导入图像和CV2,但仍然有一个问题。

import numpy as np
import cv2 as cv


path = r'C:\Users\Lilly\Downloads\lena_color.jpg'
img = cv.imread(path,0)
equ = cv.equalizeHist(img)
res = np.hstack((img,equ)) 
cv.imwrite('res.png',res)

这是一个错误。

PS C:\Users\Lilly> & C:/Users/Lilly/AppData/Local/Programs/Python/Python39/python.exe c:/Users/Lilly/Downloads/Equalization.py
Traceback (most recent call last):
  File "c:\Users\Lilly\Downloads\Equalization.py", line 17, in <module>
    cv.imwrite('res.png',res)
cv2.error: OpenCV(4.7.0) :-1: error: (-5:Bad argument) in function 'imwrite'
> Overload resolution failed:
>  - img data type = 17 is not supported
>  - Expected Ptr<cv::UMat> for argument 'img'
oxiaedzo

oxiaedzo1#

imread()失败。它返回None。找不到文件或文件已损坏。
equalizeHist()并不抱怨,它只是返回None
res现在变为array([None, None], dtype=object)
imwrite()无法处理此问题,并报告此错误消息。

修复是确保imread()成功。确定文件是否实际存在。

assert os.path.exists(path)
img = cv.imread(path,0)
assert img is not None

相关问题