Numpy 将形状从 (3, 512, 660, 4) 更改为 (3,2048,660,1) [英] Numpy change shape from (3, 512, 660, 4) to (3,2048,660,1)
问题描述
我正在研究 tensorflow,我在一个形状为 (3, 512, 660, 4)
的 numpy 数组中有一些汽车图像.
I am working on tensorflow and I am having some images of cars in an numpy array with shape (3, 512, 660, 4)
.
这里,3
对应一个汽车索引,512*660
是一个图片尺寸,4
对应一个车标的不同边车.
In this, 3
corresponds to a car index, 512*660
is an image size and 4
corresponds to the different sides of a car.
即(1, 512, 660, 1)
对应Car1 - 正面图,(1, 512, 660, 2)
对应Car1 -左侧图像等.
That is, (1, 512, 660, 1)
corresponds to Car1 - front side image, (1, 512, 660, 2)
corresponds to Car1 - Left side image and so on.
现在,我想将汽车的所有图像合并为一个图像(2048*660
).也就是说,我想将 (3, 512, 660, 4)
重塑为 (3, 2048, 660, 1)
.
Now, I want to concat all the images of a car into one image (2048*660
). That is, I want to reshape (3, 512, 660, 4)
to (3, 2048, 660, 1)
.
有人可以帮我吗?
我尝试了 reshape 函数,但它实际上是重叠图像而不是连接它.
I tried reshape function but it actually overlaps images rather than concatenating it.
推荐答案
我们可以置换坐标轴,将最后一个坐标轴作为新的第三个坐标轴向前推进并重塑.置换轴可以用 np.swapaxes
或 np.transpose
或 np.rollaxis
处理,给我们三个解决方案,就像这样 -
We could permute axes to push the last axis up front as the new third axis and reshape. Permuting axes could be handled with np.swapaxes
or np.transpose
or np.rollaxis
, giving us three solutions, like so -
a.swapaxes(2,3).reshape(3,2048,660,1)
a.transpose(0,1,3,2).reshape(3,2048,660,1)
np.rollaxis(a,3,2).reshape(3,2048,660,1)
如果你想在前面有边索引,相应地转置它 -
If you wanted to have sides-index at the front, transpose it accordingly -
a.transpose(0,3,1,2).reshape(3,2048,660,1)
这篇关于Numpy 将形状从 (3, 512, 660, 4) 更改为 (3,2048,660,1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!