def peakmem_denoise_nl_means_f64(self): denoise_nl_means(self.volume_f64, patch_size=3, patch_distance=2, sigma=self.sigma, h=0.7 * self.sigma, fast_mode=False)
def time_denoise_nl_means_fast_f64(self): denoise_nl_means(self.volume_f64, patch_size=3, patch_distance=2, sigma=self.sigma, h=0.7 * self.sigma, fast_mode=True)
def time_denoise_nl_means_f32(self): denoise_nl_means(self.volume_f32, patch_size=3, patch_distance=2, sigma=self.sigma, h=0.7 * self.sigma, fast_mode=False)
def test_denoise_nl_means_2d(fast_mode): img = np.zeros((40, 40)) img[10:-10, 10:-10] = 1. sigma = 0.3 img += sigma * np.random.randn(*img.shape) img_f32 = img.astype('float32') for s in [sigma, 0]: denoised = restoration.denoise_nl_means(img, 7, 5, 0.2, fast_mode=fast_mode, multichannel=True, sigma=s) # make sure noise is reduced assert_(img.std() > denoised.std()) denoised_f32 = restoration.denoise_nl_means(img_f32, 7, 5, 0.2, fast_mode=fast_mode, multichannel=True, sigma=s) # make sure noise is reduced assert_(img.std() > denoised_f32.std()) # Sheck single precision result assert np.allclose(denoised_f32, denoised, atol=1e-2)
def peakmem_denoise_nl_means_fast_f32(self): denoise_nl_means(self.volume_f32, patch_size=3, patch_distance=2, sigma=self.sigma, h=0.7 * self.sigma, fast_mode=True)
def NLM(noisy_images: np.ndarray, noise_std_dev: float, show_progress=False) -> np.ndarray: validate_array_input(noisy_images) validate_if_noise_std_dev_is_a_float(noise_std_dev) filtered_images = [] if show_progress: for i in tqdm(range(noisy_images.shape[0])): filtered_images.append( denoise_nl_means( noisy_images[i, :,:,0], sigma=noise_std_dev ) ) else: for i in range(noisy_images.shape[0]): filtered_images.append( denoise_nl_means( noisy_images[i, :,:,0], sigma=noise_std_dev ) ) filtered_images = np.array(filtered_images) return filtered_images
def peakmem_denoise_nl_means_f32(self): restoration.denoise_nl_means(self.volume_f32, patch_size=3, patch_distance=2, sigma=self.sigma, h=0.7 * self.sigma, fast_mode=False)
def time_denoise_nl_means_fast_f32(self): restoration.denoise_nl_means(self.volume_f32, patch_size=3, patch_distance=2, sigma=self.sigma, h=0.7 * self.sigma, fast_mode=True)
def Nonlocal_proj(S, lambda_this): """ This function does the Non local mean projection """ S1 = np.reshape(S[0, :], image_size) S2 = np.reshape(S[1, :], image_size) patch_kw = dict( patch_size=8, # 5x5 patches patch_distance=7, # 13x13 search area multichannel=False) # Nonlocal mean denoising S1 = denoise_nl_means(S1, h=1.12 * lambda_this, fast_mode=False, sigma=lambda_this, **patch_kw) S2 = denoise_nl_means(S2, h=1.12 * lambda_this, fast_mode=False, sigma=lambda_this, **patch_kw) S[0, :] = np.reshape(S1, (1, n * n)) S[1, :] = np.reshape(S2, (1, n * n)) return S
def test_denoise_nl_means_2d_multichannel_deprecated(): # reduce image size because nl means is slow img = np.copy(astro[:50, :50]) # add some random noise sigma = 0.1 imgn = img + sigma * np.random.standard_normal(img.shape) imgn = np.clip(imgn, 0, 1) psnr_noisy = peak_signal_noise_ratio(img, imgn) with expected_warnings(["`multichannel` is a deprecated argument"]): denoised = restoration.denoise_nl_means(imgn, 3, 5, h=0.75 * sigma, multichannel=True, sigma=sigma) psnr_denoised = peak_signal_noise_ratio(denoised, img) # make sure noise is reduced assert_(psnr_denoised > psnr_noisy) # providing multichannel argument positionally also warns with expected_warnings(["Providing the `multichannel` argument"]): restoration.denoise_nl_means(imgn, 3, 5, 0.75 * sigma, True, sigma=sigma)
def test_denoise_nl_means_multichannel(fast_mode, dtype, channel_axis): # for true 3D data, 3D denoising is better than denoising as 2D+channels dtype = np.float64 rstate = np.random.RandomState(5) # synthetic 3d volume img = data.binary_blobs(length=32, n_dim=3, seed=5) img = img[:, :24, :16].astype(dtype, copy=False) sigma = 0.2 imgn = img + sigma * rstate.randn(*img.shape) imgn = imgn.astype(dtype) # test 3D denoising (channel_axis = None) denoised_ok_multichannel = restoration.denoise_nl_means( imgn, 3, 2, h=0.6 * sigma, sigma=sigma, fast_mode=fast_mode, channel_axis=None) # set a channel axis: one dimension is (incorrectly) considered "channels" imgn = np.moveaxis(imgn, -1, channel_axis) denoised_wrong_multichannel = restoration.denoise_nl_means( imgn, 3, 2, h=0.6 * sigma, sigma=sigma, fast_mode=fast_mode, channel_axis=channel_axis ) denoised_wrong_multichannel = np.moveaxis( denoised_wrong_multichannel, channel_axis, -1 ) psnr_wrong = peak_signal_noise_ratio(img, denoised_wrong_multichannel) psnr_ok = peak_signal_noise_ratio(img, denoised_ok_multichannel) assert_(psnr_ok > psnr_wrong)
def test_denoise_nl_means_3d(): img = np.zeros((12, 12, 8)) img[5:-5, 5:-5, 2:-2] = 1. sigma = 0.3 imgn = img + sigma * np.random.randn(*img.shape) psnr_noisy = compare_psnr(img, imgn) for s in [sigma, 0]: denoised = restoration.denoise_nl_means(imgn, 3, 4, h=0.75 * sigma, fast_mode=True, multichannel=False, sigma=s) # make sure noise is reduced assert_(compare_psnr(img, denoised) > psnr_noisy) denoised = restoration.denoise_nl_means(imgn, 3, 4, h=0.75 * sigma, fast_mode=False, multichannel=False, sigma=s) # make sure noise is reduced assert_(compare_psnr(img, denoised) > psnr_noisy)
def time_denoise_nl_means_f32(self): restoration.denoise_nl_means(self.volume_f32, patch_size=3, patch_distance=2, sigma=self.sigma, h=0.7 * self.sigma, fast_mode=False, **_channel_kwarg(False))
def time_denoise_nl_means_fast_f64(self): restoration.denoise_nl_means(self.volume_f64, patch_size=3, patch_distance=2, sigma=self.sigma, h=0.7 * self.sigma, fast_mode=True, multichannel=False)
def peakmem_denoise_nl_means_fast_f64(self): restoration.denoise_nl_means(self.volume_f64, patch_size=3, patch_distance=2, sigma=self.sigma, h=0.7 * self.sigma, fast_mode=True, **_channel_kwarg(False))
def smoothing(image, smoothReconMode, smoothIncrement, smoothMidIteration, smoothMaxIteration, iterationNumber): f = image fdtype = float if smoothReconMode > 0 and iterationNumber % smoothIncrement == 0 and iterationNumber > 0: #smooth to stem growth of noise fCenter = image #avoid padding issues with some smoothing algorithms by ensuring image is centered fReal = np.real(fCenter) fImag = np.imag(fCenter) if smoothReconMode == 1: print("Smooth TV") h = 2 if iterationNumber > smoothMidIteration and iterationNumber <= smoothMaxIteration: h /= 2.0 elif iterationNumber > smoothMaxIteration: h /= 4.0 fReal = denoise_tv_chambolle(fReal, h, multichannel=False) fImag = denoise_tv_chambolle(fImag, h, multichannel=False) elif smoothReconMode == 2: print("Smooth Median") fReal = ndimage.median_filter(fReal, 4).astype(fdtype) fImag = ndimage.median_filter(fImag, 4).astype(fdtype) elif smoothReconMode == 3: h = 4 ''' NLM Smoothing Notes: A higher h results in a smoother image, at the expense of blurring features. For a Gaussian noise of standard deviation sigma, a rule of thumb is to choose the value of h to be sigma of slightly less. The image is padded using the reflect mode of skimage.util.pad before denoising. ''' if iterationNumber > smoothMidIteration and iterationNumber <= smoothMaxIteration: h /= 2.0 elif iterationNumber > smoothMaxIteration: h /= 4.0 print("Smooth NL h:", h) fReal = denoise_nl_means(fReal, patch_size=5, patch_distance=11, h=h, multichannel=False, fast_mode=True).astype(fdtype) fImag = denoise_nl_means(fImag, patch_size=5, patch_distance=11, h=h, multichannel=False, fast_mode=True).astype(fdtype) elif smoothReconMode == 4: print("Smooth Bilateral") fReal = denoise_bilateral(fReal, sigma_color=0.15, sigma_spatial=7, multichannel=False).astype(fdtype) fImag = denoise_bilateral(fImag, sigma_color=0.15, sigma_spatial=7, multichannel=False).astype(fdtype) f = fReal + 1j * fImag return f
def test_no_denoising_for_small_h(): img = np.zeros((40, 40)) img[10:-10, 10:-10] = 1. img += 0.3*np.random.randn(*img.shape) # very small h should result in no averaging with other patches denoised = restoration.denoise_nl_means(img, 7, 5, 0.01, fast_mode=True) assert np.allclose(denoised, img) denoised = restoration.denoise_nl_means(img, 7, 5, 0.01, fast_mode=False) assert np.allclose(denoised, img)
def test_nl_means_denoising_2d(): img = np.zeros((40, 40)) img[10:-10, 10:-10] = 1. img += 0.3*np.random.randn(*img.shape) denoised = restoration.denoise_nl_means(img, 7, 5, 0.2, fast_mode=True) # make sure noise is reduced assert img.std() > denoised.std() denoised = restoration.denoise_nl_means(img, 7, 5, 0.2, fast_mode=False) # make sure noise is reduced assert img.std() > denoised.std()
def test_denoise_nl_means_3d(): img = np.zeros((20, 20, 10)) img[5:-5, 5:-5, 3:-3] = 1. img += 0.3*np.random.randn(*img.shape) denoised = restoration.denoise_nl_means(img, 5, 4, 0.2, fast_mode=True, multichannel=False) # make sure noise is reduced assert img.std() > denoised.std() denoised = restoration.denoise_nl_means(img, 5, 4, 0.2, fast_mode=False, multichannel=False) # make sure noise is reduced assert img.std() > denoised.std()
def test_denoise_nl_means_2drgb(): # reduce image size because nl means is very slow img = np.copy(astro[:50, :50]) # add some random noise img += 0.5 * img.std() * np.random.random(img.shape) img = np.clip(img, 0, 1) denoised = restoration.denoise_nl_means(img, 7, 9, 0.3, fast_mode=True) # make sure noise is reduced assert img.std() > denoised.std() denoised = restoration.denoise_nl_means(img, 7, 9, 0.3, fast_mode=False) # make sure noise is reduced assert img.std() > denoised.std()
def test_denoise_nl_means_3d_dtype(fast_mode): img = np.zeros((12, 12, 8), dtype=int) img_f32 = img.astype('float32') img_f64 = img.astype('float64') assert restoration.denoise_nl_means( img, patch_distance=2, fast_mode=fast_mode).dtype == 'float64' assert restoration.denoise_nl_means( img_f32, patch_distance=2, fast_mode=fast_mode).dtype == img_f32.dtype assert restoration.denoise_nl_means( img_f64, patch_distance=2, fast_mode=fast_mode).dtype == img_f64.dtype
def test_denoise_nl_means_2d_dtype(fast_mode): img = np.zeros((40, 40), dtype=int) img_f32 = img.astype('float32') img_f64 = img.astype('float64') assert restoration.denoise_nl_means( img, fast_mode=fast_mode).dtype == 'float64' assert restoration.denoise_nl_means( img_f32, fast_mode=fast_mode).dtype == img_f32.dtype assert restoration.denoise_nl_means( img_f64, fast_mode=fast_mode).dtype == img_f64.dtype
def test_denoise_nl_means_multichannel(): # for true 3D data, 3D denoising is better than denoising as 2D+channels img = np.zeros((13, 10, 8)) img[6, 4:6, 2:-2] = 1. sigma = 0.3 imgn = img + sigma * np.random.randn(*img.shape) denoised_wrong_multichannel = restoration.denoise_nl_means( imgn, 3, 4, 0.6 * sigma, fast_mode=True, multichannel=True) denoised_ok_multichannel = restoration.denoise_nl_means( imgn, 3, 4, 0.6 * sigma, fast_mode=True, multichannel=False) psnr_wrong = compare_psnr(img, denoised_wrong_multichannel) psnr_ok = compare_psnr(img, denoised_ok_multichannel) assert_(psnr_ok > psnr_wrong)
def test_denoise_nl_means_multichannel(): img = np.zeros((21, 20, 10)) img[10, 9:11, 2:-2] = 1. img += 0.3*np.random.randn(*img.shape) denoised_wrong_multichannel = restoration.denoise_nl_means( img, 5, 4, 0.1, fast_mode=True, multichannel=True) denoised_ok_multichannel = restoration.denoise_nl_means( img, 5, 4, 0.1, fast_mode=True, multichannel=False) snr_wrong = 10 * np.log10(1. / ((denoised_wrong_multichannel - img)**2).mean()) snr_ok = 10 * np.log10(1. / ((denoised_ok_multichannel - img)**2).mean()) assert_(snr_ok > snr_wrong)
def test_denoise_nl_means_multichannel(): img = np.zeros((21, 20, 10)) img[10, 9:11, 2:-2] = 1. img += 0.3*np.random.randn(*img.shape) denoised_wrong_multichannel = restoration.denoise_nl_means(img, 5, 4, 0.1, fast_mode=True, multichannel=True) denoised_ok_multichannel = restoration.denoise_nl_means(img, 5, 4, 0.1, fast_mode=True, multichannel=False) snr_wrong = 10 * np.log10(1. / ((denoised_wrong_multichannel - img)**2).mean()) snr_ok = 10 * np.log10(1. / ((denoised_ok_multichannel - img)**2).mean()) assert snr_ok > snr_wrong
def test_no_denoising_for_small_h(fast_mode, dtype): img = np.zeros((40, 40)) img[10:-10, 10:-10] = 1. img += 0.3*np.random.randn(*img.shape) img = img.astype(dtype) # very small h should result in no averaging with other patches denoised = restoration.denoise_nl_means(img, 7, 5, 0.01, fast_mode=fast_mode, channel_axis=None) assert_(np.allclose(denoised, img)) denoised = restoration.denoise_nl_means(img, 7, 5, 0.01, fast_mode=fast_mode, channel_axis=None) assert_(np.allclose(denoised, img))
def test_denoise_nl_means_3d_dtype(fast_mode): img = np.zeros((12, 12, 8), dtype=int) img_f32 = img.astype('float32') img_f64 = img.astype('float64') with expected_warnings(['Image dtype is not float']): assert restoration.denoise_nl_means( img, fast_mode=fast_mode).dtype == 'float64' assert restoration.denoise_nl_means( img_f32, fast_mode=fast_mode).dtype == img_f32.dtype assert restoration.denoise_nl_means( img_f64, fast_mode=fast_mode).dtype == img_f64.dtype
def Denoiser(d_name, sigma_f, x_f): x = torch_to_np(x_f) if d_name == 'nlm': patch_kw = dict(patch_size=5, # 5x5 patches patch_distance=6, # 13x13 search area multichannel=True) s0 = np.mean(estimate_sigma(x[0], multichannel=True)) s1 = np.mean(estimate_sigma(x[1], multichannel=True)) x0 = denoise_nl_means(x[0], h=s0, sigma=s0, fast_mode=False, **patch_kw) x1 = denoise_nl_means(x[1], h=s1, sigma=s1, fast_mode=False, **patch_kw) x = np.stack([x0, x1]) else: raise "other denoisers not implemented" x_f = np_to_torch(x) return x_f
def test_denoise_nl_means_2d(): img = np.zeros((40, 40)) img[10:-10, 10:-10] = 1. sigma = 0.3 img += sigma * np.random.randn(*img.shape) for s in [sigma, 0]: denoised = restoration.denoise_nl_means(img, 7, 5, 0.2, fast_mode=True, multichannel=True, sigma=s) # make sure noise is reduced assert_(img.std() > denoised.std()) denoised = restoration.denoise_nl_means(img, 7, 5, 0.2, fast_mode=False, multichannel=True, sigma=s) # make sure noise is reduced assert_(img.std() > denoised.std())
def chain_2(image, sigma=2): """ función que aplica el filtro bilateral, NLM, unsharp, una 2da iteración del NLM y la mediana """ A = cv2.bilateralFilter(image, 5, 50, 50) B = denoise_nl_means(A, patch_size=10, patch_distance=10, h=0.1, preserve_range=False) C = np.around(unsharp_mask(B, radius=10, amount=10), 2) D = denoise_nl_means(C, patch_size=15, patch_distance=20, h=0.19) E = median(D) return
def test_denoise_nl_means_3d(): img = np.zeros((20, 20, 10)) img[5:-5, 5:-5, 3:-3] = 1. sigma = 0.3 img += sigma * np.random.randn(*img.shape) for s in [sigma, 0]: denoised = restoration.denoise_nl_means(img, 5, 4, 0.2, fast_mode=True, multichannel=False, sigma=s) # make sure noise is reduced assert_(img.std() > denoised.std()) denoised = restoration.denoise_nl_means(img, 5, 4, 0.2, fast_mode=False, multichannel=False, sigma=s) # make sure noise is reduced assert_(img.std() > denoised.std())
def denoiseNonLocalMeans(imagen): """ Reemplaza la intensidad de cada pixel con la media de los pixels a su alrededor """ noisy = img_as_float(imagen) denoise = denoise_nl_means(noisy, patch_size=4, patch_distance=7, h=0.05) return denoise
def _correctNoise(self, image): ''' denoise using non-local-means with guessing best parameters ''' from skimage.restoration import denoise_nl_means # save startup time image[np.isnan(image)] = 0 # otherwise result =nan out = denoise_nl_means(image, patch_size=7, patch_distance=11, #h=signalStd(image) * 0.1 ) return out
def DenoisingNLM2D(image, **kwargs): # GOAL: denoise a 2D image and return the denoised image using NLM # Import the function to apply nl means in 2D images from skimage.restoration import denoise_nl_means # Get the parameters for the denoising min_dim = float(min(image.shape)) patch_size = kwargs.pop('patch_size', int(np.ceil(min_dim / 30.))) patch_distance = kwargs.pop('patch_distance', int(np.ceil(min_dim / 15.))) h = kwargs.pop('h', 0.04) multichannel = kwargs.pop('multichannel', False) fast_mode = kwargs.pop('fast_mode', True) img_den = denoise_nl_means(image, patch_size=patch_size, patch_distance=patch_distance, h=h, multichannel=multichannel, fast_mode=fast_mode) # Perform the denoising return img_den
def test_denoise_nl_means_2d_multichannel(): # reduce image size because nl means is slow img = np.copy(astro[:50, :50]) img = np.concatenate((img, ) * 2, ) # 6 channels # add some random noise sigma = 0.1 imgn = img + sigma * np.random.standard_normal(img.shape) imgn = np.clip(imgn, 0, 1) for fast_mode in [True, False]: for s in [sigma, 0]: for n_channels in [2, 3, 6]: psnr_noisy = compare_psnr(img[..., :n_channels], imgn[..., :n_channels]) denoised = restoration.denoise_nl_means(imgn[..., :n_channels], 3, 5, h=0.75 * sigma, fast_mode=fast_mode, multichannel=True, sigma=s) psnr_denoised = compare_psnr(denoised[..., :n_channels], img[..., :n_channels]) # make sure noise is reduced assert_(psnr_denoised > psnr_noisy)
astro = img_as_float(data.astronaut()) astro = astro[30:180, 150:300] sigma = 0.08 noisy = random_noise(astro, var=sigma**2) # estimate the noise standard deviation from the noisy image sigma_est = np.mean(estimate_sigma(noisy, multichannel=True)) print("estimated noise standard deviation = {}".format(sigma_est)) patch_kw = dict(patch_size=5, # 5x5 patches patch_distance=6, # 13x13 search area multichannel=True) # slow algorithm denoise = denoise_nl_means(noisy, h=1.15 * sigma_est, fast_mode=False, **patch_kw) # slow algorithm, sigma provided denoise2 = denoise_nl_means(noisy, h=0.8 * sigma_est, sigma=sigma_est, fast_mode=False, **patch_kw) # fast algorithm denoise_fast = denoise_nl_means(noisy, h=0.8 * sigma_est, fast_mode=True, **patch_kw) # fast algorithm, sigma provided denoise2_fast = denoise_nl_means(noisy, h=0.6 * sigma_est, sigma=sigma_est, fast_mode=True, **patch_kw) fig, ax = plt.subplots(nrows=2, ncols=3, figsize=(8, 6), sharex=True, sharey=True)
def test_denoise_nl_means_wrong_dimension(): img = np.zeros((5, 5, 5, 5)) with testing.raises(NotImplementedError): restoration.denoise_nl_means(img, multichannel=True)
import numpy as np import matplotlib.pyplot as plt from skimage import data, img_as_float from skimage.restoration import denoise_nl_means astro = img_as_float(data.astronaut()) astro = astro[30:180, 150:300] noisy = astro + 0.3 * np.random.random(astro.shape) noisy = np.clip(noisy, 0, 1) denoise = denoise_nl_means(noisy, 7, 9, 0.08) fig, ax = plt.subplots(ncols=2, figsize=(8, 4), sharex=True, sharey=True, subplot_kw={'adjustable':'box-forced'}) ax[0].imshow(noisy) ax[0].axis('off') ax[0].set_title('noisy') ax[1].imshow(denoise) ax[1].axis('off') ax[1].set_title('non-local means') fig.subplots_adjust(wspace=0.02, hspace=0.2, top=0.9, bottom=0.05, left=0, right=1) plt.show()
blurred by other denoising algoritm. """ import numpy as np import matplotlib.pyplot as plt from skimage import data, img_as_float from skimage.restoration import denoise_nl_means astro = img_as_float(data.astronaut()) astro = astro[30:180, 150:300] noisy = astro + 0.3 * np.random.random(astro.shape) noisy = np.clip(noisy, 0, 1) denoise = denoise_nl_means(noisy, 7, 9, 0.08, multichannel=True) fig, ax = plt.subplots(ncols=2, figsize=(8, 4), sharex=True, sharey=True, subplot_kw={'adjustable': 'box-forced'}) ax[0].imshow(noisy) ax[0].axis('off') ax[0].set_title('noisy') ax[1].imshow(denoise) ax[1].axis('off') ax[1].set_title('non-local means') fig.tight_layout() plt.show()