-
Notifications
You must be signed in to change notification settings - Fork 280
/
tutorial35_denoising_using_NLM.py
48 lines (32 loc) · 1.73 KB
/
tutorial35_denoising_using_NLM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#Video Playlist: https://www.youtube.com/playlist?list=PLHae9ggVvqPgyRQQOtENr6hK0m1UquGaG
"""
https://scikit-image.org/docs/dev/auto_examples/filters/plot_nonlocal_means.html
Works well for random gaussian noise but not as good for salt and pepper
https://www.iro.umontreal.ca/~mignotte/IFT6150/Articles/Buades-NonLocal.pdf
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 the current patch.
"""
import cv2
import numpy as np
from skimage import io, img_as_float
from skimage.restoration import denoise_nl_means, estimate_sigma
img_gaussian_noise = img_as_float(io.imread('images/Osteosarcoma_01_25Sigma_noise.tif', as_gray=True))
img_salt_pepper_noise = img_as_float(io.imread('images/Osteosarcoma_01_8bit_salt_pepper.tif', as_gray=True))
img = img_gaussian_noise
sigma_est = np.mean(estimate_sigma(img, multichannel=True))
#sigma_est = 0.1
denoise_img = denoise_nl_means(img, h=1.15 * sigma_est, fast_mode=True,
patch_size=5, patch_distance=3, multichannel=False)
"""
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.
"""
#denoise_img_as_8byte = img_as_ubyte(denoise_img)
cv2.imshow("Original", img)
cv2.imshow("NLM Filtered", denoise_img)
cv2.waitKey(0)
cv2.destroyAllWindows()