Adaptive optics is a promising technique for the improvement of microscopy in tissues. A large palette of indirect and direct wavefront sensing methods has been proposed for in vivo imaging in experimental animal models. Application of most of these methods to complex samples suffers from either intrinsic and/or practical difficulties. Here we show a theoretically optimized wavefront correction method for inhomogeneously labeled biological samples. We demonstrate its performance at a depth of 200 μm in brain tissue within a sparsely labeled region such as the pyramidal cell layer of the hippocampus, with cells expressing GCamP6. This method is designed to be sample-independent thanks to an automatic axial locking on objects of interest through the use of an image-based metric that we designed. Using this method, we show an increase of in vivo imaging quality in the hippocampus.