We report an algorithm to identify and correct distorted wavefronts in atomic resolution scanning tunneling microscope images. This algorithm can be used to correct nonlinear in-plane distortions without prior knowledge of the physical scanning parameters, the characteristics of the piezoelectric actuator, or individual atom positions. The 2D image is first defined as a sum of sinusoidal plane waves, where a nonlinear distortion renders a curve for an otherwise ideal linear wavefront. Using the Fourier transforms of local areas of the image, the algorithm generates a wavefront vector field. The identified wavefronts are subsequently linearized for each plane wave without changing lattice orders, giving rise to distortion corrections. Our algorithm is complementary to conventional post-processing algorithms that require prior detection of real space features, which can also be used to correct nonlinear distortions in 2D images acquired by other microscopy techniques.