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

2024-01-12

这可能是一个愚蠢的问题,但我无法弄清楚 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 像素,
  • 我的图像是彩色的(RGB 由于 nChannels = 3),它是 openCV 兼容类型,
  • my image mode is 16 which corresponds to a particular openCV byte-order.
    • 有人知道我可以浏览哪个网站/文档来了解更多信息吗?
  • the data in the data column is of type Binary but:
    • 当我跑步时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.
接下来,我想将之前的连接与完全相同位置但来自第二天拍摄的照片的像素值的另一个连接进行比较。如果我发现它们不相等,那么我就得出结论,给定的照片是在阴天/雨天拍摄的。如果相等则晴天。

任何有关这方面的帮助将不胜感激。为了更好地理解,我对我的示例进行了庸俗化,但我的目标几乎是相同的。我知道机器学习模型可以实现这些目标,但我很乐意先尝试一下。我的第一个目标是将这一列分成与每个颜色代码相对应的 3 列:红色矩阵、绿色矩阵、蓝色矩阵


我想我有逻辑。我使用 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 行。

希望对某人有帮助:)!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spark 如何使用图像格式读取我的图像? 的相关文章

随机推荐