'Color Correction Matrix in LAB Color Space - OpenCV

Suppose we have l,a,b values for 5 circles inside an image. These values are calculated using OpenCV.

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

Actually, we take 100 random pixels from every circle and calculate normal average LAB value for every circle (Which I am not sure it is the right way to do)

values are np.array similar to the following:

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 ]]

Those circles are also measured using colorimeter instrument. The colorimeter generates a reference values.

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]

Lets call LAB Measured Colors Values as m1 Lets call LAB Reference Colors Values as m2

We have the measured values and the reference values. How can we calculate the CCM - Color Correction Matrix?

I do that using the following:

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])

This generates:

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

After getting the CCM - I want to apply the CCM on m1 (LAB Measured Colors), and correct them.

How can we do that ?

I am doing the following, however the results seems not ok:

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])

We get the following:

 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]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source