[英]Stacking images as numpy array
I'm trying to use a for-loop to stack 6 different images one on top of another to create a 3D stack.I'm new to Python...and I am not able to figure this out. 我正在尝试使用for循环将6张不同的图像堆叠在一起以创建3D堆叠。我是Python的新手...我无法弄清楚这一点。 How can I create the stack and how can I access each image in the stack later?
如何创建堆栈,以后如何访问堆栈中的每个图像? My code is somewhat like this...
我的代码有点像这样...
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)
Try this: 尝试这个:
# 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)
The code below is a more general implementation (from which my answer above was taken) to apply a function designed to be applied to a single channel to all channels in the image. 下面的代码是一个更通用的实现(上面的回答来自我的回答),该函数将设计为应用于单个通道的功能应用于图像中的所有通道。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.