Minimising a matrix equation to find 'best fit' affine matrix

882 Views Asked by At

Here is my problem:

I have an image divided into segments. Each segment consists of pixels with coordinates (x,y) called vector $v$, each pixel has a length 3 vector RGB called $I(v)$. I want to find an affine matrix (3x3) $A$ which minimises $I(v) - A[v^T 1]^T$, however not just for the pixel, but for the whole segment. This means that the minimisation equation becomes $\sum\limits_{v \in segment}||I(v) - A[v^T 1]^T ||^2$.

How would I go about minimising this? Preferably with functions in numpy/scipy or similar (e.g. MATLAB/Octave).

1

There are 1 best solutions below

0
On

I am assuming that a segment can be written as $\{v_i\}_{i=1}^n \subset \mathbb{R}^2$.

Let $x = (x_1^T,x_2^T,x_3^T)^T \in \mathbb{R}^8$ represent the 'free' elements of $A$. Here $x_1,x_2 \in \mathbb{R}^3$, $x_3 \in \mathbb{R}^2$. That is, we have:

$$A = \begin{bmatrix} x_1^T \\ x_2^T \\ x_3^T & 1\end{bmatrix}.$$

For each $i \in \{1,...,n\}$, define $B_i \in \mathbb{R}^{3 \times 8}$, and $b_i \in \mathbb{R}^3$ by: $$B_i = \begin{bmatrix} (v_i^T 1) & & \\ & (v_i^T 1) & \\ & & v_i^T\end{bmatrix}, \; \; b_i = I(v_i) -\begin{bmatrix} 0 \\ 0 \\ 1\end{bmatrix}$$

Then $$ || B_ix-b_i||^2 = ||I(v_i) - A[v_i^T 1]^T ||^2.$$

Form $B \in \mathbb{R}^{3n \times 8}$ and $b \in \mathbb{R}^{3n}$ by 'stacking' the $B_i, b_i$ on top of each other.

Then you can write the problem as $\min_{x \in \mathbb{R}^8} || Bx-b||^2$.