# HOWTO: Distance calibration of the A620

Richard Lemieux, 2007-05-24

The purpose of distance calibration is to get correct distance numbers from the camera software. The intent is to be able to compute distance related numbers such as the depth of field and the optimal stereo separation distance. The process of calibration returns parameter values that are used in a program that displays distance related numbers such as program rf_3.c [2].

The A620 already provides a distance number. The distance number provided by Canon is used in the 'distance scale' shown in manual focus mode and while doing 'autofocus lock' (shutter half-press and down arrow). The 'Canon distance number' can also be found in the EXIF data (Look for FocusDistanceUpper if using Phil Harvey's Exif tool). The CHDK will display that number in the OSD/Misc window. The 'Canon distance number' returned by the A620 is an integer between 0 and 65535 (a 16-bit number) and is interpreted as a distance in millimeters. Both the CHDK and the EXIF viewer may scale that number and show the distance either in meters or centimeters.

The 'Canon distance number' appears to be pretty close to the real distance if the distance is measured from the back of the camera and is small (roughly less than half the hyperfocal distance). At larger distances the number returned by Canon is way off; the errors are more apparent at smaller focal lengths but are still there at the maximum zoom position.

Focusing is performed by moving a lens group within the lens. This is different from the movement of the lens occurring when using the zoom control. The movement of the lens group associated with focusing is done by a stepping motor and associated gears and is very precise. It is so precise that the camera can memorize the focus position and return to it days later if using the Custom mode.

Calibration is made possible by the use of a model relating the "Canon distance number" and the measured distance. Such a model is developed in [1]. The resulting model appears to be good enough for practical use. The model returns a formula that translates a 'Canon distance number' into an actual distance. The formula is dependent on three parameters. The purpose of calibration is to find out figures for those three parameters.

An open question is whether the parameter figures obtained by calibrating one A620 can be used for all other A620's. I conjecture that two of the parameters at least ('d0' and 'a1') are pretty much fixed, while the third parameter 'si' could be obtained by simply looking at the EXIF data of pictures of far away subjects.

In the following, I describe the procedure I used to calibrate my A620. Expect to be taking many measurements if you do this procedure.

## PRELIMINARY

It appears that the 'Canon distance number' (s) relates to the movement of the lens focusing mechanism (x) as follows [1],

x = a0 + a1/s

x "(mm) Movement of the lens focusing mechanism" s "(0-65535) Canon distance number as returned by the A620" a0 = -a1/si si "Value of the Canon distance number when the lens focuses at Infinity" a1 "Parameter estimated through the calibration procedure"

Basic optics tells us the subject distance (D) for any position of the lens focusing mechanism (x). When 'x=0', the CCD is at the focus point of the lens and the lens is focused at infinity 'D=Inf'.

D = f + f^2/x

f "Focal length of the lens (7.3mm to 29.2mm on the A620)"

What we are going to do is to measure the distance between the lens rim and the subject when the lens is at zoom position 0 (7.3mm). Call this measure 'Dm'. Since 'Dm' is not 'D', I need to fill in the difference with an additional parameter 'd0' that is going to be different for each focal length of the lens.

The formula we are going to calibrate is the following,

Dm - d0 = f + (f^2/a1)/(1/s - 1/si) (1)

Dm "(mm) Measured distance" s "(0-65535) Number found on the OSD/misc display of the CHDK" d0 "(mm) Parameter to estimate" a1 "(mm) Parameter to estimate" si "() Parameter to estimate"

The last point is that we need to make sure that the A620 actually focuses on the subject when we take measurements of 'Dm' and 's'. We are going to depend on the autofocus of the camera. How the autofocus of the A620 actually works is a bit obscure. It is likely a contrast based system. My assumption is that the autofocus will set the focus on the subject if the subject if very contrasty and surrounded by low contrast objects. Ideally the subject should be isolated and above ground. The subject should look contrasty from the camera position at all zoom settings.

## CALIBRATION

Calibration involves taking measurements and selecting the parameters that best fit the measurements. Doing it in reverse, I first look at how I will fit the parameters. I am going to need a plotting software and plot the following in log-log coordinates,

1. The measured data (Dm, s) 2. The value of Dm computed with formula (1) (Dm(s;d0,a1,si), s).

To plot the formula I need to select values of 'd0', 'a1', and 'si' to start with. It is safe to start with,

d0 = 0 Controls the low end of the plot (macro range). a1 = f^2 Controls the mid range of the plot (just a bit of control here). si = 2000 Controls the behavior at the largest distances (the most important parameter).

Then do the plot and evaluate how close the computed formula fits the data. Change the parameters and do it again until you are happy with the fit.

Now that you know what is needed, you are ready to start collecting the data. Just accumulate pairs of (Dm, s) -- the measured distance and the Canon distance number -- using the CHDK for all focal lengths and for distances going from the closest to at most twice the hyperfocal distance. You may include data for far away subjects if you wish but be critical of the value of the data (was the autofocus capable of doing its job unambiguously?). Expect much fluctuation on 's' as the distance increases. Keep the measured data; don't make averages.

The hyperfocal distance number is found in the depth-of-field calculator of the CHDK software.

That's it for the calibration.

## HOW TO USE THE RESULT

Here is a simple example. The program I load in my own A620 is listed in [2].

float f_a[] = {7.300,8.460,9.565,10.835,12.565,14.926,17.342,21.709,29.200}; // f_a Array of focal lengths. float d0[] = {-61.7, -61.7, -61.7, -86.4, -61.7, -61.7, -61.7, -61.7, -61.7}; float a1[] = {55.2, 77.8, 96, 114, 165, 233, 303, 495, 888}; float si[] = {1350, 1590, 1984, 3614, 4248, 8104, 10341, 11903, 21930};

// z Current zoom position. float f = f_a[z]; float gcav = GetCurrentAvValue(); float Av = pow(2.,(double)(gcav/(2.0f*96.0f))); // Aperture number (2.8, 5.6 ...) float x = a1[z]*(1.0f/s - 1.0f/si[z]); float Ar = 0.915E-3f*Av; // (mm) Radius of first ring of Airy figure (diffraction). float coc = 3E-3f; // (mm) Diameter of the circle of confusion float xH = Av*(Ar > coc ? Ar : coc); // Use the largest between Ar and coc. float H = f + f*f/xH; // Hyperfocal distance float D1 = d0[z] + f + (1.0f - xH/f)*f*f/(x + xH); // Close end of DOF. float D = d0[z] + f + f*f/x; // Subject distance. float D2 = d0[z] + f + (1.0f + xH/f)*f*f/(x - xH); // Far end of DOF.

## REFERENCES

[1] Richard Lemieux, A distance display for my Powershot A620.

[2] The program I use on my camera.

[3] en.wikipedia.org/wiki/Hyperfocal_distance

[4] en.wikipedia.org/wiki/Airy_disc

[5] en.wikipedia.org/wiki/Visible_spectrum