Skip to main content
 首页 » 编程设计

python之你如何将 numpy 数组中的图像集合 reshape 为一个大图像

2024年10月24日30lyj

我在将 4D numpy 数组 reshape 为 2D numpy 数组时遇到了一些问题。目前 numpy 数组如下,(35280L, 1L, 32L, 32L)。格式为图像数量、 channel 、宽度、高度。基本上,我有 35280 个 32x32 的图像 block ,我想组合图像 block (保留索引)来创建一个大图像。

请您参考如下方法:

reshape 是不够的,您必须使用 swapaxes 仔细地重新排列您的数据。

示例数据:

dims=nbim,_,h,w=np.array([6,1,7,6]) 
data=arange(dims.prod()).reshape(dims)%256 

图像:

figure() 
for i in range(nbim): 
    subplot(1,nbim,i+1) 
    imshow(data[i,0],vmin=0,vmax=255) 

和大图:

#number of images in each dim : 
nh = 2 # a choice 
nw=nbim // nh 
 
bigim=data.reshape(nh,nw,h,w).swapaxes(1,2).reshape(nh*h,nw*w) 
figure()     
imshow(bigim)