How could I convert coordinates from and 3D world to pixels on a top down image of that world

1.6k Views Asked by At

I have a list of coordinates that represent an objects location in a world. I'd like to display these objects as sprites on a top-down 2d map of that world. The image is 600x600.

I need a way to convert from world coordinates to a pixel on the image that would represent their location.

I have a few point and their correlating image locations that I found manually.

World Coordiantes:

  1. (72, -64)
  2. (-40, 63)
  3. (72, -51)

And their respective image pixels:

  1. (528, 27)
  2. (37, 468)
  3. (480, 32)
2

There are 2 best solutions below

0
On

You can do this using a projective transformation. In homogeneous coordinates, let a world point be denoted by $\mathbf{q} = [x,y,1]^T$ and let an image point be denoted by $\mathbf{p} = [u,v,1]^T$.

The transformation from a world point to an image point can be computed as

$$\mathbf{p} = P \mathbf{q} = K[R|t]\mathbf{q}$$

where $K$ transforms the world point to the image point and $R$ and $T$ rotates and translates the world point before projecting on the images (e.g., if the world is not aligned with the image).

The matrix $K$ is given by $$ K = \begin{bmatrix} sx& 0& cx\\ 0& sy& cy\\ 0& 0& 1 \end{bmatrix},$$ where the parameters $s_x$ nad $s_y$ define the scaling between image points and world points (e.g., pixels per unit) and the parameters $c_x$ and $c_y$ define the offset from the center of the image.

The matrix $[R|t]$ is given by $$ [R|t] = \begin{bmatrix} \cos \theta& -\sin \theta & t_x\\ \sin \theta& \cos \theta & t_y\\ 0& 0& 1 \end{bmatrix}. $$ where $\theta$ is the rotation between the image and world frame and $t$ is the translation between the image and world.

You need to define $s_x, s_y, c_x, c_y, \theta, t_x,$ and $t_y$ based on your problem. Note that if the world frame and image frame are already aligned (or if you don't want to rotate or translate the world before projecting on the image), then $[R|t] = [I|0]$ and $\mathbf{p} = K \mathbf{q}$.

Hope this helps you get started.

0
On

Well, you'll need to carry out a dilation (not necessarily an increase in size). For e.g. if you use a dilation/scaling factor $k$, a point $(x, y)$ in your world will be the point $(kx, ky)$ on your minimap. I don't see anything like that going on in the coordinates you provided (there is no consistent k) and if that's the case, sorry to say, no (simple) formula you can use.

If you use a formula (one scaling factor $k$ for all points in your world), your minimap will be more accurate and thus more useful.

Supposing your world is $100 \times 100$ then, the point $(0, 0)$ in the world has to be the pixel $(0, 0)$ on your minimap and the point $(100, 00)$ in the world has to be the pixel $(600, 600)$ That's a $k = 6$.

Coordinates World Coordinates Minimap
$k = 6$
$(72, -64)$ $(432, -384)$
$(-40, 63)$ $(-240, 378)$
$(72, -51)$ $(432, -306)$