问题
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