← All news

Software Autofocus, Explained: How Your Microscope Finds Focus From the Image

// Imaging Fundamentals

Software autofocus, explained simply

How the software finds your sharpest plane using the picture itself. No sensor, no focus motor. In plain language.

// TL;DR
  1. Software autofocus moves the stage, scores each frame for sharpness, and keeps the sharpest. No extra sensor.
  2. Sharpness is one number. Crisp edges score high, blur scores low. Use contrast by default.
  3. Plot the scores and you get a curve. A clear peak is real focus. A flat curve means nothing to focus on, so hold position.
  4. Search coarse then fine. Big steps to find the neighborhood, small steps to nail the address.
  5. Every step costs an exposure. Coarse plus fine might be 16 shots; fine over the whole range, 40. Fewer shots, less bleaching.

01 What it is

Software autofocus lets the computer find focus on its own. No focus motor. No extra sensor. It judges the picture itself and keeps the sharpest plane.

The loop is simple. It moves the stage to a height, takes a frame, and rates how sharp that frame is. Then it does the same thing at the next height. Move, capture, score, repeat.

After it has tried several heights, it keeps the one with the best score. You do not pick the height. You give it a range, and it finds the sharpest plane inside that range on its own.

Hardware autofocus is the other kind. It uses a separate sensor that watches the coverslip and corrects in real time. It is fast and it holds focus well during a time-lapse. But it needs that extra sensor, and not every scope has one. This article is about the software kind, which works from the image you are already taking.

Pros
  • No extra sensor or hardware needed
  • Works on any system that can take a frame
  • Any camera, any objective, any sample
  • Reads the image directly, no calibration beam
Cons
  • Every focus step costs an exposure
  • Adds bleaching to fluorescence samples
  • Can fail on empty or low-structure fields
  • Slower than hardware at holding focus in a time-lapse

02 Measuring sharpness

Everything depends on one number. The focus score answers a single question. How sharp is this frame?

In focus, edges are crisp and neighboring pixels differ a lot. A dark pixel sits next to a bright one. Out of focus, everything blends together and those differences shrink. Sharp frames score high. Blurry frames score low.

There are three common ways to turn a frame into that number.

Contrast. Edge differences. The safe default, and it works for most samples.

Intensity. The brightest pixels. Good for a few bright beads on a dark field, where there are not many edges but there is a clear signal.

Variance. The spread of brightness across the frame.

Start with contrast. Switch to intensity when your sample is sparse and bright.

Starlyte one-page cheat sheet: software autofocus, focus scoring, the focus curve, coarse and fine search, and the light budget
// The full one-page cheat sheet. Click to download the PDF.

03 The focus curve

Score every height in your range and plot the scores against position. You get a focus curve. It climbs to a peak, then falls. The top of that hill is best focus.

Reading the curve tells you whether to trust the result. A real peak stands out from the flat parts around it. That means there is genuine structure in the field to lock onto.

A bad curve is jittery and flat. No clear peak, just noise wobbling around the same level. That usually means there is nothing in the field to focus on. An empty region. A blank patch of dish. A channel with no signal.

This is where software autofocus most often fails, and the fix is counterintuitive. When the curve is flat, do not move at all. Better to hold position than chase a peak that is not there. One clear peak is the goal. No peak means stay put.

04 Coarse then fine

Checking every micron over a wide range is slow. Forty microns at one-micron steps is forty frames every time autofocus runs. Multiply that across a tiled, multi-point, time-lapse run and you spend the session focusing instead of imaging.

So the search runs in two passes.

The first pass is coarse. Big steps over the full range to find the rough neighborhood of the peak.

The second pass is fine. Small steps in a narrow window around the winner to nail the exact height.

Coarse finds the neighborhood. Fine finds the exact address. Same answer as a brute-force scan, a fraction of the frames.

05 Every step costs light

Here is the part people forget, and it matters more than speed. Every step in the search is one more exposure. For fluorescence, every exposure is more bleaching. The routine is not free. It spends signal from the exact sample you are trying to preserve.

Put numbers on it. A coarse plus fine search might cost around 16 shots. A fine search at the same small step size across the whole range might cost 40 for the same answer. That is more than double the light, every time autofocus runs, with nothing to show for it but a dimmer sample.

Fewer shots means less light on the sample. That is the real reason two passes win. Not just faster. Gentler on the thing you care about.

06 How to set it up well

Short version.

Set a sensible search range. Wide enough to contain focus on a bad day, narrow enough that you are not scanning empty space.

Pick contrast unless your sample is sparse and bright, then use intensity.

Use coarse then fine, not one fine pass over the whole range. Same accuracy, far less light.

Trust a clear peak. Ignore a flat one. If the curve is noise, hold position.

And treat every focus frame as a frame off your light budget. Spend them like they cost something, because they do.

None of this needs a software background. It needs someone willing to lay out what the autofocus is actually doing instead of leaving it as a black box. If the software lands on the wrong plane, that is the software’s job to handle, not yours to babysit.
// Free Cheat Sheet

The one-page version, free

Everything above, plus the focus loop diagram, the in-focus vs out-of-focus picture, the focus curve, the coarse-then-fine search, and the light budget, on one page you can keep next to the scope. No email required.

// FREE CHEAT SHEET
Software autofocus. One page by the scope.