3

enter image description here

This trapezoid is a rectangular region looked at from a perspective. Since I don't have a way to transform the rectangle by rotation/translation transformations, I thought I could just create a linear space from the known 4 corners coordinates.

By linear space, I mean create a number of values that range from 0 to 1 linearly. So, a unidimensional linear space from 0 to 1 in 100 steps would look like [0, 0.01, 0.02, 0.03 ... 1]. In this case, I'd like to do this but in two dimensions in the trapezoid. If this were a rectangle it would be trivial, I'm not sure there is a way to do this for a trapezoid.

What is the "right" way to do this?

Thanks in advance

  • The question is not clear to me. What do you mean by "create a linear space"? – Paul Jan 05 '17 at 12:45
  • edited question – Pedro Batista Jan 05 '17 at 12:50
  • If those are the coordinates of the corners then it is a rectangle isn't it? It is just in perspective, as you say. – Paul Jan 05 '17 at 14:17
  • I posted a question on stackoverflow with some more detail. I guess this is more of a programming problem than math. I was just hoping there would be some generic rule to do this.

    http://stackoverflow.com/questions/41487380/obtain-two-dimensional-linear-space-on-trapezoid-shape-inside-image-frame

    – Pedro Batista Jan 05 '17 at 14:21
  • This seems exactly my problem. It seems I need to learn about projective interpolation

    http://math.stackexchange.com/questions/13404/mapping-irregular-quadrilateral-to-a-rectangle

    – Pedro Batista Jan 05 '17 at 16:42

1 Answers1

2

The “right” method depends on what it is that you’re ultimately trying to do. Two reasonable interpretations are that you want to map the unit square onto this trapezoid or that you’re going to make a uniform grid on the trapezoid by straight linear interpolation in both directions and then perhaps map that back to the unit square.

The barycentric method that you’ve found in this answer is a good technique. Another approach is to compute the planar perspective transformation that maps between the unit square and trapezoid and use that to map points. That’s a commonly-available graphic library function, but if you’re doing it yourself you can find descriptions of the process with a web search, and briefly described in this answer.

If the parallel sides of the trapezoid are parallel to the $x$-axis, this transformation matrix is fairly simple. Let the lower-left corner be at the origin, the bottom and top widths be $w_b$ and $w_t$, respectively, and the top left corner be at $(x_t,h)$. Then, if you work through the calculation in the second link, above, many of the terms vanish and you’re left with $$A=\pmatrix{w_b&{w_b\over w_t}x_t&0\\0&{w_b\over w_t}h&0\\0&{w_b-w_t\over w_t}&1}$$ for the matrix that maps the unit square onto the trapeziod, which translates into the mapping $$(x,y)\mapsto\left({w_b\over (1-y)w_t+yw_b}(w_tx+x_ty),{w_b\over(1-y)w_t+yw_b}hy\right).\tag{*}$$ This looks plausible: the denominator interpolates linearly between the top and bottom widths and depends only on $y$. The resulting $y$-coordinate doesn’t depend on $x$, either. For a fixed $y$, the $x$-coordinates in the trapezoid are offset proportionally to the transformed $y$-coordinate and are uniformly scaled by the width of the trapezoid at that height. You’ll end up with a grid that looks something like this:

enter image description here

Mapping to a trapezoid that has its lower-left corner at an arbitrary point $(x_b,y_b)$ is a matter of translating after performing the mapping of the previous paragraph, which in matrix terms amounts to filling in the last column of $A$ with those lower-left corner coordinates.

Going in the other direction, you can define a “uniform” grid on the trapezoid via $$\begin{align}u &= \left(x-\frac{x_t}hy\right)/w_y\\v &= y/h\end{align}$$ where $w_y$ is the width of the trapezoid at height $y$, i.e., $w_y=(1-y)w_b+yw_t$. This is similar to the previous grid, but this one is also evenly spaced in the $y$ direction. If you then need to map this back to a unit square for some reason, compute $A^{-1}$, which isn’t too hard given $A$’s relatively simple structure. For an arbitrarily-located trapezoid, simply translate first, i.e., subtract the coordinates of the lower-left corner from $x$ and $y$ before computing $u$ and $v$.

If the trapezoid isn’t aligned with a coordinate axis, then another way to tackle this is to translate and rotate so that it’s in “standard” position, as above, but I think computing the perspective transform matrix $A$ directly isn’t going to be any more work in that case.

amd
  • 53,693
  • 1
    Thank you for the detailed response. Unfortunately, for my case I can't assume that the bases are parallel to the x axis, they might not even be parallel to each other. I found this resource that I think details very well all the math involved in this operation.

    http://graphics.cs.cmu.edu/courses/15-463/2010_fall/Papers/proj.pdf

    I guess I just need to solve equation (1), since I have everything I need to calculate them. u, v are my pixel coordinates in my case, and the rest of the coefficients a,b,c.. can be derived from the coordinates of each corner.

    – Pedro Batista Jan 06 '17 at 10:32
  • If you could give your opinion about the link I provided on the comment I would be very much appreciated :) – Pedro Batista Jan 06 '17 at 10:39
  • Nop, I had it backwards :/ – Pedro Batista Jan 06 '17 at 11:04
  • @PedroBatista So, you don’t even have a trapezoid, but a general quadrilateral. That’s exactly what’s described in the second link that I cited. The paper that you found covers exactly the same material, just using slightly different notation. There’s no equation to solve, per se. Just plug your values in and compute the result. As I mentioned, though, unless you’ve got a burning desire to do this yourself, see if whatever graphics library you’re using will do this for you—look for “homography.” – amd Jan 06 '17 at 17:57
  • Yes, you are right. I didn't have the math terminology quite right in my question. The thing is that when you are not from an english speaking country, some times it is hard to use the correct scientific words and its confusing when its time to ask technical questions :)

    My application is not for graphics, its actually a coordinate space where I need to determine position of detected objects from camera. I just need the coordinates relative to the [0, 0] corner. This is just a test to check if the accuracy is better then when using coordinate space transformation matrices.

    – Pedro Batista Jan 06 '17 at 18:07
  • The algorithm is done, took me 1 hour :) Just needed to know how to interpret the math. – Pedro Batista Jan 06 '17 at 18:08
  • @PedroBatista You’re still using a coordinate transformation matrix, whether explicity or implicitly via the resulting coordinate conversion formulas. Any loss of precision is going to come from the way that the respective matrices are built—the more multiplications and divisions along the way, the worse it’ll get. – amd Jan 06 '17 at 18:28
  • True, but when I do a camera calibration with the chessboard I get to big of an error, maybe because of the algorithm that calculates the distortion factors and the camera matrix. This way I atleast guarantee that if object is detected on one of the corners it will be correct, any error will come in the inner coordinates, which was not the case with my previous approach. Then, I can refine the position with post processing. But i dont know why I am writting this since this has nothing to do with the problem. Cheers, and thanks, – Pedro Batista Jan 06 '17 at 20:40