Biological specimens are rife with optical inhomogeneities that seriously degrade imaging performance under all but the most ideal conditions. Measuring and then correcting for these inhomogeneities is the province of adaptive optics. Here we introduce an approach to adaptive optics in microscopy wherein the rear pupil of an objective lens is segmented into subregions, and light is directed individually to each subregion to measure, by image shift, the deflection faced by each group of rays as they emerge from the objective and travel through the specimen toward the focus. Applying our method to two-photon microscopy, we could recover near-diffraction-limited performance from a variety of biological and nonbiological samples exhibiting aberrations large or small and smoothly varying or abruptly changing. In particular, results from fixed mouse cortical slices illustrate our ability to improve signal and resolution to depths of 400 microm.