【Python】cv2.error: OpenCV(4.8.0) :-1: error: (-5:Bad argument) in function ‘cvtColor‘ > Overload res

The code snippet is:

# 将图片转换为灰度图
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)

My code has an error:

(PyTorch) D:\CodeProject>D:/Anaconda/envs/PyTorch/python.exe d:/CodeProject/2023_SZ_Cup/Problem_1/evaluate.py
Traceback (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'

insert image description here

The error message prompts cv2.error: (-5:Bad argument) in function 'cvtColor', because the parameters passed to cv2.cvtColor are incorrect when the image is loaded from the original path and converted to grayscale.

Modify the code:

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)

Major changes include:

  1. Pass the image path as a parameter to the cv2.imread function to load the image correctly.
  2. Use the cv2.IMREAD_GRAYSCALE flag to ensure that images are read as grayscale images.

But the code still reports an error:

(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'

insert image description here

This error message indicates that there is a problem when we read the image, and the image may not be loaded correctly, resulting in an error in subsequent image processing.

You can add some error checking after reading the image to make sure the image loaded successfully. Here is the modified code, with error checking and printing added for better debugging:

    # 读取图片并转换为灰度图
    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)

The code ends up pointing out the problem:

[ WARN:0@0.312] global loadsave.cpp:248 cv::findDecoder imread_('./2023_SZ_Cup/datasets/B棰?无法加载图像

Just change the name! No more errors.

Guess you like

Origin blog.csdn.net/wzk4869/article/details/132584614