亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 703 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號