LAB色彩空间中的色彩校正矩阵-OpenCV [英] Color Correction Matrix in LAB Color Space - OpenCV

查看:936
本文介绍了LAB色彩空间中的色彩校正矩阵-OpenCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们在一个图像中有5个圆的l,a,b值.这些值是使用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")

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

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)

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

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]

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

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

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

这将生成:

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,并进行更正.

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

我们得到以下信息:

 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)

应返回接近m_2的数组m_3.也就是说,第一行求解方程式

should return an array m_3 that is close to m_2. That is, the first line solves the equation

m_1 x = m_2

;因此,m_1np.linalg.lstsq发现的x的简单矩阵相乘应近似为m_2.

in the least-squares sense; and therefore a simple matrix multiplication of m_1 with the x found by np.linalg.lstsq should approximate m_2.

这意味着您应该在计算ColorCorrectionMatrix时除去转置.

This means you should remove the transpose in your calculation of ColorCorrectionMatrix.

但是!.此更正对缺少翻译的颜色进行了转换. Lab空间中由a和b跨越的平面是色度平面.该平面原点的点表示白色/灰色(无色).如果图片需要白点调整(白平衡),则意味着真正的白色不在此平面的原点.需要进行翻译才能将其移到该位置,而无需大量的乘法就可以完成此操作.

But! This correction applies a transformation to the colors that misses a translation. The plane in the Lab space spanned by a and b is the chromaticity plane. The point at the origin of this plane represents white/grey (colorless). If a picture needs white point adjustment (white balancing), it means that what is true white is not at the origin of this plane. A translation is needed to move it there, no amount of multiplications will be able to accomplish this.

需要求解的方程是

m_1 x + y = m_2

(其中 y 是白点校正).如果我们在m_1m_2中添加一列,则可以将其重写为单个矩阵乘法.这称为齐次坐标,请参见这篇Wikipedia文章,以了解其外观

(where y is the whitepoint correction). This can be rewritten as a single matrix multiplication if we add a column of ones to m_1 and m_2. This is called homogeneous coordinates, see this Wikipedia article for an idea of what this looks like.

在RGB空间中计算色彩校正时,不会发生此问题.在RGB中,原点永远不会移动:黑色是黑色. RGB值始终为正.白平衡通过乘法来实现.

When computing color correction in RGB space, this problem does not occur. In RGB, the origin never moves: black is black. RGB values are always positive. White balancing is accomplished with a multiplication.

我建议您将色度计参考值转换为RGB,而不是将图像像素转换为Lab,然后在RGB空间中执行颜色校正.确保确保记录的图像位于线性RGB空间中,而不是sRGB,后者是非线性的(如果事实证明图像另存为sRGB,则会在线找到转换方程式.)

I would recommend that you convert your colorimeter reference values to RGB, instead of your image pixels to Lab, and perform the color correction in RGB space. Do make sure that the image you record is in linear RGB space, not sRGB, which is non-linear (you'll find conversion equations online if it turns out your images are saves as sRGB).

在线性RGB空间中,以与在Lab空间中相同的方式对像素值求平均是非常好的.

In linear RGB space it is perfectly fine to average pixel values in the same way you did in Lab space.

这篇关于LAB色彩空间中的色彩校正矩阵-OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆