This invention discloses an
autofocus method, apparatus, electronic device, and storage medium, belonging to the field of
autofocus technology. The
autofocus method includes: adjusting the
focal length within a first focusing range according to a first preset step size, acquiring a first image captured by the camera at each
focal length, and determining an effective focusing area and a second focusing range based on the first image; adjusting the
focal length within a second focusing range according to a second preset step size, acquiring a second image captured by the camera at each focal length, and determining the optimal focusing position based on the sharpness evaluation value of the effective focusing area in the second image; wherein the first preset step size is greater than the second preset step size. This invention performs focusing in two stages: the first stage uses a large step size to filter the effective focusing area and narrow the focusing range, and the second stage uses a small step size for focusing and uses fewer but more effective areas for focusing, thus achieving higher operating efficiency.