The human eye is very sensitive to fuzziness and can hardly bear seeing blurred images. This situation is still worse for X-ray microanalysis as a defect in focussing the electron beam can induce errors in intensities (in reference 1, a focus deviation of 10 micrometers causes an error of about 5% due to the modification of the Bragg angle). While a human operator can easily modify the Z stage position depending on the optical image seen on the monitor, several hours-long acquisition sessions require an automated way of reproducing the operator behavior.
The basic principle is to use the optical signal delivered by the camera installed on the probe at different Z values, extracting for each step a simple characteristic in the image. The characteristic chosen is the standard deviation of the grey levels, which is easily computed from the histogram moments of order 1 and 2, and should be at its peak value (lowest value on Transmission Electron Microprobes) on the focussed image.