我试图通过将像素的R、G、B值(0到255)与其可见光的单独带宽映射来提取像素的波长 wavelengths
这是我当前的Python程序👇
#variable
r, g, b = 245, 122, 155 #let's take RGB value of a pixel
def my_map(x, in_min, in_max, out_min, out_max): #Prominent map function
return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
# Map 8 bits value of R,G,B (0 to 255) to their bandwidth of wavelength
R = my_map(r, 0, 255, 499, 700)# rgb 0 = 499 nm, rgb 255 = 700
G = my_map(g, 0, 255, 440 , 580) # rgb 0 = 440 nm, rgb 255 = 580
B = my_map(b, 0, 255, 380, 490) # rgb 0 = 440 nm, rgb 255 = 490
print('\nWavelength of R', r , 'is', R, '\nWavelength of G', g , 'is', G, '\nWavelength of B', b , 'is', B,)
#mean of constituent wavelengths
Wavelength = R+G+B
Wavelength /= 3
print('\nWavelength : ', Wavelength )
map 功能工作正常,r或g或b的各个波长值准确,但波长是整个可见光谱中的点值,等等
在上面的代码中,我取了 RGB 的平均值,但它不准确。我知道这不是办法。
如何才能准确测量波长?
最佳答案
没有什么可以保证您选择的 RGB 值位于光谱轨迹上,因此您需要找到 dominant wavelength .
您需要知道您的 RGB 值使用哪个 RGB 颜色空间进行编码,转换为 CIE XYZ 三刺激值,然后转换为 CIE xy 色度坐标,最后,您可以计算主波长。在您的情况下,由于您的 RGB 值是整数,因此您还需要将它们标准化为浮点表示,同时不要忘记应用逆编码颜色分量传输函数。
使用Colour ,我维护它,并假设这些值是用 sRGB 编码的,这里是你可以做到的:
import colour
import numpy as np
RGB_f = np.array([245.0, 122.0, 155.0]) / 255
# Using the individual definitions:
RGB_l = colour.models.eotf_sRGB(RGB_f)
XYZ = colour.RGB_to_XYZ(
RGB_l,
colour.models.RGB_COLOURSPACE_sRGB.whitepoint,
colour.models.RGB_COLOURSPACE_sRGB.whitepoint,
colour.models.RGB_COLOURSPACE_sRGB.matrix_RGB_to_XYZ,
)
xy = colour.XYZ_to_xy(XYZ)
wl, xy_1, xy_2 = colour.dominant_wavelength(
xy, colour.models.RGB_COLOURSPACE_sRGB.whitepoint
)
# Using the automatic colour conversion graph:
wl, xy_1, xy_2 = colour.convert(RGB_f, "Output-Referred RGB", "Dominant Wavelength")
print(wl, xy_1, xy_2)
colour.plotting.colour_style()
figure, axes = colour.plotting.plot_chromaticity_diagram_CIE1931(
diagram_opacity=0.15, standalone=False
)
xy_i = np.vstack([xy_1, xy, colour.models.RGB_COLOURSPACE_sRGB.whitepoint, xy_2])
axes.plot(xy_i[..., 0], xy_i[..., 1], "-o")
colour.plotting.render()
-496.0 [ 0.63564364 0.21924977] [ 0.0211551 0.42807958]
所以在这里,您会注意到主波长为负,因为最近的交点位于紫色线上,因此返回补波长。 p>
关于python - 如何在Python中将RGB转换为波长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71977306/