问题
I have an numpy array of shape (780,256,256)
representing 780 tiles of an image, which I need to reassemble into the original image, but can't figure out how to reshape this properly.
The 780 tiles should be arranged in a grid 26x30 grid, so the end result has shape (6656, 7680)
. The tiles are in order as the image goes left to right, top to bottom.
I can get the tiles in a line by using np.hstack
on the array, and the first row correctly using row1 = np.hstack(array_of_tiles[0:30,:,:])
, but any reshaping I then do doesn't maintain the tile structure.
I can probably write out the tiles to tif and mosaic using QGIS but what is the correct way using numpy directly?
回答1:
Step-by-step:
1) Arrange tiles correctly:
tiles = array_of_tiles.reshape(26, 30, 256, 256)
2) Piece them together: To make one coherent image the first row of pixels of the second tile (tiles[0, 1, 0, :]) be joined to the end of the first row of pixels of the first tile (tiles[0, 0, 0, :]) etc. From that we can see that the two middle axes must be swapped:
tiles = tiles.swapaxes(1, 2)
3) Remove excess dimensions. The order of pixels is now correct but they are layed out in a 4D structure. We need reduce that to 2D:
img = tiles.reshape(6656, 7680)
来源:https://stackoverflow.com/questions/48846876/how-to-mosaic-arrays-using-numpy