2014-10-05 72 views
1

我想使用for-loop来堆叠6个不同的图像,一个在另一个之上来创建一个3D堆栈。我是Python新手......我无法这一点。我如何创建堆栈,以后如何访问堆栈中的每个图像?我的代码是有点像这样...堆叠图像作为numpy数组

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) 

回答

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