在python中将图像从彩色批量转换为灰度

时间:2017-04-21 11:20:48

标签: python batch-processing data-conversion

所以我有一批彩色图像,我想让它们成为灰度图像。唯一的问题是,有时图像的形状为[batch_size, channels, height, width],有时它们是[batch_size, height, width, channels]。我需要一个能够获取一批彩色图像的功能(无论它具有哪两种形状),并提供一批形状为[batch_size, height, width, channels]的灰度图像(当然,通道为1)。

到目前为止,我有这个功能:

from scipy import misc

def color_to_grayscale(image_batch, dim_order='NHWC'):

   grayscale_batch = np.array()

   if dim_order='NCHW':
    image_batches =  np.transpose(image_batch, [0, 2, 3, 1])
   else:
    image_batches = image_batch

   for idx in range(image_batches[0].shape):
    image = image_batches[idx, :, :, :]
    grayscale = np.zeros((image.shape[0], image.shape[1]))

    for rownum in range(len(image)):
        for colnum in range(len(image[rownum])):
            grayscale[rownum][colnum] = np.average(image[rownum][colnum])

    grayscale = np.array(grayscale, dtype="float32")
    grayscale = grayscale.reshape((grayscale.shape[0], grayscale.shape[1], 1))

    grayscale_batch = np.stack(grayscale, grayscale_batch)

return grayscale_batch

我正在考虑在for循环结束时执行np.vstack来重建批处理,但它看起来很乱。此外,我不在考虑上述两个案例(维度)。

有什么想法吗?

编辑:更新了我希望工作的代码(但仍然没有)。

1 个答案:

答案 0 :(得分:0)

您不需要进行任何循环,堆叠或任何操作,只需在频道轴上使用numpy.mean()

# axis=-1 is channels, keepdims is optional
greyscale_image_batch = numpy.mean(image_batch, axis=-1, keepdims=True)
image_batch.shape             # [batch_size, height, width, channels]
greyscale_image_batch.shape   # [batch_size, height, width, 1]

要使阵列形状正确,您可以使用transpose()

if image_batch.shape[1] == 3:
    image_batch = image_batch.transpose([0, 2, 3, 1])
相关问题