Pixel index coordinate system (for images) to cartesian coordinate system

1.3k Views Asked by At

I have some vectors plotted with respect to a coordinate system that has its origin at the top left at $(0, 0)$ and $x$ increases to the right, and $y$ increases downwards - standard in image processing. I want to put these vectors inside a cartesian coordinate system where $(0, 0)$ is the in center, and vectors to the left of it have a negative $x$, and vectors below it have a negative $y$, etc.

Currently, I'm using a rather "brute force" approach where I compute the image's center point as $(w/2, h/2)$ where $w$ and $h$ are the width and height of the image respectively. Then for a vector in the image, say $(x, y)$, I compute $(x' = |w/2 - x|, y'=|h/2 - y|)$ to get the $x$ and $y$ distance of the vector away from the origin. Then, I define the following rules.

If $x' < w/2$: negate $x$

If $y' > h/2$: negate $y$

and apply them to each of my "absolute value" vectors.

I'm not satisfied with this solution because I feel like there is a more elegant approach using linear algebra. However, I'm not sure how to frame this problem as a change of basis, because it seems as though the basis vectors for both coordinate systems are the same.

EDIT: I will probably go with my solution because I can run it in linear time, and I don't have immediate access to a matrix library in my development environment. However, I would still like to know the mathematical solution to this problem.

2

There are 2 best solutions below

2
On BEST ANSWER

$(x,y) \mapsto (x+\frac{w}{2}, \frac{h}{2}-y)$ should work if I understand you correctly. For example $(0,0) \mapsto (\frac{w}{2},\frac{h}{2})$

EDIT Corrected formula to agree with specs

4
On

All linear transformations in the linear algebra sense assume that the zero vectors agree. If you move your zero vector (translation) you have an affine transform. The link gives a way to use an augmented matrix to represent both actions simultaneously through matrix multiplication.