gpt4 book ai didi

python - 如何将XYZ颜色空间更改为RGB 0-255

转载 作者:行者123 更新时间:2023-12-02 16:09:24 25 4
gpt4 key购买 nike

我有这样的XYZ数据集:

[[  15.4999257    20.91432805    8.15938343]
[ 38.47211872 41.38471882 4.41641215]
[ 8.42304194 10.26972173 4.58244444]
[ 59.15345728 65.6834669 42.20671404]
[ 48.21549352 50.67621445 7.84460441]
[ 28.5987903 29.48986333 25.78866701]
[ 16.37042029 11.17896503 20.92325834]...]

我想将其更改为RGB 0-255:
import numpy as np

XYZ_to_RGB_matrix = [
[3.24062548, -1.53720797, -0.49862860],
[-0.96893071, 1.87575606, 0.04151752],
[0.05571012, -0.20402105, 1.05699594]]

my_rgb = (np.dot(XYZ_to_RGB_matrix, xyz_test) * 255).astype('uint8')

xyz_test只是XYZ数据集中的一个行数组,

但是,我发现结果与正确的标签不符,

例如, XYZ:[ 15.4999257 20.91432805 8.15938343]它的标签是绿叶
但是我的rgb结果是 [244 116 51],它不是绿色,所以我的代码中有一些错误吗?

最佳答案

试试这个代码:

from colormath.color_objects import sRGBColor, XYZColor
from colormath.color_conversions import convert_color

XYZ = [[15.4999257, 20.91432805, 8.15938343],
[38.47211872, 41.38471882, 4.41641215],
[8.42304194, 10.26972173, 4.58244444],
[59.15345728, 65.6834669, 42.20671404],
[48.21549352, 50.67621445, 7.84460441],
[28.5987903, 29.48986333, 25.78866701],
[16.37042029, 11.17896503, 20.92325834]]

RGB = []

for xyz_list in XYZ:
xyz = XYZColor(*[component/100 for component in xyz_list])
rgb = convert_color(xyz, sRGBColor)
rgb_list = [255*color for color in rgb.get_value_tuple()]
RGB.append(rgb_list)

print(RGB)

有了这段代码,我得到的输出为RGB矩阵:
[92.22372010136964, 137.4037125386466, 78.86090637192737]
[189.9967785535171, 173.00440159210268, 0.0]
[77.62483771390573, 95.56815462725146, 61.177682509553435]
[201.10203247049455, 216.97942244866422, 185.00583953450555]
[211.2583657458791, 187.8058726835025, 47.400682365292234]
[147.26702521874506, 147.4599728158739, 152.30904210885427]
[131.7100385873106, 69.84477339437986, 144.29850008120889]

请注意,您必须安装所需的软件包:
pip install colormath

关于python - 如何将XYZ颜色空间更改为RGB 0-255,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62180730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com