# Module 4 Programming - Matlab

E. Wyatt Anderson December 14, 2007

1
1.1

Written Exercises
Exercises 1, 2 and 3

For the sake of simplicity in future use of the equations, I formulated the appropriate expressions while taking f into account at all steps. Using the properties of similar triangles, we can construct equations for the position of the virtual image of the point P = [XY Z]T on the virtual image plane, f away from the focal plane xy as follows (The z coordinate of the xl,r points on the virtual image plane is f , and θ is the angle of the inner triangles formed by the rays through the image plane):
xr = b − f tan θ = b − f tan arctan b−X Z = −b − f tan θ = b−f = −b − f tan arctan = −b − f yl = yr = f ∗( Y ) Z −b − X Z −b − X Z b−X Z

xl

Since we have two cameras, we can use the similar properties of similar triangles to create inverse expressions given xl , xr , yl , yr , f and b for the point P = [XY Z]T . We eﬀectively create equations for the lines running from the focal plane through the virtual image plane on the xz axis, then set them equal to each other to ﬁnd the intersection, which is the point P :
x = z y = = b(xl + xr ) 2b + xl − xr −f (x − xl ) −b − xl yl ∗ z f

+f

1

To test these equations, I ﬁrst generated MATLAB functions fproject and iproject for the forward and inverse projections of a point, respectively (these functions are included in respectively named .m ﬁles). For example:
>> P P = 200 5 6 >> [xl, xr, yl, yr] = fproject(P, 1, 1) xl = 32.5000 xr = 34.1667 yl = 0.8333 yr = 0.8333 >> iproject(xl, xr, yl, yr, 1, 1) x = 200.0000 z = 6.0000 y = 5.0000 ans = 200.0000 >>

2

Stereo Camera Simulation

To complete the stereo camera simulation, I translated the equations from the above notation in to MATLAB programs. The main driver function plotmodel, in ﬁle plotmodel.m, takes a minimum of 3 arguments and an optional total of 6 arguments, as follows. plotmodel() depends on move.m and fproject.m to function: The model argument is a multicolumn plotmodel(model, focal length, b, translate matrix, tilt degrees, pan degrees) matrix, with three rows (X , Y , and Z coordinates), with each column representing one point, as in the MyBox example, which we will use to generate the upcoming ﬁgures in this section. This function performs coordinate transformation, unit conversion, and rendering tasks, by taking the input model and then applying the appropriate X , Y , and Z translations from T the X Y Z translation matrix, then it converts the units from millimeters to pixels based on the speciﬁed CCD size, which in this case is equal to .5in. Once the points are all projected using calls to fproject in fproject.m, the points are all plotted, using the o marker for the ”right” camera and the + marker for the ”left” camera. The results of changing the length of f , as shown in Figure 1, are commensurate with what we would expect given a real camera. The results of panning, tilting, and translating the camera positions, as shown in Figure 2, are also what we would expect. To translate, we simply apply the translation amount to the point before we project it. To tilt and pan, 2

0 50 100 150 200 250 300 350 400 450 0 100 200 300 400 500 600

0 50 100 150 200 250 300 350 400 450 0 100 200 300 400 500 600

0 50 100 150 200 250 300 350 400 450 0 100 200 300 400 500 600

(a) f = 12mm

(b) f = 24mm

(c) f = 36mm

Figure 1: Plots of the camera view of MyBox at various lengths of f

0 50 100 150 200 250 300 350 400 450 0 100 200 300 400 500 600

0 50 100 150 200 250 300 350 400 450 0 100 200 300 400 500 600

0 50 100 150 200 250 300 350 400 450 0 100 200 300 400 500 600

(a) f = 24mm, 2◦ tilt
0 50 100 150 200 250 300 350 400 450 0 100 200 300 400

(b) f = 24mm, 4◦ pan
0 50 100 150 200 250 300 350 400 450
500 600

(c) f = 24mm, b = 20mm

0

100

200

300

400

500

600

(d) Translation of 500mm in the x (e) Translation of 500mm in the x direction direction and 500mm in the y direction

Figure 2: Various transformations of the camera position

3

we use the product of two transformation matrices deﬁned in move.m and apply them to the point P that we pass to fproject.

3

Control Simulation

I started to build the control simulation by building the functionality in the .m ﬁles Controller.m, Simulate.m, Camera.m and getconfig.m. Controller.m was destined to be the PID controller as per the assignment, Simulate.m was destined to be the driver for the control simulation, Camera.m is a functional representation of a camera that can return projected x, y points in pixels based on the conﬁguration data and a passed point in 3D space P , and getconfig.m is a simple function for storing conﬁguration variables and returning them based on a passed key. Unfortunately, I’ve been unable to get the simulation to a working state. I built up the appropriate functions to the point where Simulate() should plot a graph of the tilt error, but given any variation of the P gain, other variables, image size, or the algorithms, the results are incongruous with what I would expect. I’ve tried a multitude of combinations in all of my .m ﬁles to see if I could get the simulation to behave, but, alas, the clock has run out. I’m sure with more background in control theory, linear algebra, or MATLAB programming I could have formulated an appropriate solution to the problem, but with the skills I have now and what I’ve been able to pick up in this limited amount of time for this module, I’m currently unable to continue.

4