Strong subsurface attenuation leads to distortion of amplitudes and phases of seismic waves propagating inside the earth. The amplitude and the dispersion losses from attenuation are often compensated for during prestack depth migration. However, most attenuation compensation or Qcompensation migration algorithms require an estimate of the background Q model. We have developed a wave-equation gradient optimization method that inverts for the subsurface Q distribution by minimizing a skeletonized misfit function ∈, where ∈ is the sum of the squared differences between the observed and the predicted peak/centroid-frequency shifts of the early arrivals. The gradient is computed by migrating the observed traces weighted by the frequency shift residuals. The background Q model is perturbed until the predicted and the observed traces have the same peak frequencies or the same centroid frequencies. Numerical tests determined that an improved accuracy of the Q model by wave-equation Q tomography leads to a noticeable improvement in migration image quality. © 2016 Society of Exploration Geophysicists.