首页 > 解决方案 > Error message when trying to resize with cv2.resize

问题描述

I'm trying to load a .npy file and resize it with cv2.resize but I get the following error message:

cv2.error: OpenCV(4.5.1-dev) /home/name/opencv_build/opencv/modules/imgproc/src/resize.cpp:3688: error: (-215:Assertion failed) !dsize.empty() in function 'resize'

This is my code:

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

Output of print(img.shape) is (1, 1, 192, 640).

标签: pythonopencvimage-resizing

解决方案


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

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

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

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

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

但只要这些列表中只有 1 个图像,这将起作用


推荐阅读