Focus Peaking - An Algorithm

Written by Paul Bourke
September 2024


Focus peaking is a technique utilised in many digital phones to assist with the choice of focus distance as well as depth of focus. In essence it highlights, normally using a user selectable colour, the outline of the regions of the image in focus. In image processing the technique is generally known as the Canny edge detector, dated around 1986 and named/credited to John F Canny.

This document outlines one possible process with the following image as an example. It may be necessary to view the high resolution version to notice that the decorative post is in focus while the background is slightly out of focus. In what follows it is assumed the reader is familiar with applying 2D filter kernels to an image to derive a second image, but the technique more commonly known as convolution.

Grey scale conversion

The process only requires a grey-scale image, while there are other choices that are more perceptually correct, the approach here is simply to use the average of the r,g,b values.

It is sometimes suggested to perform a histogram equalisation or the image but the author hasn't found it necessary.

Gaussian Blur

Since the image may contain pixel level noise which will interfere with the edge detection filters (see later), the grey-scale image is first smoothed with a Gaussian blur, a 5x5 filter is a common choice. There are a number of choices when forming this filter, one approach is to decide on the standard deviation σ in units of pixels and populate the 5x5 kernel directly using the equations for a unit volume Gaussian as follows, where x and y would range from -2.5 to 2.5.

A more accurate standard 5x5 Gaussian filter with σ=1 is as follows, total volume=273.

Another with a volume=159.

The resultant Gaussian smoothed image is shown below.

Edge Detection

The next stage is an edge detection, a Sobel filter (also known as the Sobel–Feldman operator) will be chosen in this exercise. There are two filters involved, one for estimating the gradient in the horizontal direction and one for the gradient in the vertical direction. The two filters Gx and Gy are given by

The two filtered images are given below, one can see that predominantly vertical structures are highlighted in the Gx image on the left, and predominantly horizontal structures are highlighted in the Gy image on the right.

Applying these two filters results in a vector in the direction of changing gradient. Of interest is the magnitude, that is, the highest change in gradient irrespective of direction, specifically

The Sobel magnitude is shown below.

Although the standard Sobel filter is adequate here, it is quite a crude approximation. Other filters have been proposed, such as the Scharr operators, Gx and Gy below.

Threshold

The final stage is the threshold the edge detection to form the edge that will be used to highlight the focus region.

Some implementations perform a thinning algorithm here in order to create single pixel wide edges. Since the focus peaking is only an "effect" to highlight the areas of focus, the author doesn't believe any additional edge processing is required.

Apply to original image

And finally, the threshold outline overlaid onto the original image, in this case in pink. As expected, the foreground object has been outlined while the background hasn't.