This project has moved and is read-only. For the latest updates, please go here.

issues with QR solver

Jul 1, 2011 at 10:35 AM

Hey there!

I've got two issues with the QR solver: most importantly I don't get the expected results and the other thing is that I get errors when trying to access the data.

 

I'm trying to do an image warp using data of an evenly spaced grid and a distorted grid. As a test case I started with a 3*3 evenly spaced grid with one point being displaced. For the calculation of the coefficients for the warp function I'm using the QR solver.
The warp function will be of the form x' = ax + by + cxy + dx^2y + exy^2 + fx^2 + gy^2 + hx^3 + iy^3. Currently I'm only testing the x coordinate of the warp with grid values 85, 255, 425 for x/y.
Here's how my code looks like (with the lists grid and gridwarp containing the 9 coordinates):

 

DenseMatrix COF = new DenseMatrix(9, 9);            
DenseMatrix CON = new DenseMatrix(9, 1);

for (int i = 0; i < grid.Count; i++)
{
double x = grid[i].x;
double xsquare = x * x;

double y = grid[i].y;
double ysquare = y * y;

COF[i, 0] = x;
COF[i, 1] = y;
COF[i, 2] = x * y;
COF[i, 3] = xsquare * y;
COF[i, 4] = x * ysquare;
COF[i, 5] = xsquare;
COF[i, 6] = ysquare;
COF[i, 7] = x * xsquare;
COF[i, 8] = y * ysquare;

CON[i, 0] = gridwarp[i].x;
}

QR qrmat = COF.QR();
DenseMatrix res = (DenseMatrix) qrmat.Solve(CON);

for (int i = 0; i < res.RowCount; i++)
{
coefficients_x.Add((double) res.Data.GetValue(i));
}

One weird thing here is that I can't directly access the result via res[0, i]. This works for res[0, 1] but from then on I will get an exception. However, accessing the data via res.Data like in the code works.
The following is the code for evaluating the position in the warped image:

 

x_warp = x * coefficients_x[0] + y * coefficients_x[1] + x * y * coefficients_x[2] + xsquare * y * coefficients_x[3] + x * ysquare * coefficients_x[4] + xsquare * coefficients_x[5] + ysquare * coefficients_x[6] + x * xsquare * coefficients_x[7] + y * ysquare * coefficients_x[8];

 

When displacing the grid element (2, 1) by -5 I will get for the x coordinate of element (0, 0) the value 76 instead of 85. This is strange since in both grids this element has the same coordinates.
However, when doing no displacement the values correspond so there my code can't be fundamentally wrong.

 

The questions now are:

Is there something wrong in my code?
Is the QR solver conceptually not stable enough for my needs?
Are there better methods I could use for my problem?

 

I'd very much appreciate your help!

Jul 1, 2011 at 11:40 AM
Hi,

>One weird thing here is that I can't directly access the result via res[0, i].
>This works for res[0, 1] but from then on I will get an exception. However,
>accessing the data via res.Data like in the code works.
I'm just going to address this, since I'm not sure QR is what you want
to use to solve that equation.

QR solves for X in AX=B. COF (A) is 9x9 and CON (B) is 9x1, so the
solution X will be 9x1. If you try accessing any column of than 0,
you'll get an out-of-bounds exception. res.Data gives you the data of
res matrix in a column-major array. When you loop through its values,
you are essentially getting res[0,0], res[1,0], ..., res[8,0],..., not
res[0,0], res[0,1]..., res[0,8]

Regards,
Marcus

On Fri, Jul 1, 2011 at 12:35 PM, bugmenot2 <notifications@codeplex.com> wrote:
> From: bugmenot2
>
> Hey there!
>
> I've got two issues with the QR solver: most importantly I don't get the
> expected results and the other thing is that I get errors when trying to
> access the data.
>
>
>
> I'm trying to do an image warp using data of an evenly spaced grid and a
> distorted grid. As a test case I started with a 3*3 evenly spaced grid with
> one point being displaced. For the calculation of the coefficients for the
> warp function I'm using the QR solver.
> The warp function will be of the form x' = ax + by + cxy + dx^2y + exy^2 +
> fx^2 + gy^2 + hx^3 + iy^3. Currently I'm only testing the x coordinate of
> the warp with grid values 85, 255, 425 for x/y.
> Here's how my code looks like (with the lists grid and gridwarp containing
> the 9 coordinates):
>
>
>
> DenseMatrix COF = new DenseMatrix(9, 9);
> DenseMatrix CON = new DenseMatrix(9, 1);
>
> for (int i = 0; i < grid.Count; i++)
> {
> double x = grid[i].x;
> double xsquare = x * x;
>
> double y = grid[i].y;
> double ysquare = y * y;
>
> COF[i, 0] = x;
> COF[i, 1] = y;
> COF[i, 2] = x * y;
> COF[i, 3] = xsquare * y;
> COF[i, 4] = x * ysquare;
> COF[i, 5] = xsquare;
> COF[i, 6] = ysquare;
> COF[i, 7] = x * xsquare;
> COF[i, 8] = y * ysquare;
>
> CON[i, 0] = gridwarp[i].x;
> }
>
> QR qrmat = COF.QR();
> DenseMatrix res = (DenseMatrix) qrmat.Solve(CON);
>
> for (int i = 0; i < res.RowCount; i++)
> {
> coefficients_x.Add((double) res.Data.GetValue(i));
> }
>
> One weird thing here is that I can't directly access the result via res[0,
> i]. This works for res[0, 1] but from then on I will get an exception.
> However, accessing the data via res.Data like in the code works.
> The following is the code for evaluating the position in the warped image:
>
>
>
> x_warp = x * coefficients_x[0] + y * coefficients_x[1] + x * y *
> coefficients_x[2] + xsquare * y * coefficients_x[3] + x * ysquare *
> coefficients_x[4] + xsquare * coefficients_x[5] + ysquare *
> coefficients_x[6] + x * xsquare * coefficients_x[7] + y * ysquare *
> coefficients_x[8];
>
>
>
> When displacing the grid element (2, 1) by -5 I will get for the x
> coordinate of element (0, 0) the value 76 instead of 85. This is strange
> since in both grids this element has the same coordinates.
> However, when doing no displacement the values correspond so there my code
> can't be fundamentally wrong.
>
>
>
> The questions now are:
>
> Is there something wrong in my code?
> Is the QR solver conceptually not stable enough for my needs?
> Are there better methods I could use for my problem?
>
>
>
> I'd very much appreciate your help!
>
> Read the full discussion online.
>
> To add a post to this discussion, reply to this email
> ([email removed])
>
> To start a new discussion for this project, email
> [email removed]
>
> You are receiving this email because you subscribed to this discussion on
> CodePlex. You can unsubscribe or change your settings on codePlex.com.
>
> Please note: Images and attachments will be removed from emails. Any posts
> to this discussion will also be available online at codeplex.com
Jul 1, 2011 at 2:19 PM

I thought I tried accessing both res[0,0], res[1,0], ..., res[8,0] and res[0,0], res[0,1], ..., res[0,8] and both didn't work. I guess I'm proved wrong! Anyway, thanks for pointing that out!

In the mean time I implemented the warping. I warped an image with coefficients gained with no distortion. The result however contains distortions, even if it shouldn't in this case.
I'd really appreciate more clues on the QR solver issue... thank you for looking into it Marcus!

Cheers,

Johann

Jul 6, 2011 at 8:19 AM

Have you had any insights yet?
I might change my implementation to use splines... would make things more complicated but could avoid numerical problems.

 

Cheers,

Johann