Python 官方文档:入门教程 => 点击学习
代码片段为: # 将图片转换为灰度图image1 = cv2.cvtColor(origin_iamge, cv2.COLOR_BGR2GRAY)image2 = cv2.cvtColor(sp_im
代码片段为:
# 将图片转换为灰度图image1 = cv2.cvtColor(origin_iamge, cv2.COLOR_BGR2GRAY)image2 = cv2.cvtColor(sp_image, cv2.COLOR_BGR2GRAY)image3 = cv2.cvtColor(sp1_image, cv2.COLOR_BGR2GRAY)mse_1 = mse(image1, image2)mse_2 = mse(image1, image3)ssim_1 = ssim(image1, image2)ssim_2 = ssim(image1, image3)
我的代码出现了报错:
(PyTorch) D:\CodeProject>D:/Anaconda/envs/PyTorch/python.exe d:/CodeProject/2023_SZ_Cup/Problem_1/evaluate.pyTraceback (most recent call last): File "d:\CodeProject\2023_SZ_Cup\Problem_1\evaluate.py", line 20, in <module> image1 = cv2.cvtColor(origin_iamge, cv2.COLOR_BGR2GRAY)cv2.error: OpenCV(4.8.0) :-1: error: (-5:Bad argument) in function 'cvtColor'> Overload resolution failed:> - src is not a numpy array, neither a Scalar> - Expected Ptr<cv::UMat> for argument 'src'
错误信息提示 cv2.error: (-5:Bad argument) in function ‘cvtColor’,这是因为在将图像从原始路径加载并转换为灰度图时,传递给 cv2.cvtColor 的参数不正确。
修改代码:
image1 = cv2.imread(origin_image_path, cv2.IMREAD_GRAYSCALE)image2 = cv2.imread(sp_image_path, cv2.IMREAD_GRAYSCALE)image3 = cv2.imread(sp1_image_path, cv2.IMREAD_GRAYSCALE)
主要更改包括:
但是代码还是报错:
(PyTorch) D:\CodeProject>D:/Anaconda/envs/PyTorch/Python.exe d:/CodeProject/2023_SZ_Cup/Problem_1/evaluate.py[ WARN:0@0.328] global loadsave.cpp:248 cv::findDecoder imread_('./2023_SZ_Cup/datasets/B棰?Traceback (most recent call last): File "d:\CodeProject\2023_SZ_Cup\Problem_1\evaluate.py", line 21, in <module> mse_1 = mse(image1, image2) File "d:\CodeProject\2023_SZ_Cup\Problem_1\evaluate.py", line 7, in mse err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)AttributeError: 'NoneType' object has no attribute 'astype'
这个错误信息表明我们在读取图像时出现了问题,图片可能没有正确加载,导致后续的图像处理出错。
可以在读取图像后添加一些错误检查来确保图像是否成功加载。以下是修改后的代码,添加了错误检查和打印信息,以便更好地调试:
# 读取图片并转换为灰度图 image1 = cv2.imread(origin_image_path, cv2.IMREAD_GRAYSCALE) image2 = cv2.imread(sp_image_path, cv2.IMREAD_GRAYSCALE) image3 = cv2.imread(sp1_image_path, cv2.IMREAD_GRAYSCALE) if image1 is None or image2 is None or image3 is None: print("无法加载图像") else: mse_1 = mse(image1, image2) mse_2 = mse(image1, image3) ssim_1 = ssim(image1, image2) ssim_2 = ssim(image1, image3) print("-----调包-----") print('MSE:', mse_1) print('ssim:', ssim_1) print("-----算法-----") print('MSE:', mse_2) print('ssim:', ssim_2)
代码最后指出问题所在:
[ WARN:0@0.312] global loadsave.cpp:248 cv::findDecoder imread_('./2023_SZ_Cup/datasets/B棰?无法加载图像
修改名字即可!不再报错。
来源地址:https://blog.csdn.net/wzk4869/article/details/132584614
--结束END--
本文标题: 【Python】cv2.error: OpenCV(4.8.0) :-1: error: (-5:Bad argument) in function ‘cvtColor‘ > Overload res
本文链接: https://lsjlt.com/news/399111.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0