将图像堆叠为numpy数组

时间:2014-10-05 10:25:08

标签: python arrays image numpy

我试图使用for循环将6个不同的图像叠加在另一个上面以创建3D堆栈。我是Python的新手......我无法理解这一点出。如何创建堆栈以及如何在以后访问堆栈中的每个映像?我的代码有点像这样......

image = data.camera()

noisyImage = np.zeros(image.shape(0),image.shape(1))

fig = plt.figure(figsize=(12,4))  

for i in range(6):
    noisyImage = util.random_noise(image,mode='gaussian',seed=i)
    result = np.dstack(noisyImage,noisyImage)
    ax = plt.subplot(2,3,i)

1 个答案:

答案 0 :(得分:0)

试试这个:

# reshape array that is (N,M) to one that is (N,M,1)   no increase in size happens.
n1=np.reshape(noisyImage,noisyImage.shape+(1,))
if(i==1):
    result=n1
else:
#   concatenate the N,M,1 version of the array to the stack using the third index (last index) as the axis.
    result=np.concatenate(result,n1,axis=n1.ndim-1)

下面的代码是一个更通用的实现(从上面我的答案),应用一个功能,设计应用于单个通道到图像中的所有通道。

def MatrixToMultiChannel(f,x,*args,**kwargs):
    nchannels=x.shape[-1]
    y=np.reshape(x,(x.size/nchannels,nchannels))
    for i in range(0,nchannels):
        yi=np.reshape(y[:,i],x.shape[:x.ndim-1])
        m1=genericF(f,yi,*args, **kwargs)
        m1=np.reshape(m1,m1.shape+(1,))
        if(i==0):
            fout=m1
        else:
            fout=np.concatenate((fout,m1),axis=m1.ndim-1)
    return fout