numpy.array(image)
和img_to_array(image)
函数有什么区别?img_to_array
位于keras.preprocessing.image
包中,我想将图片作为输入传递给这个函数。numpy.array(image)
和img_to_array(image)
函数有什么区别?img_to_array
位于keras.preprocessing.image
包中,我想将图片作为输入传递给这个函数。那么,您可以通过查看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数组)。x = np.asarray(img, dtype=dtype)
)。img_to_array()
是一个图像类的方法。该类并不代表一个数组,而是更抽象的东西,但图像本质上是一个数组。这可能就是为什么使用numpy.array(image)
会得到类似的结果。
请注意,随着方法拥有更多信息(称之为“上下文”),它们应该更有效和可靠。例如,当涉及表示时,opencv2正在操作BGR图像,而不是RGB。起初可能会感到困惑,但使用正确的cv2库,您甚至不必真正考虑它(取决于您打算做什么)。
img_to_array
中默认的数据类型是'float32'
,而在使用np.array
时,默认的数据类型将是表示数据所需的最小类型。在这两种情况下,您都可以使用dtype
参数显式指定所需的数据类型。 - todaynp.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