python - 如何 cv2.imshow 存储在 Numpy 数组中的图像?

标签 python arrays image numpy opencv

我有 2 个文件夹,每个文件夹有 10 张图片,我已将它们加载到 Numpy 数组中并将它们连接起来(原始单个 np 数组形状:10 128 128 3,连接 np 数组形状:20 128 128 3)。现在我想循环 20 次并 cv2.imshow 每个图像,例如,第一个 '1 128 128 3',然后是 '2 128 128 3' 等等。但是我如何将这样的形状提供给 cv2.imshow?

我的代码:

import numpy as np
import cv2
import glob

first_images = np.array([cv2.imread(file) for file in glob.glob("/filepath/*.png")])
second_images = np.array([cv2.imread(file) for file in glob.glob("/filepath/*.png")])


num_of_first =first_images.shape[0]
num_of_second = second_images.shape[0]

image_array = np.concatenate((first_images,second_images),axis=0)

for image in range(image_array.shape[0]):

也许我可以做类似的事情 single_image = image_array(图像,,,) 然后将其 reshape 为 128x128x3?但我不确定该怎么做。

编辑:

已尝试 cv2.imshow(image_array[image, :,:,:])得到

TypeError: Required argument 'mat' (pos 2) not found

我改用 cv2.imwrite 并且能够得到我想要的。问题仍然是如何让 cv2.imshow 工作。

最佳答案

我认为您需要将“shape”与 numpy 结合使用来获取数组大小而不是“len”。

关于python - 如何 cv2.imshow 存储在 Numpy 数组中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54844148/

相关文章:

python - numba 似乎不起作用

python - 与倒数第二个相比,最后一个索引对 numpy 数组的访问时间的影响更大

java - ArrayIndexOutOfBounds 异常,但没有越界?

c# - 字节数组到 int C#

css - 通过创建一个类使我的图像跨越整个网站

Python 错误 : "socket.error: [Errno 11] Resource temporarily unavailable" when sending image

java - 比较两个屏幕截图无法正常工作-java

python - 如何计算满足给定条件的向量的所有排列

python - 如何安装旧版本的 django 软件包?

python - 如何在 tkinter python gui 中找到鼠标点击附近的标签