Pixels to Coordinates

2.5k Views Asked by At

all I am trying to convert pixels to x,y,z tuples. I have an image that is 200x300x3 and I would like to convert this into x,y,z tuples to implement within Unity.

I have a file that gives positions for the pixels based on y, x, down, and right. This gives the min and the max of the desired cube. Then when another set of pixels is applied it should theoretically map out a full cub.

I did find this article: How do I map pixels to a coordinate system? However I am confused still about finding the relationship for what I want to employ.

If anyone could educate me on this that would be great thanks.

1

There are 1 best solutions below

1
On BEST ANSWER

Pixel2xyCoordinates

Before starting to calculate, you have to choose appropriate values for $x_{\text{max}}$ and $y_{\text{max}}$ of the $x-y$ grid, in which you are going to replicate the given rasterized image.

First calculate the two scale factors $s_x$ and $s_y$ as shown below. These two scale factors will help us to fit all the pixels of the image inside the $x-y$ grid of your choice, which has its origin (0, 0) and ($x_{\text{max}}$, $y_{\text{max}}$) at the bottom-left corner and the top-right corner of the screen respectively. $$s_x = \dfrac{ x_{\text{max}}-0}{200-0}=\dfrac{ x_{\text{max}}}{200}$$ $$s_y = \dfrac{ y_{\text{max}}-0}{300-0}=\dfrac{ y_{\text{max}}}{300}$$

Now, consider a pixel, which has its horizontal pixel index $\pmb{a}$ and vertical pixel index $\pmb{b}$. Since both the horizontal pixel index and the $x$-coordinates increases from left to right, the $x$-coordinate corresponding to horizontal pixel index $a$ of the considered pixel ($a$, $b$) is given by, $$x = s_x a = \dfrac{ x_{\text{max}}}{200}a.$$

In contrast to the aforementioned relationship between the horizontal pixel index and the $x$-coordinates, the vertical pixel index and the $y$-coordinates increase in different directions. Therefore, we need to use the following conversion formula to obtain the $y$-coordinate corresponding to the vertical pixel index $b$ of the considered pixel ($a$, $b$). $$y = s_y (300 – b )= \dfrac{ y_{\text{max}}}{300}(300 – b).$$