Find unknown $5\times 5$ matrix

72 Views Asked by At

I am looking for a way to find a $5\times 5$ matrix. The matrix is used to transform colors in a photo. The matrix transforms red, green, blue, and opacity values into new values like this : $$\begin{pmatrix}R'\\G'\\B'\\A'\\1\end{pmatrix}=\begin{pmatrix}r1&r2&r3&r4&r5\\ g1&g2&g3&g4&g5\\ b1&b2&b3&b4&b5\\ a1&a2&a3&a4&a5\\ 0&0&0&0&1\end{pmatrix}\cdot\begin{pmatrix}R\\G\\B\\A\\1\end{pmatrix}$$

The last line of the matrix has know values and the effect I am looking for doesn't include opacity changes so $A'$ and $A$ are always equal to 1.

The effect I need on images is already made with an image editing software but I need to find this matrix to use it in an SVG filter. I can therefore identify $R', G', B', A'$ and $R,G,B, A$ for many color transformations.

The issue is that I cannot identify the unknow values in the matrix as each time I set the equations, I end up with too many uknowns.

Is it even possible to find the values of the $5\times 5$ matrix ?

This is a follow up question from this stackoverflow post : Convert photoshop adjustement layer to SVG filter

1

There are 1 best solutions below

2
On

Take $5$ random RGBA values $R_k, G_k, B_k, A_k$ for $k = 1,2,3,4,5$ and record the corresponding values $R'_k, G'_k, B'_k, A'_k$ after the application of the filter. We have $$\begin{bmatrix} R'_1 & R'_2 & R'_3 & R'_4 & R'_5 \\ G'_1 & G'_2 & G'_3 & G'_4 & G'_5 \\ B'_1 & B'_2 & B'_3 & B'_4 & B'_5 \\ A'_1 & A'_2 & A'_3 & A'_4 & A'_5 \\ 1 & 1 & 1 & 1 & 1 \end{bmatrix} = M \cdot \begin{bmatrix} R_1 & R_2 & R_3 & R_4 & R_5 \\ G_1 & G_2 & G_3 & G_4 & G_5 \\ B_1 & B_2 & B_3 & B_4 & B_5 \\ A_1 & A_2 & A_3 & A_4 & A_5 \\ 1 & 1 & 1 & 1 & 1 \end{bmatrix}$$ where $M$ is out unknown matrix. $M$ can now be recovered as $$M = \begin{bmatrix} R'_1 & R'_2 & R'_3 & R'_4 & R'_5 \\ G'_1 & G'_2 & G'_3 & G'_4 & G'_5 \\ B'_1 & B'_2 & B'_3 & B'_4 & B'_5 \\ A'_1 & A'_2 & A'_3 & A'_4 & A'_5 \\ 1 & 1 & 1 & 1 & 1 \end{bmatrix} \cdot \begin{bmatrix} R_1 & R_2 & R_3 & R_4 & R_5 \\ G_1 & G_2 & G_3 & G_4 & G_5 \\ B_1 & B_2 & B_3 & B_4 & B_5 \\ A_1 & A_2 & A_3 & A_4 & A_5 \\ 1 & 1 & 1 & 1 & 1 \end{bmatrix}^{-1}$$

If you do not care about opacity, then the fourth row of $M$ is $0, 0, 0, 0, 1$ and the fourth column is $0$, the rest can be found analogously as $$\begin{bmatrix} r_1 & r_2 & r_3 & r_5 \\ g_1 & g_2 & g_3 & g_5 \\ b_1 & b_2 & b_3 & b_5 \\ 0 & 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix} R'_1 & R'_2 & R'_3 & R'_4 \\ G'_1 & G'_2 & G'_3 & G'_4 \\ B'_1 & B'_2 & B'_3 & B'_4 \\ 1 & 1 & 1 & 1 \end{bmatrix} \cdot \begin{bmatrix} R_1 & R_2 & R_3 & R_4 \\ G_1 & G_2 & G_3 & G_4 \\ B_1 & B_2 & B_3 & B_4 \\ 1 & 1 & 1 & 1 \end{bmatrix}^{-1}$$