Interesting question! Determining the focus of a visual image is carried out in the visual association area of the brain. Ultimately, this process results in focusing of the retinal image by adjustment of the shape of the lens in the eye. Lens shaping to focus the image is called accommodation
The neuronal circuitry involved in accommodation includes the following structures:
The input to the accommodation response is provided by the retina, optic nerve, thalamus, and visual cortex. The visual cortex projects to the association cortex.
The (simplified) output scheme is the following: The association cortex projects to the supraoculomotor nuclei, which in turn generates motor control signals that initiate the accommodation response. The signal is then sent bilaterally to the oculomotor complex, and hence input from one eye is enough to focus both eyes.
The motor output regulates the ciliary muscles that control the shape of the crystalline lens. Negative accommodation adjusts the eye for long distances by relaxation of the ciliary muscles. Positive accommodation adjustment of the eye for short distances by contraction of the ciliary muscles Medical Dictionary.
As to the second part of your question: how out-of-focus images are functionally recognized:
There are at least three mechanisms responsible for accommodation:
1) Detection of image blur: if an image is blurred / out of focus the edges are not clearly defined. Retinal blur can be detected (edge detection) and assessed in the brain. Correction is then initiated as described above, and is basically done by trial and error. If adjustment results in a change of blur, the accommodation response (lens deformation) is detected as useful and accommodation is initiated. The accommodation itself is also based on trial-and-error: if blur increases the accommodation is reversed, if it decreases accommodation is increased in the same direction. (Philips & Stark, 1977). Image blur operates monocularly and is sufficient for accommodation to occur and is likely the most important mechanism for accommodation. It can occur without prior knowledge of distance, size etc.
Two more specific cases also result in accommodation, with likely similar neural circuitry involved:
2) Vergence / retinal disparity. This stimulus is generated when the eyes move both in opposite directions, i.e. when focus shifts toward objects closer or farther away. The temporary displacement on binocular retinal images is detected and leads to initiation of accommodation Fincham & Walton, 1977.
3). The near response: While 1) and 2) are retinotopic responses, the near-response is initiated by spatiotopic stimuli, including perceived depth and distance of objects, which are derived from cues such as relative size, overlap, perspective, shading and texture gradients as well as small timevarying dynamic depth cues which include motion
parallax, loom and motion in depth. Dynamic cues provide time-varying references for perception of relative depth whereas static cues allow simultaneous comparison
of separate absolute distances from which relative depth is perceived (Schor et al., 1991).