How to compute uv coordinates for an arbitrary quad?

5 views (last 30 days)
Hi
For a regular grid, it is quite straightforward to get the uv coordinates for any given pixel in an image. However, for an arbitrary quad with vertices located at A,B,C,D, while A is the lower left vertex with uv coordinates (0,0) and C is the upper right vertex with uv coordinates (1,1). For any pixel inside the quad, how to compute the uv coordinates of the particular pixel? I found that a simple bilinear interpolation may not directly work.
Thank you very much!

Answers (1)

Julius Muschaweck
Julius Muschaweck on 27 Jul 2022
Edited: Julius Muschaweck on 27 Jul 2022
With A == [Ax, Ay], etc, and a point P == [Px,Py] for certain u,v, the first step is indeed to subtract A from B, C, D and P, effectively placing A at the origin, i.e, Ax == Ay == 0, just like you do.
Then, you obtain P from u and v as
Px == u*(1 - v)*Bx + (1 - u)*v*Cx + u*v*Dx;
Py == u*(1 - v)*By + (1 - u)*v*Cy + u*v*Dy;
in standard degree 1 Bezier patch notation. The last term includes u*v, which complicates solving for u and v; the solution is indeed nonlinear in Px and Py.
You get
syms Bx By Cx Cy Dx Dy Px Py u v
[solu, solv] = solve([u*(1 - v)*Bx + (1 - u)*v*Cx + u*v*Dx - Px == 0,
u*(1 - v)*By + (1 - u)*v*Cy + u*v*Dy - Py == 0], [u, v])
solu = 
solv = 
which you can simplify using the 2D cross product, if you want, like
cross2D = @(AA,BB) AA(1)*BB(2) - AA(2)*BB(1);
syms B C D P
B = [Bx,By];
C = [Cx,Cy];
D = [Dx,Dy];
P = [Px,Py];
% and so on
and then e.g.
syms sigma_2
sigma_2 = 2 * cross2D(C,D-B)
sigma_2 = 
Such cross product are abundant in the solution.
You can then use these results to write a general function uv_from_P(A, B, C, D, P)

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by