尝试使用 cv2.resize

问题描述

我正在尝试加载 .npy 文件并使用 cv2.resize 调整其大小,但收到以下错误消息:

cv2.error: OpenCV(4.5.1-dev) /home/name/opencv_build/opencv/modules/imgproc/src/resize.cpp:3688: error: (-215:Assertion Failed) !dsize.empty( ) 在函数“调整大小”中

这是我的代码

filepath = 'data.npy'
img = np.load(filepath)
print(img.shape)
res = cv2.resize(img,(352,1216))

print(img.shape)输出(1,1,192,640)

解决方法

如果是灰度/单通道,opencv 中的图像应该只有 2 暗,如果是彩色,则应该只有 3 暗。您似乎有一个灰色/单通道图像 [192,640] 包含在 2 个列表中 [1,1,---]

所以要获取图像,您需要从这两个列表中获取它。

img = np.load(filepath)[0][0]

或者如果您不确定它包含多少个列表,您可以这样做

img = np.squeeze(np.load(filepath)

但是只要这些列表中只有 1 张图像,它就会起作用