python - 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)
.
解决方案
如果灰度/单通道,opencv 中的图像应该只有 2 个暗淡,如果是彩色,则应该只有 3 个暗淡。您似乎有一个灰色/单通道图像 [192,640] 包含在 2 个列表中 [1,1,---]
所以要获取图像,您需要从这两个列表中获取它。
img = np.load(filepath)[0][0]
或者如果您不确定它包含多少个列表,您可以这样做
img = np.squeeze(np.load(filepath)
但只要这些列表中只有 1 个图像,这将起作用
推荐阅读
- angular - ReferenceError:未定义 IDBIndex Angular SSR
- javascript - 我们可以在两个不同的 div 中渲染两个不同的内容吗?
- vscode-remote - vscode-remote 是否自动提交我对文件的修改仅存在于图像中
- sqlite - 找不到包'sqlite3' - sqlite3 已经安装
- javascript - 如何使用 node.js 和 socket.io 连接到已建立的服务器(不是本地主机)?
- python - 如何使用 AWS 部署多个 TensorFlow 模型?
- java-8 - Java 8 加密问题
- python - 如何找到绘图和直线之间的交点
- php - 从 iphone 设备录制和上传的视频不会在 iphone 中播放
- asp.net - 未处理的拒绝(SyntaxError):JSON 中位置 0 的意外标记 <?