Decision Logic

With some understanding as to how to extract a focus measure from an image, we can apply the measure to determine the "best" guess for an object distance.
The simplest method would simply to take pictures at small intervals and "remember" which object distance had the greatest focus measure. This method, however, fails to take advantage of the unimodal distribution of focus measure graphs. As a result, the camera would have to take many pictures and perform many calculations, leading to a slow and inefficient process.
Instead, we adopted a method informally known as "staying on top of the mountain." We start the camera with a certain object distance and acquire an image and calculate the focus measure. We move the camera forward a certain object distance increment and re-acquire the image and focus measure. If the second measure exceeded the first, we continue in the same direction with the same increment. If the first measure were greater than the second, we reverse the direction and halve the increment. We repeat the process until the differences in the focus measures between two consecutive images is less than an user-specified threshold.

This decision logic is illustrated in color. First, we start at some small object distance (first red line) and increase the object distance until the focus measure decreases (other red lines). When the focus measure decreases, we decrease the focus measure with a smaller increment (shown by the blue lines). We again reverse the direction when the focus measure decreases (at the leftmost blue line). Finally, we hone in on the most focused image shown by the green line.