The accuracy of the Shack-Hartmann wavefront sensor (SHWS) for measuring the distortion wavefront is mainly dependent upon the measurement accuracy of the centroid of the focal spot. Many methods have been presented to improve the accuracy of the wavefront centroid measurement, but most of them are based on a point of improvement. Propose a complete centroid optimization method. Based on analyzing background noise of the focal spot image, the method of adaptive threshold denoising is introduced, then the sub-aperture detection windows are optimized by modified watershed algorithm, and the centroid of the focal spot is calculated by the higher moment centroid algorithm in an optimized window, where the linear interpolation have been used. Simulation and experimental results showed that the centroid detection window could automatically adjust its size, matching perfect with spot distribution area, and the proposed method could reach high precision and repeatability of focal spot centroid in some certain SNR.