The document presents a new algorithm for denoising ultrasound images called the optimal nonlocal means algorithm. It calculates the mean distance of all pixel neighborhoods in the image, rather than totaling all neighborhood distances as in the original nonlocal means algorithm. The proposed algorithm exhibits better performance in noise removal, visual quality of restored images, and mean square error compared to the original algorithm, as evidenced by experiments on phantom and normal ultrasound images. Numerical measurements of SNR, RMSE, and PSNR support that calculating nonlocal means with mean neighborhood distances provides a better method for image denoising.