如何堆叠多个图像numpy ndarray
How to stack mutiple images numpy ndarray
我从opencv得到了图像numpy数组,然后我想把32张图像叠加在一起,最终我想要得到的形状是(3, 32, image_height, image_width), 下面是代码片段:
import cv2
import numpy as np
video_path = 'xxxx.mp4'
frame_buffer = np.array([])
frame_index = 0
frame_buffer_num = 0
cap = cv2.VideoCapture(video_path)
while True:
ret, image_np = cap.read()
print(image_np.shape)
if frame_index == 0:
frame_buffer = image_np # initialize empty frame_buffer
frame_index += 1
frame_buffer_num += 1
continue
frame_index += 1
frame_buffer_num += 1
frame_buffer = np.stack(frame_buffer, image_np)
if frame_buffer_num == 32:
print(frame_buffer.shape)
break
我运行它但出现以下错误:
Traceback (most recent call last):
File "/home/weidawang/Python/temp.py", line 19, in <module>
frame_buffer = np.stack(frame_buffer, image_np)
File "<__array_function__ internals>", line 6, in stack
File "/home/weidawang/miniconda3/lib/python3.7/site-packages/numpy/core/shape_base.py", line 430, in stack
axis = normalize_axis_index(axis, result_ndim)
TypeError: only size-1 arrays can be converted to Python scalars
------------更新------------
感谢@abe 的启发,以下代码有效:
import cv2
import numpy as np
video_path = 'xxxx.mp4'
frame_index = 0
frame_buffer_num = 0
cap = cv2.VideoCapture(video_path)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # float
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
frame_buffer = np.zeros((3, 32, int(height), int(width)))
while True:
ret, image_np = cap.read()
image_np = image_np.reshape(3, int(height), int(width))
print(image_np.shape)
frame_buffer[:, frame_buffer_num, :, :] = image_np
frame_index += 1
frame_buffer_num += 1
if frame_buffer_num == 32:
print(frame_buffer)
print(frame_buffer.shape)
break
您为每一帧获得的图像都有一个形状 (3, H, W)
对吗?你想堆叠其中的 32 个。然后,您可以先将 image_np
重塑为 image_np = image_np.reshape((3, 1, H, W))
,然后 np.append(frame_buffer, image_np, axis=1)
,其中 frame_buffer
是使用 image_np
的第一个重塑实例初始化的。这应该会产生一个形状为 (3, 32, H, W)
的张量
或者,您可以初始化 frame_buffer = np.zeros((3, 32, H, W))
并在每次迭代时 frame_buffer[:, i, :, :] = image_np
我从opencv得到了图像numpy数组,然后我想把32张图像叠加在一起,最终我想要得到的形状是(3, 32, image_height, image_width), 下面是代码片段:
import cv2
import numpy as np
video_path = 'xxxx.mp4'
frame_buffer = np.array([])
frame_index = 0
frame_buffer_num = 0
cap = cv2.VideoCapture(video_path)
while True:
ret, image_np = cap.read()
print(image_np.shape)
if frame_index == 0:
frame_buffer = image_np # initialize empty frame_buffer
frame_index += 1
frame_buffer_num += 1
continue
frame_index += 1
frame_buffer_num += 1
frame_buffer = np.stack(frame_buffer, image_np)
if frame_buffer_num == 32:
print(frame_buffer.shape)
break
我运行它但出现以下错误:
Traceback (most recent call last):
File "/home/weidawang/Python/temp.py", line 19, in <module>
frame_buffer = np.stack(frame_buffer, image_np)
File "<__array_function__ internals>", line 6, in stack
File "/home/weidawang/miniconda3/lib/python3.7/site-packages/numpy/core/shape_base.py", line 430, in stack
axis = normalize_axis_index(axis, result_ndim)
TypeError: only size-1 arrays can be converted to Python scalars
------------更新------------
感谢@abe 的启发,以下代码有效:
import cv2
import numpy as np
video_path = 'xxxx.mp4'
frame_index = 0
frame_buffer_num = 0
cap = cv2.VideoCapture(video_path)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # float
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
frame_buffer = np.zeros((3, 32, int(height), int(width)))
while True:
ret, image_np = cap.read()
image_np = image_np.reshape(3, int(height), int(width))
print(image_np.shape)
frame_buffer[:, frame_buffer_num, :, :] = image_np
frame_index += 1
frame_buffer_num += 1
if frame_buffer_num == 32:
print(frame_buffer)
print(frame_buffer.shape)
break
您为每一帧获得的图像都有一个形状 (3, H, W)
对吗?你想堆叠其中的 32 个。然后,您可以先将 image_np
重塑为 image_np = image_np.reshape((3, 1, H, W))
,然后 np.append(frame_buffer, image_np, axis=1)
,其中 frame_buffer
是使用 image_np
的第一个重塑实例初始化的。这应该会产生一个形状为 (3, 32, H, W)
或者,您可以初始化 frame_buffer = np.zeros((3, 32, H, W))
并在每次迭代时 frame_buffer[:, i, :, :] = image_np