OpenCV uses imread to read the image failure solution

Use the following code to read the image, [WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_(' D:.jpg'): can't open/read file error message

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 读取图像
img = cv.imread("D:\2.jpg")
# 显示图像
# 利用opencv展示图像
# cv.imshow('image', img)
# cv.waitKey(0)
# cv.destroyAllWindows()
# matplotlib
# plt.imshow(img[:, :, ::-1])
plt.imshow(img, cmap="gray")
plt.show()

# 图像保存
cv.imwrite("D:\2.png", img)

insert image description here

The reason is that "\" should be replaced with "\\" when using an absolute path

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 读取图像
img = cv.imread("D:\\2.jpg")
# 显示图像
# 利用opencv展示图像
# cv.imshow('image', img)
# cv.waitKey(0)
# cv.destroyAllWindows()
# matplotlib
# plt.imshow(img[:, :, ::-1])
plt.imshow(img, cmap="gray")
plt.show()

# 图像保存
cv.imwrite("D:\\2.png", img)

Imported successfully
insert image description here

Guess you like

Origin blog.csdn.net/m0_60266328/article/details/127753455