Numpy change shape from (3, 512, 660, 4) to (3,2048,660,1)

让人想犯罪 __ 提交于 2020-01-06 04:42:08

问题


I am working on tensorflow and I am having some images of cars in an numpy array with shape (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.

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.

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).

Can someone help me?

I tried reshape function but it actually overlaps images rather than concatenating it.


回答1:


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)


来源:https://stackoverflow.com/questions/46388237/numpy-change-shape-from-3-512-660-4-to-3-2048-660-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!