1. Home
  2. Explore
  3. Engineering
  4. Camera Matrix in Computer Vision

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Camera Matrix in Computer Vision

Transcript

Hello,in this video we're going to look at the camera matrix, which is a core concept in computer vision. The camera matrix tells us how a point in world space gets mapped onto an image. And we're going to start very simple and gradually make our way to the full camera matrix.So let's get started.We want to find the camera matrix for real cameras, like for example this one, or that one. But those cameras have large apertures and lots of lenses which makes them very complicated to analyse.

So instead of modelling a real camera we use a simplification called the pinhole camera. An ideal pinhole camera has an infinitesimally small aperture, it's just described as a point. And as a consequence, each point on the image plane, the sensor, is reached by light from only one single direction. If the aperture was larger, like here, then light from many directions would reach a point on the sensor. And that would make the image blurry. Real cameras have larger apertures and they use lenses to prevent or reduce this blurriness. But lenses introduce errors, for example radial distortion or tangential distortion. And those errors are not captured by the pinhole model.

So if we use the pinhole model for real cameras then we make errors. But some of those errors, like radial distortion, can be corrected at some later point. And others, like tangential distortion, are so small that we can usually ignore them, if we use a good camera and lens. So the pinhole model is a very useful model because it's simple and reasonably accurate.So let's start with our world coordinate system. What coordinate system you choose depends on your application. Often it looks like this, where the x- and y- axes form the ground plane and the z-axis points up. However, when working with cameras it is customary to use a coordinate system like this one. Here the sensor is aligned with the x- and y-axes and the camera looks along the positive z-axis.

In some applications, for example in computer graphics, the camera usually looks into the negative z-axis. It's just a different convention. What we'll talk about is still valid there but if you use that, or any other, convention, then you'll have to change a few signs in the formulas. The z-axis also has other names, such as optical axis or principal axis. So for the moment let's forget about the world coordinate system and just consider the coordinate system of our camera.Let's place the aperture of our pinhole camera at the origin of the camera coordinate system. The image plane, which corresponds to our sensor, is behind the aperture and aligned with the x- and y-axes. The optical axis, the z-axis, goes through the centre of the image plane.

Let's assume the sensor has width W and height H. Throughout this video capital letters refer to variables whose units are millimeters, whereas small letters will generally refer to variables whose units are pixels. Ok, we'll now look at the setup from the top so we can see the x- and z-axes. But what we'll do here will be equivalent for the y- and z-axes that we would see if we were looking at the setup from the side. Also, we'll switch to an orthographic projection now.So, the distance between the image plane and the aperture is called the focal length. I've abbreviated the focal length here with a capital F, which tells us that it's unit is millimeters.

The angle between the extreme light rays that can be captured is called the angle of view. In this case we're looking at the x-axis so we're dealing with the horizontal angle of view.Now let's assume that we want to take a picture of the point that's makes up the corner of this little cube here. Here's the scene as the camera sees it. Let's call it's x-coordinate capital X and it's Z-coordinate capital Z. And for now we assume that these coordinates are given in the coordinate system of the camera. We will deal with coordinates given in the world coordinate system later.So a light ray will go from that point, through the aperture onto our sensor. The first thing we notice is that the point on the image is on the other side of the aperture, so it has a negative X-coordinate.

Let's call that point -U. By adding the minus, U is actually positive in this specific case, so it has the same sign as X. That's not something that we have to do but it makes the formulas a little nicer. Ok, now we can do a little geometry. We're going to use the rule of similar triangles, which in this case means that -U over minus the focal length is equal to X over Z .. and this equation is the basis for the intrinsic camera matrix.Now let's look at a different pinhole camera with twice the focal length.

In this case the angle of view has changed and we're getting a different image. For example, here is what the camera sees now. However, if we also double the size of the image plane then we get back to the same image. The image plane is larger but we have the same angle-of-view as before, so we capture the same scene. And right now we're not yet dealing with a sensor that has a discrete number of pixels. For now this is a continuous image plane, so the image that's captured by these two pinhole cameras is the same.So from these two examples we can see that there are many, infinitely many, different pinhole cameras that give the same image. And we can include that in our formula by adding a factor k to both the numerator and the denominator on the left-hand-side.

That also tells us that the absolute values of the focal length and image plane don't really matter for this formula, only their relative values matter. So we can freely choose k. And since, when we deal with pictures, we usually work with pixels, it's convenient to choose k such that the distances are expressed in pixels.So let's use the letter (small) w for the width of the image plane, or sensor, in pixels.

And then we can set k to small w over capital W, so to the sensor width in pixels divided by the sensor width in millimeters. This factor tells us the number of pixels per millimeter. And then the formula becomes this. The value at the top left is now the image coordinate of our point in pixels. And the value on the bottom is the focal length expressed in pixels.Now let's consider the situation where our sensor is not perfectly aligned with the optical axis.

So for example our sensor is offset in the x-direction by u0. And I'm assuming that this value is already given in pixels. If we have it in millimeters then we can use a similar formula as before to get the value in pixels.So in this case, this sensor offset in the positive x-direction would make the image coordinate more negative, so -u has become more negative by u0. So we have to add u0 back in to obtain the correct value .. so our formula becomes this.

Now let's rearrange this formula a little bit so we end up with this equation. And if we do the same for the Y direction then we get this one here. Here we use v for the image coordinate along the y-axis. And then we can rewrite these two equations in matrix form as shown here.Ok, so this matrix already looks similar to the intrinsic camera matrix. There are still two differences though. First, in many texts you see an entry here. That entry refers to the shear of the image plane. So to something like this. To my knowledge that's not really a thing for an ideal pinhole camera.

The second difference is that our matrix only has two rows whereas usually there are three rows. And that's because so far we've dealt with Euclidean space so we've used Cartesian coordinates. To see why there are usually three rows let's briefly have a look at homogeneous coordinates.We won't go very deeply into this and we won't do it in a formal way, we'll just cover enough for our purpose here. So let's look at a two-dimensional Euclidean plane, with x- and y-axes.

This is the origin and this is some arbitrary point. In this specific case it's at coordinates [3, 2] but it could be anywhere on the plane. The coordinates I'm showing here are Cartesian coordinates and they allow us to describe any point on the plane, as long as it's not infinitely far away. So for example, if we consider a line going from the origin through this point and extending to infinity, then we can write the coordinates of every point on that line, except for the one at infinity. If we use homogeneous coordinates then we can represent that point, and any other point at infinity.

And homogeneous coordinates can do that because they use one more dimension than Euclidean coordinates. So our point [3, 2] now becomes (3, 2, 1). Why is there a 1 and not some other number? Well, by definition, if we have Cartesian coordinates [x, y] then the corresponding homogeneous coordinates are (xS, yS, S). In our case we've just set S to 1. But of course S could also be 2 or 3 or any other number, except for zero.

All of these coordinates refer to the same Cartesian point. So homogeneous coordinates are not unique. And that's one characteristic that we'll use in our discussion about the camera matrix. If we have a set of homogeneous coordinates then we can multiply them by some non-zero scalar without changing the point they refer to. Because multiplying all of them by the same non-zero value just changes the value of S. If you want to recover the Cartesian coordinates then you just divide the first two entries by the third entry.So how can homogeneous coordinates represent values at infinity. Well, consider the line we saw before. We can get any coordinate on that line by changing the value of the third coordinate. For example, if we make it 1/2 then we get this point.

If we make it 1/4 then we get this point. So as the third component goes to zero we move ever closer to infinity. And the point (3, 2, 0) is defined as the point at infinity that corresponds to the direction of this line.Why do we need that? I think that's actually very interesting .. but it's not important for this video. So if you'd like to know more about it then let me know in the comments so I know if it's worth making a video about it.There is another very useful aspect of homogeneous coordinates that we will use. If you want to rotate and translate a point in Cartesian coordinates then you would multiply the coordinates by a rotation matrix and then add a translation vector to the result. That's two separate operations. In homogeneous coordinates we can do both of these operations in one matrix multiplication.

We just have to create a matrix whose top left entries describe the rotation and last column describes the translation. And now both rotation and translation can be done in one operation, and they can also be inverted in a single operation, which makes things easier.You'll notice that there is this tilde rather than an equal sign between the two sides of the formula. And that means that the two expressions are equivalent .. but they are not the same because the homogeneous coordinates have three entries may be scaled differently.So far we've talked about this in two dimensions but homogeneous coordinates work in the same way in higher dimensions. For example, a set of 3-dimensional Cartesian coordinates [X, Y, Z] could be represented like this.

Ok, now that we know what homogeneous coordinates are, let's convert the equation from before from Cartesian to Homogeneous coordinates. On the right hand side we just add a line containing a 1. The equation still has to hold so on the left-hand-side we have to add a 1/Z .. or if we write this differently then we get this equation ... The matrix on the left is the usual intrinsic camera matrix ... On the right there's a homogeneous vector and based on what we talked about before we know that this one is equivalent to that one, where s is some arbitrary, non-zero constant.Ok, so far we've assumed that the coordinates that we're looking at, these X Y Z coordinates, are already given in the coordinate system of the camera. But generally, the coordinates of objects are given in the world coordinate system, not in the camera coordinate system.

So before we can apply the intrinsic camera matrix we have to transform the object's coordinates from the world coordinate system to the camera coordinate system. So let's have a look at how to do that. We have two coordinate systems, the world coordinate system and the camera coordinate system. And our objective is to find the world-to-camera matrix, so the matrix that takes coordinates in the world coordinate system and maps them to the corresponding coordinates in the camera coordinate system.Let's look at a simple example. We're in two dimensions and there are two coordinate systems.

Let's call the one on the left the world-coordinate system and the one on the right the camera coordinate system. In the world coordinate system we've got a point at location x=1. So let's start with the world coordinate system and rotate it counter-clockwise by 90°, so it's aligned with the camera coordinate system. We've only moved the coordinate system, not the actual point. Because the position of the point in the world hasn't changed. You know, the point doesn't care about our choice of coordinate system. But since we moved the coordinate system, the coordinates of our point have changed. Now let's adjust our view of the system.

I'm rotating this now but I'm not changing the coordinate system or the point, I'm just changing our view of it. As if we were turning our head sideways. Now we can more easily compare the system before and after rotating it. And here we can see that the point has rotated clockwise. So rotating our coordinate system counter-clockwise has the same effect as rotating the coordinates of the point clockwise, by the same amount. Clockwise and counter-clockwise are the inverse of each other.And that's a general observation, transforming a coordinate system in one way is the same as transforming the coordinates of objects in the inverse way. Or said differently, assuming we have a transformation matrix that tells us how the coordinate system changes, it's inverse tells us how the coordinates of objects change.So let's get back to our main example. Usually, what we know in a situation like this is the

world-coordinates of the camera and of the objects that we want to capture. The camera represents a coordinate system .. and the world-coordinates of the camera tell us how to get from the world coordinate system to the camera coordinate system.In this specific example, we start at the world coordinate system and to get to the camera coordinate system we rotate it by approximately -35° around the world z-axis, by 30° around the world y-axis and by 144° around the world x-axis .. and then we move it by -16mm along the world x-axis, by +16mm along the world y-axis and by +20mm along the world z-axis.

I've been very explicit about mentioning that these transformations happen around the world axes, because there are different ways of expressing rotations so you have to be specific about which one you're talking about.Ok, if we plug these rotation angles into the standard Euler rotation matrices then we get this rotation matrix. And our translation vector looks like this.And as we saw before when we talked about homogeneous coordinates, we can combine those two into one transformation matrix.So this is the transformation matrix that describes how the world coordinate system gets mapped to the camera coordinate system. And based on what we saw before we know the inverse is true for the coordinates of objects.

So this matrix takes the coordinates of an object in the camera-coordinate system and maps them to the object's coordinates in the world-coordinate system. So this is the camera-to-world matrix. But we need the world-to-camera matrix. And luckily the world-to-camera matrix is just the inverse of the camera-to-world matrix. And there is an easy formula available to compute the inverse for these kinds of transformation matrices.So after inverting the camera-to-world matrix we get this world-to-camera matrix .. and if we apply it to the world-coordinates of our point then we do get the point's camera-coordinates.You'll notice that the last entry of the homogeneous coordinates is one ..

so there's no rescaling required. That's because our world coordinates are just Cartesian coordinates that we put into the homogeneous shape so we can rotate and translate them more easily. So in this specific case we can simplify the equation by just dropping the last line.So to summarise, if you have the world-coordinates and rotation of your camera then put them into a matrix and invert it to get the world-to-camera matrix.And what I've been calling world-to-camera matrix is also called extrinsic camera matrix.Now we can combine the two equations that we've developed in this video. On the left we've got our equation for the intrinsic camera matrix, on the right the equation for the extrinsic camera matrix.

And if we combine them then we get what's called the camera matrix.And that's it. That's our camera matrix. In a future video we may have a look at different methods of finding the camera matrix for a particular camera and lens.So thanks for watching. I'd love to hear what you think about this approach of presenting the camera matrix. And if you liked the video then please give it a like, subscribe, leave a comment, tell your friends or anybody else.And see you soon.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent