3

I have a series of 2D points $q_i$. I also have a convex quadrilateral defined by $P =(p_1,p_2,p_3,p_4)$. I would like to find a way to map these points into the coordinate space defined by $P$ such that new $\hat{q}_i$ would be in the range $\{0,1\}$ where $\{0,0\}$ is the bottom left corner of the quad, $\{0,1\}$ is the top-left corner, $\{1,1\}$ is the top-right corner, and such.

$q_i$ are assumed to lie within $P$, and $P$ is assumed to be relatively oriented with the x-y axes so that we can unambiguously state "top-left corner of $P$". In fact, we can assume $p_i$ represent, in order, $\{0,0\},\{0,1\},\{1,0\},\{1,1\}$

Hopefully this question is clear enough, it's my first time asking here so please let me know if I need to clarify anything.

Hal T
  • 131
  • Are the $q_i$ constrained in some way? Do you have a constraint on orientation? (For instance, if the $q_i$ were already constrained to be in the $[0,1]\times[0,1]$ square, there are $|S_4|$ ways to map it congtruently to itself and half of those "flip the square over", reversing the orientation. $S_4$ is the symmetric group of the square.) – Eric Towers Dec 26 '17 at 15:39
  • $q_i$ are assumed to lie within $P$, and $P$ is assumed to be relatively oriented with the x-y axes so that we can unambiguously state "top-left corner of $P$". In fact, we can assume $p_i$ represent, in order, ${0,0},{0,1},{1,0},{1,1}$ – Hal T Dec 26 '17 at 15:42
  • Is $P$ guaranteed to be a parallelogram? The answer you accepted only applies to them, not to a more general quadrilateral. – amd Dec 26 '17 at 21:07
  • Indeed, I noticed that it doesn't work if it's not a parallelogram. – Hal T Dec 27 '17 at 18:27

2 Answers2

1

Let $(a,c)=p_3 - p_1, (b,d)=p_2-p_1$, and $\Delta = ad-bc$. If $\Delta \neq 0$ (meaning that the vectors $p_3 - p_1$ and $p_2-p_1$ are not parallel) this problem has a solution.

Let $(x_i,y_i) = q_i - p_1$. Then $q_i$ maps to $\frac{1}{\Delta}(d x_i - b y_i, a y_i - c x_i)$.


Where did that come from?

We're told that $p_1$ maps to $(0,0)$, so we should translate everyone by $p_1$ to get the origins to match.

We are told that $p_2 - p_1$ maps to a unit displacement along the $y$-axis and that $p_3 - p_1$ maps to a unit displacement along the $x$-axis. Consider $M = \begin{pmatrix} a & b \\ c & d \end{pmatrix}$. This $M$ takes the vector $\begin{pmatrix}1 \\ 0\end{pmatrix}$, a unit displacement along the $x$-axis to $\begin{pmatrix}a \\ c\end{pmatrix}$ and takes $\begin{pmatrix}0 \\ 1\end{pmatrix}$, a unit displacement along the $y$-axis, to $\begin{pmatrix}b \\ d\end{pmatrix}$. So if $(a,c)=p_3 - p_1$ and $(b,d)=p_2-p_1$, this matrix takes the unit square to the (translated to the origin) parallogram $P - p_1$.

We want to go the other way, so we want $M^{-1}$. First we find the discriminant of $M$, which is $ad-bc$. If this is non-zero, $M$ can be inverted. (This is equivalent to $\begin{pmatrix}a \\ c\end{pmatrix}$ and $\begin{pmatrix}b \\ d\end{pmatrix}$ not being parallel. The inverse is $M^{-1} = \begin{pmatrix} d/\Delta & -b/\Delta \\ -c/\Delta & a/\Delta \end{pmatrix}$.

Then we write each $q_i - p_1$ in coordinates, $(x_i, y_i)$, and apply $M^{-1}$ to it get the image point $\frac{1}{\Delta}(d x_i - b y_i, a y_i - c x_i)$.

Eric Towers
  • 67,037
1

You’re basically looking for ways to map a convex planar quadrilateral onto the unit square. There are, of course, many such maps, so you’ll need to decide what other properties you’d like yours to have to select one. One fairly simple to implement choice is a planar perspective transformation a.k.a. planar homography. One compelling reason to use this mapping is that a function for constructing this map is available in many common computer graphics APIs, so you might not have to do any of the work yourself.

A general method for constructing this transformation for an arbitrary pair of convex quads is given in this answer. Assuming that the quad isn’t degenerate—no three points are colinear— and taking $p_1$, $p_2$ and $p_3$ as the basis points, the matrix $A$ is then $P\operatorname{diag}(P^{-1}p_4)$, where $$P = \begin{bmatrix}p_1&p_2&p_3\\1&1&1\end{bmatrix}.$$ (The homogeneous coordinates of $p_4$ are used in the above formula.) A similar calculation for the unit square produces $$B = \begin{bmatrix}0&1&0\\0&0&1\\-1&1&1\end{bmatrix}.$$ The perspective map is then given by the matrix $$M = BA^{-1} = B \operatorname{diag}(P^{-1}p_4)^{-1}P^{-1}.$$ The expression following $B$ represents dividing the rows of $P^{-1}$ by the scale factors obtained from solving $P(\lambda,\mu,\tau)^T=p_4$, so you only have to perform a single matrix multiplication. Moreover, left-multiplication by $B$ corresponds to shifting the rows of the other matrix upward and replacing the last row with the sum of the second and third, minus the first.

This matrix is unlikely to have a row of ones along the bottom, so the third component of the resulting homogeneous coordinates will usually not be equal to $1$ and you’ll need to convert back into inhomogeneous Cartesian coordinates by dividing through by this value.

Another relatively simple map to implement is based on bilinear interpolation, as suggested by Eric Towers. The idea here is to draw a grid on the quad such that the grid spacing is uniform along each edge. The point $(\xi,\eta)$ on the unit square then corresponds to the intersection of the lines (again using homogeneous coordinates) $\mathbf l_\xi = ((1-\xi)p_1+\xi p_2)\times((1-\xi)p_3+\xi p_4)$ and $\mathbf l_\eta=((1-\eta)p_1+\eta p_3)\times((1-\eta)p_2+\eta p_4)$. So, mapping from a point $p = \mathbf l_\xi \times \mathbf l_\eta$ on the quad to the unit square is a matter of finding $\xi$ and $\eta$ such that $\mathbf l_\xi\cdot p = \mathbf l_\eta\cdot p = 0$. This map has a more uniform-looking grid than the perspective map, but the inverse map from the unit square to the quad involves terms in $\xi\eta$, so computing this mapping from the quad to the unit square will entail computing a square roots.

amd
  • 53,693