2016-10-20 20 views
0

我想要自定义一个现有的代码来适应我自己的需要。最初,代码使用imgs = np.ndarray((total, 1, image_rows, image_cols), dtype=np.uint8)以numpy数组格式存储图像文件列表。迭代文件夹,每个图像文件读取如下img = skimage.io.imread(os.path.join(train_data_path, image_name))它工作得很好。 的代码如下:关于修改从输入图像产生的numpy数组的形状

image_rows = 420 
image_cols = 580 
imgs = np.ndarray((total, 1, image_rows, image_cols), dtype=np.uint8) 
i=0 
for image_name in images: 
    img = skimage.io.imread(os.path.join(train_data_path, image_name)) 
    img = np.array([img]) 
    imgs[i]=img 
    i+=1 

为了满足我自己的需要,我倾向于有图像文件阵列形状[total, image_rows,image_cols,1]。换句话说,我修改了它作为imgs = np.ndarray((total,image_rows, image_cols,1), dtype=np.uint8)然而,运行代码将导致以下错误

imgs[i] = img 
ValueError: could not broadcast input array from shape (1,420,580) into shape   
(420,580,1) 

有没有办法改变的img的形状,原本具有的[1,420,580]形状从文件中读取后。如何将其更改为[420,580,1]而不影响图像中的相应像素值。

回答

1

您想调换尺寸。它可以通过转置的方法来实现:

img = img.transpose(1,2,0) 

(你的情况)

+0

感谢,将img.reshape工作。我也试过重塑。看起来代码可以编译。我唯一担心的问题是如何确保[1,420,580]中的像素可以完全匹配像素[420,580,1]。 – user288609

相关问题