xavysp
xavysp

Reputation: 73

How make a 3x4 color correction matrix with least-square manner?

I know this question will sound simple but yes, I want to get a 3x4 Color Correction Matrix (CCM) with least-square method for this data b=M*A where its dimensions are b_3x10, M_3x4 and A_4x10 Here the values:

A =
0.0194 0.0193 0.0180 0.0167 0.0175 0.0183 0.0177 0.0172 0.0178 0.0185
0.0032 0.0099 0.0274 0.0089 0.0034 0.0070 0.0042 0.0079 0.0031 0.0037
0.0845 0.0579 0.0444 0.0407 0.0367 0.0481 0.0596 0.0484 0.0375 0.0394
0.0753 0.0814 0.0991 0.0922 0.0794 0.0785 0.0774 0.0759 0.0707 0.0640

b=
0.0111 0.0111 0.0110 0.0109 0.0111 0.0113 0.0112 0.0110 0.0108 0.0106
0.0017 0.0048 0.0013 0.0024 0.0008 0.0021 0.0012 0.0029 0.0006 0.0012
0.0887 0.0778 0.0739 0.0708 0.0677 0.0694 0.0711 0.0685 0.0660 0.0654

M =
m_11 m_12 m_13 m_14
m_21 m_22 m_23 m_24
m_31 m_32 m_33 m_34

Here matlab variables they are RGB-NIR and RGB data

I have read this article but I do not know how to use with my A and b dimensions.

------------- Result ------------------- with the first answer

M = 
    0.4272    0.0008    1.7853
   -0.0573    0.0212   -0.0913
   -0.0108    0.0222    0.3717
    0.0542    0.0077    0.2767

and b approximation is:

0.0113 0.0115 0.0110 0.0112 0.0112 0.0112 0.0109 0.0105 0.0108 0.0107
0.0025 0.0021 0.0023 0.0018 0.0015 0.0018 0.0020 0.0018 0.0015 0.0015
0.0865 0.0776 0.0736 0.0696 0.0666 0.0717 0.0747 0.0690 0.0649 0.0651

Upvotes: 0

Views: 1334

Answers (2)

rayryeng
rayryeng

Reputation: 104555

Usually in least-squares problems, we solve for A*x = b, but in your case the situation is such that the coefficients you want to solve for appear at the beginning of the expression: x*A = b. Note that we can reformulate this into the standard form by transposing: x.' * A.' = b.'. After that you can use MATLAB's mldivide operator, but you'll have to put it in the above form, then transpose the result when you're finished:

M = (A.' \ b.').';

Usually in least-squares problems, x and b are column vectors but you have them as matrices instead. MATLAB can still do the job for you as it will be solving for the least-squares solutions of each column of the matrix A independently, but in your case that would be each row of M as it is transposed.

The above is preferred over using inv as it is known to be numerically unstable especially for larger sized matrices. MATLAB's mldivide automatically determines what algorithm to use based on a variety of factors such as the condition number, the structure of the coefficients matrix etc.

Upvotes: 1

Joseph Artsimovich
Joseph Artsimovich

Reputation: 1519

I haven't worked with color correction matrices before, so I don't really understand the context, but from a purely mathematical point of view, the solution should be:

M = (inv(A*A')*A*b')'

For derivation, let's rewrite b = M*A into a canonical form A*x = b with x being the unknown, giving us M'*A' = b'. The least-squares solution to A*x = b is x = inv(A'*A)*A'*b, so in our case it's M' = inv(A*A')*A*b'. Normally, x is assumed to be a vector rather than a matrix, though the formulas work for matrices as well, as it's equivalent of solving a separate least-squares problem for each column of x (that is row of M).

Upvotes: 1

Related Questions