Spark如何使用图像格式读取图像?

问题描述 投票:0回答:1

这可能是一个愚蠢的问题,但我无法弄清楚Spark如何使用spark.read.format("image").load(....)参数读取图像。

导入我的图像后,可以得到以下内容:

>>> image_df.select("image.height","image.width","image.nChannels", "image.mode", "image.data").show()
+------+-----+---------+----+--------------------+
|height|width|nChannels|mode|                data|
+------+-----+---------+----+--------------------+
|   430|  470|        3|  16|[4D 55 4E 4C 54 4...|
+------+-----+---------+----+--------------------+

我得出的结论是:

  • 我的图像是430x470像素,
  • 我的图像是彩色的(由于nChannels = 3,所以是RGB),这是一种openCV兼容类型,
  • 我的图像模式是16,它对应于特定的openCV字节顺序。
    • 有人知道我可以浏览哪个网站/文档以了解更多信息吗?
  • data列中的数据为Binary类型,但:
    • 当我运行image_df.select("image.data").take(1)时,我得到的输出似乎只是一个数组(请参见下文)。
>>> image_df.select("image.data").take(1)

# **1/** Here are the last elements of the result
....<<One Eternity Later>>....x92\x89\x8a\x8d\x84\x86\x89\x80\x84\x87~'))]

# 2/ I got also several part of the result which looks like:
.....\x89\x80\x80\x83z|\x7fvz}tpsjqtkrulsvmsvmsvmrulrulrulqtkpsjnqhnqhmpgmpgmpgnqhnqhn
qhnqhnqhnqhnqhnqhmpgmpgmpgmpgmpgmpgmpgmpgnqhnqhnqhnqhnqhnqhnqhnqhknejmdilcilchkbh
kbilcilckneloflofmpgnqhorioripsjsvmsvmtwnvypx{ry|sz}t{~ux{ry|sy|sy|sy|sz}tz}tz}tz}
ty|sy|sy|sy|sz}t{~u|\x7fv|\x7fv}.....

接下来会链接到上面显示的结果。这些可能是由于我缺乏有关openCV的知识(或其他)。尽管如此:

  • 1 /我不明白这样一个事实,如果我得到RGB图像,我应该有3个矩阵,但是输出以.......\x84\x87~'))]结尾。我更想获得类似[(...),(...),(...\x87~')]的东西。
  • 2 /这部分有特殊含义吗?像那些分隔符一样,每个矩阵之间还是什么之间的分隔符?

为了更清楚地了解我要实现的目标,我想处理图像以在每个图像之间进行像素比较。因此,我想知道图像中给定位置的像素值(我假设如果我有RGB图像,则给定位置应具有3个像素值)。

[示例:假设我有一个摄像头仅在白天指向天空,并且我想知道与左上角天空部分相对应的位置的像素值,我发现这些值的串联给出了颜色浅蓝色,表示照片是在晴天拍摄的。假设唯一的可能性是晴天是Light Blue。接下来,我想将前一个串联与另一个像素值串联在完全相同的位置进行比较,但要比较第二天拍摄的照片。如果我发现它们不相等,那么我得出结论,给定的照片是在阴天/雨天拍摄的。如果相等,则为晴天。

任何帮助,我们将不胜感激。我对示例进行了粗俗化以更好地理解,但是我的目标几乎是相同的。我知道可以使用ML模型来实现这些目标,但是我很乐意首先尝试。我的第一个目标是将该列分为与每个颜色代码相对应的3列:红色矩阵,绿色矩阵,蓝色矩阵

image-processing pyspark apache-spark-sql pixel
1个回答
0
投票

我认为我有逻辑。我使用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,)-实际上,每个元素都是有序的,并且分别对应于它们各自的颜色阴影(RG B)。经过一些小的变换,我得到了3列x 470行的430矩阵。由于我的图像的宽度(HeightxHeight)为(470x430),因此每个矩阵都对应一个像素高度位置,并且在每个内部:每个颜色3列和每个宽度位置470行。

希望对某人有帮助的人:)!

© www.soinside.com 2019 - 2024. All rights reserved.