How to mosaic arrays using numpy?

走远了吗. 提交于 2019-12-24 13:33:39

问题


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

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