img_to_array

as name suggests,img_to_array就是将图片转化成数组。

from keras.preprocessing.image import img_to_array
import cv2
image = cv2.imread('dcl.jpg')
print('[DEBUG]', type(image)) #  'numpy.ndarray'>
image = img_to_array(image)
print('[DEBUG]', type(image)) #  'numpy.ndarray'>

img_to_array 转换前后类型都是一样的,唯一区别是转换前元素类型是整型,转换后元素类型是浮点型(和keras等机器学习框架相适应的图像类型。Keras introduced a special function called img_to_array which accepts an input image and then orders the channels correctly based on the image_data_format setting)。

在 /.keras/keras.json.文件下,有如下的设置:

{
"epsilon": 1e-07,
"floatx": "float32",
"image_data_format": "channels_last",
"backend": "tensorflow"
}

image_data_format 有两个值:
channels_last
channels_first

由于历史原因,
tensorflow中为channels_last,即图片格式为(heights,weights,channels)
theano中为channels_first,即图片格式为(channels,heights,weights)

为了兼容 tensorflow 和 theano 两种后端,所以有了 img_to_array 。。。

你可能感兴趣的:(计算机视觉)