numpy数组特定操作

时间:2019-03-28 06:53:29

标签: python

我有一个512个tiff图片的numpy数组。而且我每次需要选择4张图像以对其进行一些计算。然后选择下4张图像并进行相同的操作,依此类推。...

2 个答案:

答案 0 :(得分:1)

只需重塑数组,然后使用切片一次获取4张图像:

new_array = array.reshape(4, 128)
for i in range(128):
    batch_of_pictures = new_array[:, i] # or maybe the reverse

注意,这未经测试,可能会导致错误。基本想法是合理的,如果遇到困难,您可以参考https://docs.scipy.org/doc/numpy/user/quickstart.html#indexing-slicing-and-iterating或发表评论!

由于您实际上拥有的阵列大小与我想象的不同,请尝试:

for i in range(0, 128, 4):
    batch_of_pictures = your_array[:,:,i:i+4]

同样,我还没有测试过,但应该可以!您的问题只是关于索引编制,因此将i:i + 3移到任何位置都可以产生正确的图片阵列

答案 1 :(得分:0)

假设图像沿数组img的轴0枚举:

N = 512 # the total number of images in img
for i in range(0, N, 4):
  img4 = img[i:i+4, :, :]       # img4 is now an array[4, 512, 512] of 4 images
  varImg = np.var(img4, axis=0) # computes the variance in img4, pixel-wise

另外,请参见How to create a Minimal, Complete, and Verifiable example