为了账号安全,请及时绑定邮箱和手机立即绑定

numpy.array 与 img_to_array

numpy.array 与 img_to_array

沧海一幻觉 2021-09-14 10:32:00
numpy.array(image)和img_to_array(image)功能和有什么不一样?img_to_array是在keras.preprocessing.image包裹内。我想将它与图像一起用作此函数的输入。
查看完整描述

2 回答

?
当年话下

TA贡献1890条经验 获得超9个赞

好吧,您可以通过查看以下源代码轻松找到答案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)。


查看完整回答
反对 回复 2021-09-14
  • 2 回答
  • 0 关注
  • 671 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号