38. Representing Images and Geometry
38.1 Introduction
Before we dive into the material of this chapter, let’s start by questioning the way in which we have been representing images up to now. In most of the chapters, we have represented images as ordered arrays of pixel values, each pixel described by its grayscale value (or three arrays for color images). We will call this representation the ordered array.
However, an image can be represented in different ways, making explicit certain aspects of the information present on the input. What else could we do to represent an image? Another very different, but equivalent, representation is to encode an image as a collection of points, indicating its color and location explicitly. In this case, an image is represented as a set of pixels:
Representing images as sets of points is very common in geometry-based computer vision. We will use this representation extensively in the following chapters.
Although both previous representations might seem equivalent, the set representation makes it easy to deal with other image geometries where the points are not on a regular array.
That representation can also be extended by representing geometry in different ways such as using homogeneous coordinates, or positional encoding. We will discuss homogeneous coordinates in this chapter.
The previous two representations are not the only ways in which we can represent images. Another option is to represent an image as a continuous function whose input is a location and it output is an intensity, or a color, :
These three representations induce different ways of thinking about architectures to process the visual input. These representations are not opposed and can be used simultaneously.
The ordered array of pixels is the format that computers take as input when visualizing images. However, a set of pixels is the most common format when thinking about three-dimensional (3D) geometry and image formation. Therefore, it is always important to be familiar with how to transform any representation into an ordered array.
We will start by introducing homogeneous coordinates, an important tool that will simplify the formulation of perspective projection.
38.2 Homogeneous and Heteregenous Coordinates
Homogeneous coordinates represent a vector of length by a vector of length . The transformation rule from heterogeneous to homogeneous coordinates is simply to add an additional coordinate, 1, to the heterogeneous coordinates as shown here:
August Ferdinand Möbius, a German mathematician and astronomer, introduced homogeneous coordinates in 1827. He also cocreated the famous Möbius strip.
The homogeneous coordinates have the additional rule that all (non-zero) scalings of a homogeneous coordinate vector are equivalent. For example, to represent a 2D point, we have (transforming from heterogeneous to homogeneous coordinates),
Figure 38.1: Transformation rule between heterogeneous and homogeneous coordinates in 2D. All the points along the dotted line correspond to the same point in heterogeneous coordinates. Note that the points and live in different spaces.
It is important to mention that while you can add two points in heterogeneous coordinates, you can not do the same in homogeneous coordinates!
38.3 2D Image Transformations
One important operation in computer vision (and in computer graphics) is geometric transformations of shapes. Some of the common transformations we’ll want to represent include translation, rotation, scaling, and shearing (see Figure 38.7). These transformations can be written as affine transformations of the coordinate system. The mathematical description of these transformations becomes surprisingly simple when using homogeneous coordinates, and they will become the basis for more complex operations such as 3D perspective projection and camera calibration as we will see in later sections.
Figure 38.2: 2D geometric transformations. The final transformation is an arbitrary warping with a more complex deformation field.
We’ll explore the use of homogeneous coordinates for describing 2D geometric transformations first, then see how they can easily represent 3D perspective projection.
38.3.1 Translation
Consider a translation by a 2D vector, as shown in Figure 38.3. We’ll denote the coordinates after the transformation with a prime. In heterogeneous coordinates, we have
We can write this translation in homogeneous coordinates by the product:
What is interesting is that we have transformed an addition into a product by a matrix, , and a vector, , in homogeneous coordinates. Rewriting the equation above as:
For calculations in homogeneous coordinates, both matrices and vectors are only defined up to an arbitrary (non-zero) multiplicative scaling factor.
To cascade two translation operations and , in heterogeneous coordinates, we have
Figure 38.3: (left) Translation. (right) Composition of two consecutive translations.
In homogeneous coordinates, we cascade the corresponding translation matrices:
You can check that:
In summary, in homogeneous coordinates a translation becomes a product with a matrix, and chaining translations can be done by multiplying the matrices together. The benefits of using the homogeneous coordinates will become more obvious later.
38.3.2 Scaling
Scaling the -axis by and the -axis by , shown in Figure 38.4, yields the transformation matrix in homogeneous coordinates,
The scaling matrix is a diagonal matrix.
Figure 38.4: Non-uniform or anisotropic scaling. In this example the -axis is compressed and the -axis is expanded.
Uniform scaling is obtained when , otherwise the scaling is nonuniform or anisotropic. After uniform scaling, all of the angles are preserved. In all cases, parallel lines remain parallel. Areas are scaled by the determinant of the scaling matrix.
38.3.3 Rotation
For a rotation by an angle , Figure 38.5, we simply have the matrix in homogeneous coordinates:
Figure 38.5: Rotation by an angle counterclockwise around the origin.
As in the case of the translation, a rotation in homogeneous coordinates is a product
The determinant of a rotation matrix is 1 and the matrix is orthogonal, that is the transpose is equal to the inverse: . The inverse is also a rotation matrix. The distance between any point and the origin does not change after a rotation.
In heterogeneous coordinates, the transformation can also be written in the same way but using only the upper-left matrix. Representing rotations in homogeneous coordinates has no benefit with respect to heterogeneous coordinates. But the advantage is that now both rotation and translation are written in the same way! They are both products of a matrix times a vector, so that they can be combined as we will discuss in Section 38.3.5.
If the angle of rotation is very small, then we can approximate the rotation matrix by its Taylor development:
for small , and
For small angles a rotation becomes a shear, which we will discuss next. In general, for all angles, a rotation can be written as two shears and a scaling.
Rotations in 3D become more complex as there are multiple possible parametrizations.
38.3.4 Shearing
Shearing involves scale factors in the off-diagonal matrix locations, as shown in the matrix, :
Figure 38.6 shows examples with an horizontal shear (), a vertical shear (), and an arbitrary shear.
Figure 38.6: Three examples of shear transformation.
In the example of the horizontal shear, the points are displaced along horizontal lines by displacement proportional to the coordinate, .
In a shear, lines are mapped to lines, parallel lines remain parallel, (non-zero) angles between lines change.
38.3.5 Chaining Transformations
As we have seen, homogeneous coordinates allow using all the four different transformations as matrix multiplications. We can now build complex transformations by combining these four transformations:
In heterogeneous coordinates the translation will have to be modeled as an addition breaking the homogeneity of this equation. The transformations described in this section are summarized in Figure 38.7.
Figure 38.7: Summary of 2D geometric transformations and their formulation in homogeneous coordinates.
As matrix multiplication is noncommutative, the order in which operations are performed is important (i.e., it is not the same to rotate with respect to the origin and then translate, as it is to translate and then rotate). All of the geometric transformations we have described are relative to the origin. If you want to rotate an image around an arbitrary central location, then you need to first translate to put that location at the origin, then rotate and then translate back.
Chaining transformations in such a way is a very important tool in computer graphics and we will also use it extensively as we dive deeper into geometry.
When chaining rotations and translations only we will have a euclidean transformation (lengths and angles between lines are preserved). A similarity transform is the result of chaining a rotation, translation and scaling (with uniform scaling, ). In this case angles are preserved but not lengths. Chaining all transformations results in an affine transformation. Each set of transformations forms a group.
38.3.6 Generic 2D Transformations
In general, chaining translations, scalings, rotations and shears will result in a generic matrix with the form:
As the last row of the transformation is , one could be tempted to drop it and go directly from homogeneous to heterogeneous, using the top matrix, but this will only work if the input vector has a 1 in the third component.
What happens if we have 9 degrees of freedom?
38.3.7 Geometric Transformations as Convolutions
In Chapter 15 we showed how certain geometric transformations can be written as convolutions (such as the translation) while others cannot (such as rotations, scalings, shears, etc.). However, things change when adding geometry explicitly to the image representation!
Once geometry is added to the representation, all of the transformations we discussed before can be implemented as one-dimensional (1D) convolutions over the locations as shown in the diagram in Figure 38.8.
Figure 38.8: Geometric transformation as a convolution. The input and output signals are represented as pixel sets with explicit geometry. The convolution kernels are and , corresponding to one dimensional convolutions as they only mix input features within the same input vector. The output intensity values are the same as the input.
In the diagram (Figure 38.8), each input element is a vector where , are the pixel location, and is the pixel intensity at that location. The convolution kernels are: and . The weights are the same for all inputs. The output is also represented using position explicitly: .
However, note that to perform a convolution on the output intensity will require translating the position encoding back into an image on a rectangular grid. For instance, after a rotation, the locations for the intensity values will change and the pixels will not lie on a rectangular grid anymore. The convolution kernels for the intensity channel will have to be transformed too.
38.4 Lines and Planes in Homogeneous Coordinates
One interesting application of homogeneous coordinates is to use it to describe lines and planes and perform operations with them. In 2D, the equation of a line is , which can be written in homogeneous coordinates as
In homogeneous coordinates, the equation of a line is the dot product:
This representation of the line is in homogeneous coordinates because it is scale invariant. This is, is the same line as . Therefore, it is also useful to describe the equation of the line with where is the normal to the line and is the distance to the origin.
Using homogeneous coordinates makes obtaining geometric properties of points and lines very easy. Given two points and in homogeneous coordinates, the line that passes by both points is the cross product (Figure 38.9):
Following a similar argument, you can show that given two lines and the intersection point between them is the cross product (Figure 38.9):
Figure 38.9: Using homogeneous coordinates to get the line that passes by two points, and to obtain the intersection of two lines. Both operations are analogous.
If three 2D points are colinear, then the determinant of the matrix formed by concatenating the three vectors, in homogeneous coordinates, as columns is equal to zero: . If three lines intersect in the same point we have a similar relationship: .
It is also interesting to point out that a 3D vector can be interpreted as a 2D line or as a 2D point in homogeneous coordinates.
We can also do something analogous to represent planes in 3D. The equation of a 3D plane is , which can be written in homogeneous coordinates as:
Representing 3D lines with homogeneous coordinates is not that easy and the reader can consult other sources [1] to learn more about representing geometric objects in homogeneous coordinates.
38.5 Image Warping
Now that we have seen how to describe simple geometric transformations to pixel coordinates, we need to go back to the representation of the image as samples on a regular grid. This will require applying image interpolation.
The first algorithm that usually comes to mind when transforming an image is to take every pixel in the original image represented as , apply the transformation, , to the coordinates, and record the pixel color into the resulting coordinates in the target image (Figure 38.10). As coordinates might result in non-integer values, we can simply round the result to the closest pixel coordinate (i.e., nearest neighbor interpolation as we discussed in Section 21.4.1). This algorithm is called forward mapping. It is an intuitive way of warping an image but it is really not a good idea. We will have all sorts of artifacts such as missing values and aliasing as shown in Figure 38.10 and Figure 38.11.
Figure 38.10: Comparison between forward and backward mapping using nearest neighbor interpolation. Forward mapping will produce missing values.
The best approach is to use what is called backward mapping which consists of looping over all the pixels of the target image and applying the inverse geometric transform, ; we then use interpolation (as described in Section 21.4.1 in chapter Chapter 21) to get the correct color value (Figure 38.10). This process guarantees that there will be no missing values (unless the coordinates go outside the frame of the input image) and there will be no aliasing if the interpolation is done correctly. To avoid aliasing, blurring of the input image might be necessary if the density of pixels in the target image is lower than in the input image. Figures Figure 38.10 and Figure 38.11 compare forward and backward mapping.
Figure 38.11: Comparison between forward and backward mapping. Forward mapping produces many artifacts. In both cases we use nearest neighbor interpolation.
To achieve high image quality warping it is important to choose a high quality interpolation filter such as bilinear, bicubic, or Lanczos. MIP-mapping [2] is another popular technique for high quality and efficient interpolation. MIP-mapping relies on a multiscale image pyramid to efficiently compute the best neighborhood structure needed to perform the interpolation at each location, which can be very useful when warping an image onto a curved surface.
Image warping can be applied to arbitrary geometric transformations and not just the ones described in this section.
38.6 Implicit Image Representations
An image is an array of values, , where each value is indexed as when take only on discrete values. We can say that interpolation is a way of transforming the discrete image into a continuous signal: .
An implicit image representation via a function trained to reproduce the image pixels is a function such that,
38.6.1 Interpolation
In the case of nearest neighbors or bilinear interpolation, the parameters of the interpolation function is the input image itself. For example, using a functional form, nearest neighbors interpolation can be written as:
What is really interesting about thinking about interpolation in this way is that we can now extend the space of possible functions to include other functional forms. For instance, this function could be implemented by a neural network that will take as input the two image coordinate values and and will output the intensity value at that location. The training set for the neural network is the image itself, and it will consist of the input-output pairs (i.e., location as input and intensities/colors as output). During training the neural network will memorize the image. The training will contain only values at discrete positions but in test time we can use any continuous input values. For this formulation to work, the neural network should be able to generalize to non-integer coordinate values, that is, it should be able to interpolate between samples.
38.6.2 Image Warping with Implicit Representations
Once the neural network, , has learned to reproduce the image, we can reconstruct the original image or apply transformations to it. Image warping is then implemented by simply applying the inverse geometric transformation to the discrete coordinates of the output grid and use the functional representation of the image to get the interpolated values:
The example in Figure 38.12 (b) shows an image encoded by a sinusoidal representation network (SIREN) [3] and then reconstructed with a rotation of 45 degrees and a scaling by 0.5 along both dimensions.
(a)
(b)
Figure 38.12: Figure (left) Original image encoded by a SIREN network. (right) Image after a rotation of 45 degrees and a scaling by 0.5 along both dimensions.
From this result we can make a few observations. First, we can see that, due to the transformation, there is aliasing in the sampled image (aliasing is most visible in the leg of the tripod). One way of avoiding aliasing would be by sampling the output on a finer grid and then blurring and downsampling the result to the desired image size. The second observation is that the way the boundary is extended is not like any of the methods that we studied in chapter Chapter 15; instead the image is padded by some form of noise that smoothly extends the image without adding strong image derivatives.
We can also study the reverse problem where we have two images (one is a transformed version of the other) and the goal is to identify the transformation . This problem is called image alignment.
The spatial transformer network [4] is an example of using parametric image transformations inside a neural network. Such transformation can be helpful during learning to align the training examples into a canonical pose.
38.7 Concluding Remarks
Homogeneous coordinates are extensively used in computer vision and computer graphics. They allow simplifying the computation of geometric image transformations. Therefore, many libraries in vision and graphics assume that the user is knowledgeable about the different coordinate systems.
One of the most important uses is in the formulation of perspective projection. We will devote the next chapter to describing the image formation process and camera models using homogeneous coordinates.
Representing images as collections of pixels with an explicit representation of geometry has a long history and is at the center of many modern methods for 3D image representation.
[1]
R. Hartley, A. Zisserman, Multiple view geometry in computer vision., 2nd ed., Cambridge University Press, Cambridge, UK, 2004.
[2]
L. Williams, Pyramidal parametrics., in: Siggraph, 1983: pp. 1–11.
[3]
V. Sitzmann, J.N.P. Martel, A.W. Bergman, D.B. Lindell, G. Wetzstein, Implicit neural representations with periodic activation functions., in: Nips, 2020.
[4]
M. Jaderberg, K. Simonyan, A. Zisserman, koray kavukcuoglu, Spatial transformer networks., in: Nips, 2015: pp. 2017–2025.












