LAB Color Space - OpenCV 中的颜色校正矩阵

Color Correction Matrix in LAB Color Space - OpenCV

假设图像中有 5 个圆圈的 l、a、b 值。这些值是使用 OpenCV 计算的。

imlab=cv2.cvtColor(circle_img_only,cv2.COLOR_BGR2LAB).astype("float32")

实际上,我们从每个圆中随机取 100 个像素,并计算每个圆的正常平均 LAB 值(我不确定这是正确的做法)

值 np.array 类似于以下内容:

LAB Measured Colors Values =
[[ 27.553 -26.39    7.13 ]
 [ 28.357 -27.08    7.36 ]
 [ 28.365 -27.01    7.21 ]
 [ 29.749 -27.78    7.42 ]
 [ 28.478 -26.81    7.14 ]]

这些圆圈也是用色度计测量的。色度计生成参考值。

LAB Reference Colors Values =
[35.07, -24.95, 3.12]
[35.09, -24.95, 3.18]
[35.0, -25.6, 3.21]
[34.97, -25.76, 3.36]
[35.38, -24.55, 2.9]

我们将 LAB 测量颜色值称为 m1 让我们将 LAB 参考颜色值称为 m2

我们有测量值和参考值。 我们如何计算 CCM - 颜色校正矩阵?

我使用以下方法做到这一点:

def first_order_colour_fit(m_1, m_2 , rcond=1):

"""
Colour Fitting
==============

Performs a first order colour fit from given :math:`m_1` colour array to
:math:`m_2` colour array. The resulting colour fitting matrix is computed
using multiple linear regression.

The purpose of that object is for example the matching of two
*ColorChecker* colour rendition charts together

Parameters
----------
m_1 : array_like, (3, n)
    Test array :math:`m_1` to fit onto array :math:`m_2`.
m_2 : array_like, (3, n)
    Reference array the array :math:`m_1` will be colour fitted against.


Simply: Creating and clculating CCM - Color Correction Matrix
"""



print('CCM - Color Correction Matrix = ')
ColorCorrectionMatrix = np.transpose(np.linalg.lstsq(m_1, m_2 , rcond)[0])

这会生成:

CCM - Color Correction Matrix =
[[-0.979 -2.998 -2.434]
 [ 0.36   1.467  0.568]
 [ 0.077  0.031  0.241]]

获得 CCM 后 - 我想在 m1(LAB 测量颜色)上应用 CCM,并修正它们。

我们该怎么做?

我正在执行以下操作,但结果似乎不正常:

def CorrectedMeasuredLABValues(measured_colors_by_app , ColorCorrectionMatrix , reference_LAB_colors_values ):

CorrectedMeasured_LAB_Values = np.zeros_like(measured_colors_by_app , dtype=object)


print('\n\n\n\n Corrected Measured LAB Values Matrix = ')
for i in range(measured_colors_by_app.shape[0]):
    print(ColorCorrectionMatrix.dot(measured_colors_by_app[i]))
    CorrectedMeasured_LAB_Values[i] = ColorCorrectionMatrix.dot(measured_colors_by_app[i])

我们得到以下信息:

 Corrected Measured LAB Values Matrix =
[ 34.766 -24.742   3.033]
[ 35.487 -25.334   3.129]
[ 35.635 -25.314   3.096]
[ 36.076 -25.825   3.23 ]
[ 35.095 -25.019   3.094]

如果你这样做

ColorCorrectionMatrix = np.linalg.lstsq(m_1, m_2)[0]

然后

m_3 = np.matmul(m_1, ColorCorrectionMatrix)

应该return一个接近m_2的数组m_3。即第一行求解方程

m_1 x = m_2

在least-squares意义上;因此 m_1np.linalg.lstsq 找到的 x 的简单矩阵乘法应该近似于 m_2.

这意味着您应该在 ColorCorrectionMatrix 的计算中删除转置。

但是! 此更正对错过翻译的颜色应用了转换。由 a 和 b 跨越的实验室 space 中的平面是色度平面。该平面的原点代表 white/grey(无色)。如果一张图片需要调整白点(白平衡),那就意味着真正的白色不在这个平面的原点。需要翻译才能将其移动到那里,再多的乘法也无法完成此操作。

需要解的方程是

m_1x+y=m_2

(其中 y 是白点校正)。如果我们向 m_1m_2 添加一列 1,则可以将其重写为单个矩阵乘法。这称为齐次坐标,请参阅 this Wikipedia article 了解它的外观。

在 RGB space 中计算颜色校正时,不会出现此问题。在 RGB 中,原点永远不会移动:黑色就是黑色。 RGB 值始终为正。白平衡是通过乘法来完成的。

我建议您将色度计参考值转换为 RGB,而不是将图像像素转换为 Lab,并以 RGB space 执行色彩校正。请确保您记录的图像是线性 RGB space,而不是 sRGB,即 non-linear(如果您的图像保存为 sRGB,您会在线找到转换方程式)。

在线性 RGB space 中,按照您在实验室 space.

中所做的相同方式对像素值进行平均是完全没问题的