Focus-based depth (Z) measurements are used extensively in industrial metrology and microscopy. Typically, a peak in
the focus figure-of-merit of a region is found while moving the lens towards or away from the surface, allowing local
recovery of depth. These focus-based measurements are susceptible to errors caused by: (1) Optical aberrations and
characteristics of the lens (astigmatism, field curvature); (2) Optical and image sensor misalignments; (3) Image sensor
shape errors. Depth measurements of the same artifact can therefore significantly vary depending on the prevailing
orientation of the surface texture (due to lens astigmatism) or on the specific position in the field of view. We present a
vision-based algorithm to reduce errors in focus-based depth measurements. The algorithm consists of two steps: <b>1.
Offline calibration</b>: We generate a calibration table for the optical system, consisting of a set of Z calibration curves for
different locations in the field of view. <b>2. Run-time correction</b>: During measurement, we determine the Z correction to
the focus position using the stored Z calibration curves and a measurement of the local orientation of the surface texture.
In our tests, the correction algorithm reduced the depth measurement errors by a factor of 2, on average, for a wide range
of surfaces and conditions.