如何使用numpy拼接数组? [英] How to mosaic arrays using numpy?

查看:161
本文介绍了如何使用numpy拼接数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形状为(780,256,256)的numpy数组,代表图像的780个图块,我需要将其重新组装成原始图像,但无法弄清楚如何正确地对其重塑.

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.

应将780个图块排列在26x30的网格中,因此最终结果的形状为(6656, 7680).图像按从左到右,从上到下的顺序排列.

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.

我可以通过在数组上使用np.hstack在一行中获得切片,第一行可以使用row1 = np.hstack(array_of_tiles[0:30,:,:])正确地获得,但是我进行的任何重塑都不会保持切片结构.

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.

我可能可以使用QGIS将图块写到tif和马赛克上,但是直接使用numpy的正确方法是什么?

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

推荐答案

分步操作:

1)正确排列图块:

 tiles = array_of_tiles.reshape(26, 30, 256, 256)

2)将它们拼凑在一起:要制作一个相干图像,请将第二个图块的第一行像素(tiles [0、1、0 、:])连接到第一个图块的第一行像素的末尾(tiles [0,0,0,:])等.由此可见,必须交换两个中轴:

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)移除多余的尺寸.像素的顺序现在是正确的,但是它们以4D结构进行布局.我们需要将其减少到2D:

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)

这篇关于如何使用numpy拼接数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆