# Autofocus manual

## Autofocus setup

Micro-Manager needs to be told which autofocus device or plugin to use. To do so, click on the Autofocus Options button on the main window.

The following are autofocus plugins available in Micro-Manager:

## OughtaFocus

The OughtaFocus plugin uses Brent’s algorithm for finding a maximum of a function. The plugin acquires images at different z positions to iteratively search for maximimum image focus quality.

The plugin has a number of settings, available in the autofocus options menu:

• SearchRange_um: OughtaFocus will search over z range specified, centered at the current z position.
• Tolerance_um: Search will stop when the z position’s distance from maximum focus quality is estimated to be within the tolerance specified.
• CropFactor: The plugin will apply a camera ROI to the center of the field of view during autofocus, to reduce the time required to read out each image. A CropFactor of 1 means that the entire field will be used.
• Exposure specifies how long the plugin will expose each image during the autofocus routine.
• ShowImage determines whether images will be displayed in the Live Window during the autofocus routine.
• Maximize determines what property of the autofocusing image series will be used to evaluate the quality of focus. You can choose Mean, Standard Deviation, or Edges (a function that looks at the variance of the first derivative). Experiment with different methods to find the best one for your sample.
• Channel allows you to select a channel preset to be used for autofocus – if no Channel is selected, then the current hardware settings will be used.

## AutoFocus JAF(H&P) manual

Pakpoom (Tom) Substoontorn provided the following explanation of the various settings in the autofocus dialog:

Auto Focus Channel is the channel with which the autofocussing will be carried out. Currently, the plugin uses the configuration preset group ‘Channels’, and you will need to type in the exact name of the channel to be used.

StepNumber is the number of planes used in focusing above and below the current plane. For instance, if my current zdistance at the beginning of autofocus routine is 11um and I set the parameter as the following

         -1st stepsize: 1 micron
-1st stepNumber: 2 step
-2nd stepsize: 0.3 micron
-2nd stepNumber: 2 step


The program will take the image at 9,10, 11, 12, 13 um….Then, it finds the sharpest image. Suppose the sharpest image is taken at 12 um. Then, it take the image at 11.4, 11.7. 12, 12.3, 12.6 um. Again, it finds the sharpest of these image. Suppose the sharpest one is at 11.7 um. Then, the program set the microscope to 11.7 um as its best focus.

‘shapness’ of an image is computed by

         -applying 3x3 median filter to the image to remove noise
-convolute image with a filter [-2 -1 0; -1 0 1; 0 1 2 ]
-sum the square of each entry in the convoluted image.


Empirically, this ‘sharpness value’ is correspond to the sharpness I see by eyes (for ecoli). In other words, for the same image field, the sharpest image I see by eyes is the image with the maximum sharpness value. The plot of ‘sharpness’ against zdistance is a single bell-shape curve. i.e., the futher zdistance from the best focus zdistance, the lower ‘sharpness value’ is.

I was planning to use while loop to repeat the search for the focus (rather than just two rounds: coarse and fine). However, so far, these coarse-fine search seem to be enough.

Crop ratio

 -- tell the program what fraction of image will be used for computing sharpness.


Suppose my image is 1000x600 pixel and my crop ratio is 0.5, the program will use only the middle part of the image, size 500x300, to compute sharpness and will discard the rest. In the past, I sometime use small crop ratio (like 0.3) to make the program run faster (as it applies median filter and convolute smaller matrix). However, right now I use median filter and convolute functions implemented in imageJ, median filter and convolution is so fast that the rate limiting step of the program is no longer matrix operation, but the time of taking each snapshot.

Threshold: This value tells the program when to give up the search. Because during the best focus search the program take one snapshot and compute its sharpness at a time (and given that the sharpness-zdistance plot is a single bell-shape), the program will give up once it finds the highest sharpness. The program stop taking snapshot when the current sharpness is less than (the best image so far) X (1-Threshold) Consider the following example, the program starts taking coarse image at 9 um-13um, threshold is 0.5 at 9 um sharpNess = 36

  10 um                       59
11  um                      21--> the program stop coarse search here as 21< 59*0.5



I’m not quite confident about this threshold trick. sometimes the single bell-shape is not perfect. I would recommend setting high Threshold for now.