The image quality may be estimated using image gradients. To speed-up the autofocus procedure, the autofocus is done in two steps: the first run over the full range of the lenses movement with a significant step will provide the estimated position which will be improved in the second run over the small range with a small step. Interpolation between the few best positions may improve the results accuracy.