如何将多维数组重塑为2D图像? [英] How to reshape a multidimensional array to a 2D image?

查看:72
本文介绍了如何将多维数组重塑为2D图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理如下形状的数组

I'm working on an array shaped as follows

(64, 1, 64, 64)

实际上,这是一张灰度图像,分为64个色块,每个色块具有64 * 64px.

This is in fact one grayscale image that was split into 64 patches, each patch with 64*64px.

现在,我需要将其重建为512 * 512px的图像.

Now I need to rebuild it into a 512*512px image.

我尝试使用

np.reshape(arr, (512, 512))

但是,当然生成的图像不是预期的.

but of course the resulting image is not as expected.

我该如何解决?

推荐答案

这取决于补丁的排列方式.但是您可以尝试的第一件事是

It depends on how your patches are arranged. But the first thing you could try is

image.reshape(8, 8, 64, 64).swapaxes(1, 2).reshape(512, 512)

这是假设原始的第零维逐行列出了补丁,即0-7是补丁从左到右的第一行,8-15是第二行,依此类推.

This is assuming that the original zeroth dimension lists the patches row by row, i.e. 0-7 are the first row of patches from left to right, 8-15 the second row and so on.

第一次重塑会重新建立该排列,在为轴0和1选择索引i,j之后,将寻址第i + 1行中的第j + 1个面片.

The first reshape reestablishes that arrangement, after it choosing index i, j for axes 0 and 1 addresses the j+1st patch in the i+1st row.

现在有趣的是:通过整形合并轴时:

Now comes the interesting bit: When merging axes by reshape:

  • 仅可以合并相邻的尺寸
  • 每个块中除最右边的轴以外的所有轴都将分散

由于我们要将每个面片保持在一起,因此必须重新排列,以使当前轴2和3成为块的最右侧成员.这就是swapaxes的作用.

Since we want to keep each patch together we have to rearrange in such a way that the current axes 2 and 3 become the rightmost members of blocks. That is what the swapaxes does.

现在,形状为(8、64、8、64),轴1和3为面内坐标.将两对( 8, 64 -> 512 8, 64 -> 512 )组合在一起就可以了.

By now the shape is (8, 64, 8, 64) and axes 1 and 3 are the within-patch coordinates. Combining two pairs ( 8, 64 -> 512 8, 64 -> 512 ) is all that's left to do.

这篇关于如何将多维数组重塑为2D图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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