Bilinear transformation not working

In summary, the bilinear transformation code seems correct, but there appears to be a .jpg file concerning your original matrices where you compute the $a_0, a_1,$ etc, that didn't load.
  • #1
Sneaky6666
14
0
I want to try this bilinear transformation of a rectangle to a quad described here
http://www.fmwconcepts.com/imagemagick/bilinearwarp/FourCornerImageWarp2.pdf
on page 4.

I have the square

\(\displaystyle (500,900)(599,900)(599,999)(500,999)\)

and the quad

\(\displaystyle (454,945)(558,951)(598,999)(499,999)\)

It looks like this

Bilinear transformation not working-capture.png

Where the ith entry of the quad and square coordinates above are corresponding corners.

With that in place, I can make the following matrices

\(\displaystyle \begin{bmatrix} 1 & 500 & 900 & 450000 \\ 1 & 599 & 900 & 539100 \\ 1 & 599 & 999 & 598401 \\ 1 & 500 & 999 & 499500 \end{bmatrix} \begin{bmatrix} a_0 \\ a_1 \\ a_2 \\ a_3 \end{bmatrix} = \begin{bmatrix} 454 \\ 558 \\ 598 \\ 499 \end{bmatrix}\)

\(\displaystyle \begin{bmatrix} 1 & 500 & 900 & 450000 \\ 1 & 599 & 900 & 539100 \\ 1 & 599 & 999 & 598401 \\ 1 & 500 & 999 & 499500 \end{bmatrix} \begin{bmatrix} b_0 \\ b_1 \\ b_2 \\ b_3 \end{bmatrix} = \begin{bmatrix} 945 \\ 951 \\ 999 \\ 999 \end{bmatrix}\)

If I solve for them I get

\(\displaystyle a0=-709.911845730028\)
\(\displaystyle a1=1.50964187327824\)
\(\displaystyle a2=0.709621467197225\)
\(\displaystyle a3=-0.000510152025303541\)
\(\displaystyle b0=148.305785123967\)
\(\displaystyle b1=0.611570247933884\)
\(\displaystyle b2=0.85154576063667\)
\(\displaystyle b3=-0.000612182430364249\)

But then when I run this python script (I want to simulate manually, converting the point (454, 945) on the quad to the point (500,900) on the square), I get a different answer. I get (442.90822654, 1024.0)...

Code:
    X = 454
    Y = 945
    
    a0=-709.911845730028
    a1=1.50964187327824
    a2=0.709621467197225
    a3=-0.000510152025303541
    
    
    b0=148.305785123967 
    b1=0.611570247933884 
    b2=0.85154576063667 
    b3=-0.000612182430364249
    
    A = b2*a3 - b3*a2
    C_one = (b0*a1 - b1*a0)
    C = C_one + (b1*X - a1*Y)
    B_one = (b0*a3 - b3*a0) + (b2*a1 - b1*a2)
    B = B_one + (b3*X - a3*Y)
    
    V = (-B + (B*B - 4*A*C)**0.5 ) / (2*A)
    U = (X - a0 - a2*V) / (a1 + a3*V)
    
    print U,V

Does anyone know what's wrong?

Thanks
 

Attachments

  • Capture.PNG
    Capture.PNG
    966 bytes · Views: 73
Last edited:
Physics news on Phys.org
  • #2
Hmm, interesting. The bilinear transformation code seems correct to me. I noticed, though, that there appears to be a .jpg file concerning your original matrices where you compute the $a_0, a_1,$ etc, that didn't load. Could you please give some background on how you constructed those matrices?

Incidentally, wanting to check your four "cardinal" points is a very good idea! Sanity checks in general are essential.

[EDIT] For reference, when I copy your code to Mathematica (really Wolfram Programming Cloud), I get the same result you do. So I don't think you're getting any weird sort of round-off or numerical error here.
 
  • #3
I added the missing image above. And the matrix is based off the pdf file on page 4.

X = a0 + a1U + a2V + a3UV
Y = b0 + b1U + b2V + b3UV

Where X, and Y is coordinates on the quad, and U,V and coordinates on the square. By substituting the 4 pairs of corresponding corner coordinates, I get 8 equations. But a and b coefficients are independent, so I can separate it into 2 systems of equations of 4 unknowns.
 
  • #4
I did a test and tried to warp an image and this seems to work in general, but then in two places the formula messed up. See these two pics.

The left eye got messed and the right leg got messed too. The above example quad is for the right leg.

original:
View attachment 4605

warped:
View attachment 4606

using this warp template:
View attachment 4607
 

Attachments

  • received_10155869851720133.jpeg
    received_10155869851720133.jpeg
    16.7 KB · Views: 74
  • received_10155869855760133.jpeg
    received_10155869855760133.jpeg
    17.6 KB · Views: 80
  • received_10155869853635133.jpeg
    received_10155869853635133.jpeg
    26.1 KB · Views: 67
  • #5
I was able to fix it, using three methods. The issue was numerical unstability in the quadratic function to calculate the y coordinate.

1. Use the much more stable quadratic formula
http://www.it.uom.gr/teaching/linearalgebra/NumericalRecipiesInC/c5-6.pdf

2. translate the square to location 0,0.

3. Scale the square into a unit square.

------

3. helped a bit, 2. not sure if it made a difference, and 1. was a huge boost in accuracy.
 
Last edited:
  • #6
sneaky said:
I was able to fix it, using three methods. The issue was numerical unstability in the quadratic function to calculate the y coordinate.

1. Use the much more stable quadratic formula
http://www.it.uom.gr/teaching/linearalgebra/NumericalRecipiesInC/c5-6.pdf

3. translate the square to location 0,0.

2. Scale the square into a unit square.

------

3. helped a bit, 2. not sure if it made a difference, and 1. was a huge boost in accuracy.

Excellent! I'm glad you found something that worked out, and thanks so much for posting it here for our edification. You learn something new every day. I had no idea that the regular quadratic can be numerically unstable. It makes sense given the explanation - subtraction is usually the culprit.
 

FAQ: Bilinear transformation not working

Why is my bilinear transformation not working?

There could be several reasons why your bilinear transformation is not working. Some common issues include incorrect input data, using incorrect transformation parameters, or a coding error in the transformation algorithm.

How can I fix my bilinear transformation?

To fix your bilinear transformation, you can start by double-checking your input data and ensuring that you are using the correct transformation parameters. You can also try debugging your code to identify any potential coding errors.

Can I use bilinear transformation on any type of data?

Bilinear transformation can be used on a variety of data types, including images, audio signals, and discrete-time signals. However, it is important to understand the limitations and assumptions of this transformation for your specific data type.

What are the advantages of using bilinear transformation?

Bilinear transformation has several advantages, including its ability to preserve the frequency response and stability of a system, its straightforward implementation, and its applicability to a wide range of data types.

Are there any alternatives to bilinear transformation?

Yes, there are several alternatives to bilinear transformation, such as the Z-transform, impulse invariant transformation, and matched Z-transform. Each of these transformations has its own advantages and limitations, and the choice of which one to use depends on the specific application and requirements.

Similar threads

Replies
1
Views
729
Replies
6
Views
2K
Replies
7
Views
1K
Replies
59
Views
5K
Replies
1
Views
3K
Replies
1
Views
2K
Back
Top