v9.1.1
Menu

Homogeneous Coordinates

mediumFREENotationGeometryCV: Introduction to Computer Vision

Implement a conversion between Cartesian coordinates and homogeneous coordinates, a fundamental concept in projective geometry. This conversion is crucial in computer vision for representing and applying projective transformations.

In Cartesian coordinates, a 2D point is represented as (x,y)(x, y). In contrast, homogeneous coordinates introduce an additional dimension, representing the same point as (x,y,1)(x, y, 1). This extension enables the use of linear algebra techniques, such as matrix multiplications, to perform projective transformations.

Here are the steps to convert between these coordinate systems:

  1. To convert from Cartesian to homogeneous, append a third coordinate with value 11.
  2. To convert from homogeneous to Cartesian, divide the first two coordinates by the third.
(x,y)→(x,y,1)(X,Y,W)→(XW,YW)\begin{aligned} (x, y) &\rightarrow (x, y, 1) \\ (X, Y, W) &\rightarrow \left(\frac{X}{W}, \frac{Y}{W}\right) \end{aligned}

This technique is widely used in image processing and computer vision applications.

Example

Input
to_cartesian([6, 8, 2])
Output
[3.0, 4.0]
Reasoning
  • Input: the homogeneous triple (X,Y,W)=(6,8,2)(X, Y, W) = (6, 8, 2). The first two entries are scaled coordinates and WW is the scale factor shared by both.
  • Math: recover the Cartesian point by dividing each of the first two entries by WW: x=62=3x = \frac{6}{2} = 3 and y=82=4y = \frac{8}{2} = 4.
  • Output: the 2D point [3.0, 4.0]. It has one fewer entry than the input because WW is used up by the division. The values are real numbers (written with a decimal part) because they come from a division, and each is rounded to 4 decimal places, which only matters when WW does not divide evenly.
  • Notice that scaling all three entries by the same nonzero factor names the same point: (12,16,4)(12, 16, 4) also gives (3,4)(3, 4). Going the other way, (3,4)(3, 4) simply becomes (3,4,1)(3, 4, 1), and dividing by W=1W = 1 returns the original point unchanged.

Constraints

  • For to_homogeneous: input is [x, y], output is [x, y, 1]
  • For to_cartesian: input is [X, Y, W], output is [X/W, Y/W] rounded to 4 decimals
  • W is guaranteed to be non-zero

Learn this in

More from CV: Introduction to Computer Vision

solution.py

Test results

Sign in to run your code against the test cases.