numpy.array与img_to_array的区别

5
numpy.array(image)img_to_array(image)函数有什么区别?img_to_array位于keras.preprocessing.image包中,我想将图片作为输入传递给这个函数。
2个回答

7

那么,您可以通过查看img_to_array的源代码来轻松找到答案:

def img_to_array(img, data_format='channels_last', dtype='float32'):
    """Converts a PIL Image instance to a Numpy array.
    # Arguments
        img: PIL Image instance.
        data_format: Image data format,
            either "channels_first" or "channels_last".
        dtype: Dtype to use for the returned array.
    # Returns
        A 3D Numpy array.
    # Raises
        ValueError: if invalid `img` or `data_format` is passed.
    """
    if data_format not in {'channels_first', 'channels_last'}:
        raise ValueError('Unknown data_format: %s' % data_format)
    # Numpy array x has format (height, width, channel)
    # or (channel, height, width)
    # but original PIL image has format (width, height, channel)
    x = np.asarray(img, dtype=dtype)
    if len(x.shape) == 3:
        if data_format == 'channels_first':
            x = x.transpose(2, 0, 1)
    elif len(x.shape) == 2:
        if data_format == 'channels_first':
            x = x.reshape((1, x.shape[0], x.shape[1]))
        else:
            x = x.reshape((x.shape[0], x.shape[1], 1))
    else:
        raise ValueError('Unsupported image shape: %s' % (x.shape,))
    return x

因此,主要区别在于您可以将数据格式参数传递给img_to_array,以将通道放置在第一个轴或最后一个轴上。此外,它会确保返回的数组是一个3D数组(例如,如果给定的输入img是一个可能表示灰度图像的2D数组,则会添加另一个维度为1的轴以使其成为3D数组)。
请注意,尽管在docstring中已经提到输入图像是PIL图像实例,但它也可以与numpy数组或甚至Python列表一起使用(因为首先将输入转换为numpy数组:x = np.asarray(img, dtype=dtype))。

这两个函数的输出结果相同吗? - Nishant
1
@nish2288 是的,返回的值将是相同的。但请注意,在 img_to_array 中默认的数据类型是 'float32',而在使用 np.array 时,默认的数据类型将是表示数据所需的最小类型。在这两种情况下,您都可以使用 dtype 参数显式指定所需的数据类型。 - today
今天对于一张图片,我使用np.array得到了[-44.5 -50.5 -51.5 -53.5 -58.5 -54.5 -26.5 -10.5 -14.5],而使用img_to_array则得到了[ 83. 77. 76. 74. 69. 73. 101. 117. 113.]。有任何想法吗?因为我没有改变任何默认设置。 PS:这是最后一个滤波器的前10个。 - Kanishk Mair

1
据我所见,在一些示例中,img_to_array()是一个图像类的方法。该类并不代表一个数组,而是更抽象的东西,但图像本质上是一个数组。这可能就是为什么使用numpy.array(image)会得到类似的结果。

请注意,随着方法拥有更多信息(称之为“上下文”),它们应该更有效和可靠。例如,当涉及表示时,opencv2正在操作BGR图像,而不是RGB。起初可能会感到困惑,但使用正确的cv2库,您甚至不必真正考虑它(取决于您打算做什么)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接