我想我有逻辑。我使用 keras.preprocessing.image.img_to_array() 函数来了解值是如何分类的(因为我有一个 RGB 图像,所以我必须有 3 个矩阵:每个颜色 R G B 一个)。如果有人想知道它是如何工作的,我可能是错的,但我认为我有一些东西:
from keras.preprocessing import image
import numpy as np
from PIL import Image
# Using spark built-in data source
first_img = spark.read.format("image").schema(imageSchema).load(".....")
raw = first_img.select("image.data").take(1)[0][0]
np.shape(raw)
(606300,) # which is 470*430*3
# Using keras function
img = image.load_img(".../path/to/img")
yy = image.img_to_array(img)
>>> np.shape(yy)
(430, 470, 3) # the form is good but I have a problem of order since:
>>> raw[0], raw[1], raw[2]
(77, 85, 78)
>>> yy[0][0]
array([78., 85., 77.], dtype=float32)
# Therefore I used the numpy reshape function directly on raw
# to have 470 matrix of 3 lines and 470 columns:
array = np.reshape(raw, (430,470,3))
xx = image.img_to_array(array) # OPTIONAL and not used here
>>> array[0][0] == (raw[0],raw[1],raw[2])
array([ True, True, True])
>>> array[0][1] == (raw[3],raw[4],raw[5])
array([ True, True, True])
>>> array[0][2] == (raw[6],raw[7],raw[8])
array([ True, True, True])
>>> array[0][3] == (raw[9],raw[10],raw[11])
array([ True, True, True])
因此,如果我理解得很好,spark 会将图像读取为一个大数组 - (606300,) - 实际上每个元素都是有序的并且对应于它们各自的颜色阴影 (R G B)。
经过我的小变换后,我获得了 3 列 x 470 行的 430 矩阵。由于我的图像 (WidthxHeight) 为 (470x430),因此每个矩阵对应于一个像素高度位置,并且每个矩阵内部:每种颜色 3 列,每个宽度位置 470 行。
希望对某人有帮助:)!