# Precision Auto-Focusing Apparatus Based on Long-Working-Distance, Wide-Spectrum Microscope
## 2 Materials and Methods
### Auto-Focusing Algorithm  
In focusing methods based on automatic image processing, a series of images with different focusing states can be obtained by combining the high performance imaging system mentioned above with displacement driving control; each image sequence is evaluated and normalized, and then, the evaluation function curve is calculated by interpolation. The focusing algorithm for digital image processing is divided into two principal parts--selecting the focusing evaluation function and fitting the curve to determine the focusing position [15]. In the focusing process, the defocus state is judged by the change in sharpness produced by the defocusing of the image. The current frame image and the next frame image are evaluated, and the defocusing direction and defocusing amount of the target are judged based on a comparison of the analysis results for the different frames. Then, the relative position between the measured part and the optical system is changed until the optimal focal position is achieved. During the process of focusing, the main consideration is whether the boundary and details of the image are clear in the spatial domain, i.e., whether the high frequency components of the image are rich in the frequency domain. Meanwhile, due to the low gray value and gray gradient of the image, defocusing means that there is less information to represent the image sharpness in the image. Complex environmental factors, such as air-flow jitter, low light, detector noise, etc., will make the optical system used for image collection more vulnerable to the effects of noise. Therefore, when using automatic focus detection technology based on image processing, the image sharpness evaluation function must have a reasonable anti-noise interference capacity while maintaining focus detection with high precision. In order to suppress the effect of noise on the focusing result, we choose a median filter to realize image preprocessing before focusing. Median filtering will be very useful for eliminating the interference of outliers and line segments, especially for impulse noise. It can also protect the boundary contour information while eliminating noise. In this work, the position of the focal plane is determined by four focusing evaluation functions--the wavelet algorithm, Laplace algorithm, gradient square algorithm and an improved gradient square algorithm. The output of an ideal focusing algorithm is defined as having a maximum value at the best focused image position and decreasing as defocus increases. It is well known that the image quality of the edge area of microscopic imaging is usually worse than that of the central area. In our focusing calculation, instead of using the whole image for calculation, we select the central area of 70% of the whole image as the calculation area. This is because the image quality of the imaging system is reduced due to the larger aberrations in the edge area; meanwhile, the vignetting effect caused by pupil transition in the two-stage magnification system may also be an important reason for this result, where the size of the central area of image is \(M\times N\), the gray value of the center pixel is \(I(x,y)\), and where \(1\leq x\leq M\) and \(1\leq y\leq N\). Figure 4: Microscopic measurement integrated device. The Laplacian algorithm applies convolution masks to an image to calculate the pixel \(I(x,y)\) and the 4-neighborhood of the pixel [16], which is expressed as follows:  
\[L=\sum_{i=2}^{M-1}\sum_{j=2}^{N-1}\abs{4I(x,y)-I(x,y-1)-I(i-1,j)-I(i+1,j)-I(i,j+1)} \tag{1}\]  
The wavelet algorithm applies both high-pass (\(H\)) and low-pass (\(L\)) filtering to the image [17].