如何使用numpy拼接数组?

时间:2021-09-29 21:23:43

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.

我有一个numpy形状的阵列(780,256,256)代表一个图像的780个瓦片,我需要重新组装成原始图像,但无法弄清楚如何正确地重塑这个。

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.

780个瓷砖应排列成网格26x30网格,因此最终结果具有形状(6656,7680)。当图像从左到右,从上到下时,图块按顺序排列。

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.

我可以通过在数组上使用np.hstack来获取一行中的tile,并使用row1 = np.hstack(array_of_tiles [0:30,:,]]正确地获取第一行,但是我做的任何重新整理都没有保持瓷砖结构。

I can probably write out the tiles to tif and mosaic using QGIS but what is the correct way using numpy directly?

我可以用QGIS把瓷砖写成tif和马赛克,但是直接使用numpy的正确方法是什么?

1 个解决方案

#1


3  

Step-by-step:

1) Arrange tiles correctly:

1)正确排列瓷砖:

 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:

2)将它们拼凑在一起:为了制作一个连贯的图像,第二个图块的第一行像素(tile [0,1,0,:])将连接到第一个图块的第一行像素的末尾(图块[ 0,0,0,:])等。从中我们可以看到必须交换两个中轴:

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:

3)去除多余的尺寸。像素的顺序现在是正确的,但它们以4D结构布局。我们需要将其减少到2D:

img = tiles.reshape(6656, 7680)

#1


3  

Step-by-step:

1) Arrange tiles correctly:

1)正确排列瓷砖:

 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:

2)将它们拼凑在一起:为了制作一个连贯的图像,第二个图块的第一行像素(tile [0,1,0,:])将连接到第一个图块的第一行像素的末尾(图块[ 0,0,0,:])等。从中我们可以看到必须交换两个中轴:

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:

3)去除多余的尺寸。像素的顺序现在是正确的,但它们以4D结构布局。我们需要将其减少到2D:

img = tiles.reshape(6656, 7680)