# Particle Swarm Optimisation in Practice: Multiple Applications in a Digital Microscope System
## 3 Accurate Objects' Height from Focus Measurement  
The conventional approach to determine the height of a generic object under inspection with the digital microscope is based on the fixed single thin lens optical system model. This features a linear relation between the distances \(d_{o}\) and \(d_{i}\) (from the object and the image to the lens, respectively), as formulated in Equation (3) where \(f\) is the focal length of the lens [31]. \[\frac{1}{f}=\frac{1}{d_{o}}+\frac{1}{d_{i}} \tag{3}\]  
This simple equation makes it easy to determine the distance from the camera to the object by simply moving the camera towards and away from the object and finding the best in focus image [32]. Note that in cameras in practical use, things become more complex. Thisis because they require multiple lenses and also due to the fact that properties of the lenses are generally confidential, and that stepper motors used to move the internal focus lens do not have a linear relationship with the focus distance from the camera. Hence, the 0-25 mm admissible range of our system was initially discretised in 26 checkpoints, by moving the camera with a 1 mm step, to generate a lookup table matching corresponding heights of the object and peak focus positions of the camera. The calibration jig for performing this process is shown in Figure 1. This is used to move the camera in the vertical direction along a linear guide rail whose lead screw is turned by the stepper motor. Each movement of the camera, whose height is recorded through 10 \(\upmu\)m resolution linear encoder, is equivalent to changing the height of the test sample. All communication from the PC software to the stepper motor occurs through RS232 ports. Using the lookup table to estimate the height of an object proved to be problematic for two main reasons. First, measuring focus is a noisy process, which often returns poor peak focus measurements. In turn, the latter leads to unreliable height values. Indeed, while moving the camera, a built-in function (set up in auto focus mode) is called to find the peak focus energy with the Laplacian operator method [33] to: determine the best focus position; read the focus motor position value; generate a table of 'focus value vs. object height' data; however, incorrectly returned position values negatively impact on the calibration process, thus introducing errors. We empirically confirmed this by noticing that the peak focus energy we measured clearly differed from the peak of the data when a polynomial is fitted to the data. Additionally, significant outliers in calibration data are likely to be present due to vibrations and changes in lighting conditions on the factory floor. Second, it requires re-calibrating the system accurately due to non-consistent optical/mechanical characteristics across cameras. This is achieved by returning the microscope back to the factory, where the previously stored 26 checkpoints are used to perform the camera re-calibration process. As the 26 points cannot always represent the ideal baseline, this approach is still prone to poor re-calibration outcomes. Obviously, having to ship the system for re-calibration adds costs and delays, and it is undesirable considering that these systems are expected to be in constant use. Hence, a built-in automatic calibration routine capable of mitigating the noisy component of the focus measurement would significantly and simultaneously improve performance, customer experience and value for money. Figure 1: Camera calibration jig. To overcome these problems, we present a self-calibrating approach where uncertainty is mitigated. Instead of using the 26 noisy measured peaks, we augmented the data-set to make it more robust and accurate, and we found the optimal coefficients for a polynomial fitting them, thus being able to generate the required lookup table with a smoother function and with any preferred number of points (this approach is known to reduce noise [34]).