python - 我想更改 python 中读取数组的顺序,是否必须使用 reshape?

标签 python arrays numpy reshape

我读取了一个 tif 图像(3D 堆栈),其原始尺寸为 100 X 120 X 150(示例)。

但是,当我导入这个时,这个尺寸是 150 X 120 X 100。 我尝试使用matrix.reshape来 reshape 它,但我发现如果我使用这个 reshape ,它的元素顺序将会改变。

import numpy as np
from skimage import io

# read the image stack
fname = '100x120x150.tif'
data = io.imread(fname)
data.shape

我想要做的是更改读取顺序,同时保持元素顺序。因此,导入后,我希望看到 data.shape 为 100X120X150 作为原始尺寸顺序。

最佳答案

尽管np.swapaxes可能是答案的解决方案,但我建议编写一个显示原始大小信息并保持数据不变的函数。

例如,来自SCIFIO我选择一个 tif 文件(EmbryoCE.zip 中的 focal1.tif,大小为 425 * 434 * 205):

import numpy as np
import matplotlib.pyplot as plt
from skimage import io

fname = 'focal1.tif'
data = io.imread(fname)

np.shape(data) #shows (205, 434, 425)
data[0] # The first layer in original Z direction

plt.imshow(data[0], cmap = "gray",  aspect = 'equal')
plt.colorbar()
plt.show()
print('Shape of matrix projection:',np.shape(data[0])) # shows (434, 425)

Image Link

您可以使用data[切片数量]轻松绘制 X-Y 平面横截面。

<小时/>

并添加一个显示原始尺寸信息的函数:

def pic_size(data_matrix):
    size = print('size of image: (',np.shape(data_matrix)[2],',',np.shape(data_matrix)[1],',',np.shape(data_matrix)[0],')')
    return size
pic_size(data) #shows size of image: ( 425 , 434 , 205 )
<小时/>

顺便说一句,reshape 不是一个好主意,它给出的图像带有点,看起来像是周期性排列的,但没有任何有用的信息。

Image Link

关于python - 我想更改 python 中读取数组的顺序,是否必须使用 reshape?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56266845/

相关文章:

python - 艰苦学习 Python 练习 5 EC4

java - 我如何改变这个贪心算法来预测最全面的分数?

python - 插入排序对于逆序数组来说快得离谱

python - Matlab,Python : Fixing colormap to specified values

python - python 中嵌套元组的并行迭代

php - 如何使用 PHP 和 MySQL 将固定位置的行值添加到数组中

Python - 使用 joblib 进行循环并行化

python - 洪水填充 NumPy 数组 `numpy.ndarray` ,i。 e.为元素分配新值并更改相同值的相邻元素

python - 如何在Python中将字典加载到ndarray中

python - 将大文件上传并运行一次到 file1.py 并在 file2.py 中运行多次