""" Reference: Sreenivas Bhattiprolu - Non-Local Means (NLM) algorithm -------------------------------------------------------------------------------- Works well for Random Gaussian noise but not as good for salt and pepper. The non-local means algorithm replaces the value of a pixel by an average of a selection of other pixels values: small patches centered on the other pixels are compared to the patch centered on the pixel of interest, and the average is performed only for pixels that have patches close to current patch. -------------------------------------------------------------------------------- """ import cv2, sys, os import numpy as np from skimage import io, img_as_float from skimage.restoration import denoise_nl_means, estimate_sigma file_name = str(sys.argv[1]) im = os.getcwd() + "\\" + file_name img = img_as_float(io.imread(im, as_gray=True)) sigma_est = np.mean(estimate_sigma(img, channel_axis=None)) den_img = denoise_nl_means( img, h=1.15 * sigma_est, fast_mode=True, patch_size=5, patch_distance=3, channel_axis=None ) """ When the fast_mode argument is False, a spatial Gaussian weighting is applied to the patches when computing patch distances. When fast_mode is True a faster algorithm employing uniform spatial weighting on the patches is applied. Larger 'h' allows more smoothing between disimilar patches. """ cv2.imshow("Original", img) cv2.imshow("Filtered", den_img) cv2.waitKey(0) cv2.destroyAllWindows()