Modeling Cameras in MATLAB


Let’s demonstrate some of these principles in MATLAB. I’m going to create a point in the world and it’s going to be at coordinate 0.2 meters in the X direction 0.3 meters in the Y direction and it’s going to be 5 meters in Z direction. Now Z is the direction along the axis of a camera we call the optical axis of the camera. This is a relatively small object five meters away from the camera and a world point is always a column vector so there it is.

Now I’m going to create a camera object in fact a central camera object which does the particular projection that we just learned about and it’s got a focal length of eight millimetres.

So here we created the workspace now a central camera projection object. This object has got quite a lot of methods and we’ll have a look at some of those. But first thing I’m going to do is to use this camera object and I’m going to project my world point capital P to the image plane, which you see here that we have got some very small numbers.

These numbers are less than half a millimetre. We've taken our view of the world and we've made it much, much smaller and projected it onto the image plane. This is where the piece of film would be or this is where the digital camera chip would be. In this projection there’s no notion of pixels we've just taken image in the world and it’s described in terms of size and its distance in units or meters. We've projected it to the image plane and this is talking about the size of the image also in units or meters; a very small number of meters.

Now let’s turn this into a digital camera let’s introduce the notion of pixels. We’re going to create a new camera object. Basically recycle this line and we will tell it that the pixels are ten microns on a side so the pixels are square ten microns wide ten microns high. And their image plane has got a resolution of one thousand and twenty four square. This is a mega pixel camera.

Now if we look at the parameters of our camera object we see here we can see the focal length. Here we can see the size of pixels, the number of pixels, 1024 x 1024 exactly what we entered on the command line. And this parameter the principal point which is where the optical axis pierces the image plane is smack in the middle at coordinate 512, 512.

Now that I have my digital camera object, we can re do that projection that we had before project to world point P and this now is the coordinate of that point in units of pixels. Another method of the camera object is to actually plot these on a graphical image plane and here we can see where that world point has been projected to on the digital cameras image plane. So now the methods we can look at the cameras intrinsic parameters. Now this is the intrinsic parameter matrix; this is the overall camera projection matrix. We can pass an additional option into the project function.

First argument again is our world point and now I’m going to shift the camera a little bit. Now I’m going to tell it to shift the camera 0.1 meters in the X direction given the camera’s coordinate frame that’s a shift of 0.1 meters to the right. Not going to move it in the Y or in the Z directions so now what we see is that the projection is different. And we would expect that, we see that the U coordinate has been reduced from 544 to 528 and that’s what we would expect if we move the camera to the right we would expect the image plane coordinate to move a bit to the left.

Instead of translating the camera we could rotate the camera so we can rotate the camera around the Y axis. Now the Y axis points downwards so if I do this rotation of 0.1 radians around the Y axis that’s essentially rotating the camera towards the right and if we do that here’s the projection again and as we would expect if I rotate the camera a bit towards the right the pixel in the image will move a bit towards the left on the image plane.
Let’s consider a more complex example, one where there are multiple points in the world. To create multiple points in a grid pattern now I’m going to use the toolbox function mkgrid and I want a 2x2 grid and the points spaced on 0.2 meters centres and I want the centre of the grid to be at a coordinate 1, 2, 0. So this is a grid in the XY plane, the centre of the grid is offset 1 meter in the X direction and 2 meters in the Y direction and all the points have got a Z coordinate of zero.

So here are the set of points that I have just created in order to view these four points which lie on a ground plane I’m going to create a new camera. It’s a central projection model camera again. I’m going to set default camera parameters so this is default values for focal length image plane size and pixel size. I’m going to explicitly give the pose of the camera so our four points are lying on the ground plane so in order for my camera to be able to see them it needs to be somewhere up high looking down so the first thing that I’m going to do is to place the camera 4 meters above the ground. So this is a translation of 4 meters along the positive Z axis and then I’m going to rotate it around the Z axis by Pi/3 radians and then I’m going to rotate the camera around the Y axis so it points down at the ground.

So just rotate it around the Y axis by pi radians. Bare with me I’ll work this example out and these numbers should give something like the right result.

First thing we’re going to do is to project those 4 world points onto the camera’s image plane and here we have the coordinates of those points. So they clearly lay within the field of view and we can actually have a look at those four points. And we can plot those and that’s what the image plane looks like. So they’re just in the edge of the field of view of our camera, which is looking down.

Now let’s try and get a 3D understanding of what’s going on here so I’m going to create a new figure. I’m going to call it figure two and another method on the camera object we just created is called plot camera. What that does it to render it’s a 3D model of a camera into a graphic and the other thing that I’m going to do is to add to these graphic the 4 world points given my capital P I’m going to render them as the little spheres with a radius of 0.5 meters each so created this little scene, turn the grid on to make it a bit easier to see.

If I turn on my 3D rotation tool I can spin this around. Here we can see what it looks like, here are the four points on the ground plane now then I created using the grid function. And up the top we can see the camera and we can see the axis of the camera. Camera is a little bit small. We can zoom in and have a look at the camera there is it, there we can see its x, y and z axis, zoom in a little bit more perhaps and we can see there, it looks a little bit like a camera. It’s a box with a cylinder on the front. These are just the few examples of the many things that you can do using the camera classes within the toolbox.


There is no code in this lesson.

The linear algebra approach we’ve discussed is very well suited to MATLAB implementation. Let’s look at some toolbox functions that can simulate what cameras do.

If you are using a more recent version of MVTB, ie. MVTB 4.x then please change
>> cam.project(PW ‘Tcam’, transl(0.1, 0, 0))


>> cam.project(PW ‘pose’, transl(0.1, 0, 0)).

Professor Peter Corke

Professor of Robotic Vision at QUT and Director of the Australian Centre for Robotic Vision (ACRV). Peter is also a Fellow of the IEEE, a senior Fellow of the Higher Education Academy, and on the editorial board of several robotics research journals.

Skill level

This content assumes an understanding of high school level mathematics; for example, trigonometry, algebra, calculus, physics (optics) and experience with MATLAB command line and programming, for example workspace, variables, arrays, types, functions and classes.

More information...

Rate this lesson



  1. moha says:

    Hell, I’ve tried the above code using the version 4.3 of the toolbox and MATLAb 2018a. When I rotate the world point P using the following statement:
    cam = CentralCamera(‘focal’, 0.008, ‘pixel’, 10e-6, ‘resolution’, 1024);
    cam.project(PW ‘Tcam’, transl(0.1, 0, 0)).
    The result I get is as follows: [544 560] instead of [528 560] That is the projected point didn’t change position.

    I would appreciate any help resolving this issue.

    Thank you.

    Best regards,

    1. Peter Corke says:

      This is better handled on the Toolbox forum at tiny.cc/rvcforum. In short, the video is made using an older version of the toolbox. For MVTB4.x please change ‘Tcam’ to ‘pose’.

Leave a comment