【问题标题】:Stacking images as numpy array将图像堆叠为 numpy 数组
【发布时间】:2014-10-05 10:25:08
【问题描述】:

我正在尝试使用 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)

【问题讨论】:

    标签: python arrays image numpy


    【解决方案1】:

    试试这个:

    # 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 2018-06-05
      • 2022-10-15
      • 2021-10-11
      • 2019-10-05
      • 2021-08-14
      相关资源
      最近更新 更多