如何为Numpy数组添加新尺寸? [英] How can I add new dimensions to a Numpy array?
问题描述
我从一个图像的numpy数组开始.
I'm starting off with a numpy array of an image.
In[1]:img = cv2.imread('test.jpg')
形状是640x480 RGB图像的预期形状.
The shape is what you might expect for a 640x480 RGB image.
In[2]:img.shape
Out[2]: (480, 640, 3)
但是,我拥有的这张图像是一帧视频,长度为100帧.理想情况下,我希望有一个包含该视频中所有数据的数组,以使img.shape
返回(480, 640, 3, 100)
.
However, this image that I have is a frame of a video, which is 100 frames long. Ideally, I would like to have a single array that contains all the data from this video such that img.shape
returns (480, 640, 3, 100)
.
将下一帧(即另一组图像数据,另一个480 x 640 x 3数组)添加到我的初始数组中的最佳方法是什么?
What is the best way to add the next frame -- that is, the next set of image data, another 480 x 640 x 3 array -- to my initial array?
推荐答案
您在问如何向NumPy数组添加维度,以便可以扩展该维度以容纳新数据.可以添加维,如下所示:
You're asking how to add a dimension to a NumPy array, so that that dimension can then be grown to accommodate new data. A dimension can be added as follows:
image = image[..., np.newaxis]
这篇关于如何为Numpy数组添加新尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!