Thanks to the ability to perform imaging and manipulation at the nanoscale, atomic force microscopy (AFM) has been widely used in biology, materials, chemistry, and other fields. However, as common error sources, vertical drift and illusory slope severely impair AFM imaging quality. To address this issue, this paper proposes a robust algorithm to synchronously correct the image distortion caused by vertical drift and slope, thus achieving accurate morphology characterization. Specifically, to eliminate the damage of abnormal points and feature areas on the correction accuracy, the laser spot voltage error acquired in the AFM scanning process is first utilized to preprocess the morphology height data of the sample, so as to obtain the refined alternative data suitable for line fitting. Subsequently, this paper proposes a novel line fitting algorithm based on sparse sample consensus, which accurately simulates vertical drift and slope in the cross-sectional profile of the topographic image, thereby achieving effective correction of the image distortion. In the experiments and applications, a nanoscale optical grating sample and a biological cell sample are adopted to perform topography imaging and distortion correction, so as to verify the ability of the proposed algorithm to promote AFM imaging quality.