MATLAB: Trouble formulating minimization problem

image processing

I have the following minimization problem that I am trying solve in Matlab, but just not coming up with the right way to formulate it.
Given an image matrix V, I need to minimize the error function (not in Matlab format)
error = ∑ ( V(x,y) – f(x,y) )^2, summed over 1600 points (x,y)
where f(x,y) = P1 * x^2 + P2 * y^2 + P3 *x*y + P4*x + P5*y + P6
The goal is to determine the P1-P6 values. Any help would be greatly appreciated.
Keith

Best Answer

I assume V is of double type.
[m,n] = size(V);
[x,y] = ndgrid(1:m,1:n);
x = x(:); y = y(:);
P = [x.^2,y.^2,x.*y,x,y,ones(m*n,1)]\V(:);
The elements of P will be your desired least square coefficients.
Related Question