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.nl_means_denoising(img, 7, 5, 0.01, fast_mode=True) assert np.allclose(denoised, img) denoised = restoration.nl_means_denoising(img, 7, 5, 0.01, fast_mode=False) assert np.allclose(denoised, img)
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.nl_means_denoising(img, 7, 5, 0.01, fast_mode=True) assert np.allclose(denoised, img) denoised = restoration.nl_means_denoising(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.nl_means_denoising(img, 7, 5, 0.2, fast_mode=True) # make sure noise is reduced assert img.std() > denoised.std() denoised = restoration.nl_means_denoising(img, 7, 5, 0.2, fast_mode=False) # make sure noise is reduced assert img.std() > denoised.std()
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.nl_means_denoising(img, 7, 5, 0.2, fast_mode=True) # make sure noise is reduced assert img.std() > denoised.std() denoised = restoration.nl_means_denoising(img, 7, 5, 0.2, fast_mode=False) # make sure noise is reduced assert img.std() > denoised.std()
def test_nl_means_denoising_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.nl_means_denoising( img, 5, 4, 0.1, fast_mode=True, multichannel=True) denoised_ok_multichannel = restoration.nl_means_denoising( 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_nl_means_denoising_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.nl_means_denoising(img, 7, 9, 0.3, fast_mode=True) # make sure noise is reduced assert img.std() > denoised.std() denoised = restoration.nl_means_denoising(img, 7, 9, 0.3, fast_mode=False) # make sure noise is reduced assert img.std() > denoised.std()
def test_nl_means_denoising_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.nl_means_denoising(img, 5, 4, 0.2, fast_mode=True, multichannel=False) # make sure noise is reduced assert img.std() > denoised.std() denoised = restoration.nl_means_denoising(img, 5, 4, 0.2, fast_mode=False, multichannel=False) # make sure noise is reduced assert img.std() > denoised.std()
def test_nl_means_denoising_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.nl_means_denoising(img, 5, 4, 0.1, fast_mode=True, multichannel=True) denoised_ok_multichannel = restoration.nl_means_denoising(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 crop_data(f,mask,t): file_id = os.path.basename(f).split('.')[0] img = irtk.imread( f, dtype='float32',force_neurological=True ) seg = mask.transform(t.invert(), target=img,interpolation='nearest') x_min,y_min,z_min,x_max,y_max,z_max = seg.bbox() seg = seg[z_min:z_max+1, y_min:y_max+1, x_min:x_max+1] img = img[z_min:z_max+1, y_min:y_max+1, x_min:x_max+1].rescale(0,1000) + 1.0 # +1 to avoid zeros in the heart img_file = output_dir + '/img_' + file_id + ".nii.gz" irtk.imwrite( img_file, img ) for z in range(img.shape[0]): scale = img[z].max() img[z] = restoration.nl_means_denoising(img[z].rescale(0.0,1.0).view(np.ndarray), fast_mode=False, patch_size=5, patch_distance=7, h=0.05, multichannel=False) img[z] *= scale img[seg==0] = 0 masked_file = output_dir + '/masked_' + file_id + ".nii.gz" irtk.imwrite( masked_file, img )
def blur_predict(model, X, type="median", filter_size=3, sigma=1.0): if type == "median": blured_X = np.array(list(map(lambda x: ndimage.median_filter(x, filter_size), X))) elif type == "gaussian": blured_X = np.array(list(map(lambda x: ndimage.gaussian_filter(x, filter_size), X))) elif type == "f_gaussian": blured_X = np.array(list(map(lambda x: filters.gaussian_filter(x.reshape((28, 28)), sigma=sigma).reshape(784), X))) elif type == "tv_chambolle": blured_X = np.array(list(map(lambda x: restoration.denoise_tv_chambolle(x.reshape((28, 28)), weight=0.2).reshape(784), X))) elif type == "tv_bregman": blured_X = np.array(list(map(lambda x: restoration.denoise_tv_bregman(x.reshape((28, 28)), weight=5.0).reshape(784), X))) elif type == "bilateral": blured_X = np.array(list(map(lambda x: restoration.denoise_bilateral(np.abs(x).reshape((28, 28))).reshape(784), X))) elif type == "nl_means": blured_X = np.array(list(map(lambda x: restoration.nl_means_denoising(x.reshape((28, 28))).reshape(784), X))) elif type == "none": blured_X = X else: raise ValueError("unsupported filter type", type) return predict(model, blured_X)
def nl_means( noisy , patch_size=7 , patch_distance=11 , h=0.1 , multichannel=False , fast_mode=True ): noisy[:] , m , delta = change_range( noisy ) denois = rst.nl_means_denoising( noisy , patch_size=patch_size , patch_distance=patch_distance , h=h , multichannel=multichannel , fast_mode=fast_mode ) denois[:] = restore_range( denois , m , delta ) return denois
def test_nl_means_denoising_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.nl_means_denoising(img, 5, 4, 0.2, fast_mode=True, multichannel=False) # make sure noise is reduced assert img.std() > denoised.std() denoised = restoration.nl_means_denoising(img, 5, 4, 0.2, fast_mode=False, multichannel=False) # make sure noise is reduced assert img.std() > denoised.std()
cameraman = sk.img_as_float(io.imread('../../cameraman.bmp')) cameraman_uni = sk.img_as_float(io.imread('../../cameraman_uni.bmp')) cameraman_nor = sk.img_as_float(io.imread('../../cameraman_nor.bmp')) cameraman_ric = sk.img_as_float(io.imread('../../cameraman_ric.bmp')) cameraman_sp = sk.img_as_float(io.imread('../../cameraman_sp.bmp')) baboon = sk.img_as_float(io.imread('../../baboon.bmp')) baboon_uni = sk.img_as_float(io.imread('../../baboon_uni.bmp')) baboon_nor = sk.img_as_float(io.imread('../../baboon_nor.bmp')) baboon_ric = sk.img_as_float(io.imread('../../baboon_ric.bmp')) baboon_sp = sk.img_as_float(io.imread('../../baboon_sp.bmp')) # Apply filters lena_uni_f = sk.img_as_float(res.nl_means_denoising(lena_uni,patch_size=3)) lena_nor_f = sk.img_as_float(res.nl_means_denoising(lena_nor,patch_size=3)) lena_ric_f = sk.img_as_float(res.nl_means_denoising(lena_ric,patch_size=3)) lena_sp_f = sk.img_as_float(res.nl_means_denoising(lena_sp,patch_size=3)) cameraman_uni_f = sk.img_as_float(res.nl_means_denoising(cameraman_uni,patch_size=3)) cameraman_nor_f = sk.img_as_float(res.nl_means_denoising(cameraman_nor,patch_size=3)) cameraman_ric_f = sk.img_as_float(res.nl_means_denoising(cameraman_ric,patch_size=3)) cameraman_sp_f = sk.img_as_float(res.nl_means_denoising(cameraman_sp,patch_size=3)) baboon_uni_f = sk.img_as_float(res.nl_means_denoising(baboon_uni,patch_size=3)) baboon_nor_f = sk.img_as_float(res.nl_means_denoising(baboon_nor,patch_size=3)) baboon_ric_f = sk.img_as_float(res.nl_means_denoising(baboon_ric,patch_size=3)) baboon_sp_f = sk.img_as_float(res.nl_means_denoising(baboon_sp,patch_size=3)) # Calculate MSE
lena_sp = sk.img_as_float(io.imread('../../lena_sp.bmp')) cameraman = sk.img_as_float(io.imread('../../cameraman.bmp')) cameraman_uni = sk.img_as_float(io.imread('../../cameraman_uni.bmp')) cameraman_nor = sk.img_as_float(io.imread('../../cameraman_nor.bmp')) cameraman_ric = sk.img_as_float(io.imread('../../cameraman_ric.bmp')) cameraman_sp = sk.img_as_float(io.imread('../../cameraman_sp.bmp')) baboon = sk.img_as_float(io.imread('../../baboon.bmp')) baboon_uni = sk.img_as_float(io.imread('../../baboon_uni.bmp')) baboon_nor = sk.img_as_float(io.imread('../../baboon_nor.bmp')) baboon_ric = sk.img_as_float(io.imread('../../baboon_ric.bmp')) baboon_sp = sk.img_as_float(io.imread('../../baboon_sp.bmp')) # Apply filters lena_uni_f = sk.img_as_float(res.nl_means_denoising(lena_uni, patch_size=3)) lena_nor_f = sk.img_as_float(res.nl_means_denoising(lena_nor, patch_size=3)) lena_ric_f = sk.img_as_float(res.nl_means_denoising(lena_ric, patch_size=3)) lena_sp_f = sk.img_as_float(res.nl_means_denoising(lena_sp, patch_size=3)) cameraman_uni_f = sk.img_as_float( res.nl_means_denoising(cameraman_uni, patch_size=3)) cameraman_nor_f = sk.img_as_float( res.nl_means_denoising(cameraman_nor, patch_size=3)) cameraman_ric_f = sk.img_as_float( res.nl_means_denoising(cameraman_ric, patch_size=3)) cameraman_sp_f = sk.img_as_float( res.nl_means_denoising(cameraman_sp, patch_size=3)) baboon_uni_f = sk.img_as_float(res.nl_means_denoising(baboon_uni, patch_size=3))
for ax in axes.flatten()] for i, driver in enumerate(np.unique(drivers)[:5]): for j, n in enumerate(range(3)): #axes[i, j].imshow(denoise_bilateral(X[j*5+i,:,:,0], sigma_range=0.05, sigma_spatial=4), cmap='gray') axes[i, j].imshow(exposure.equalize_hist(X[j * 5 + i, :, :, 0]), cmap='gray') axes[i, j].set_title(driver) plt.show() exit() figs, axes = plt.subplots(5, 3, figsize=(10, 15)) [(ax.set_xticks([]), ax.set_yticks([]), ax.axis('off')) for ax in axes.flatten()] for i, driver in enumerate(np.unique(drivers)[:5]): for j, n in enumerate(range(3)): axes[i, j].imshow(nl_means_denoising(X[j * 5 + i, :, :, :], 3, 4, 0.08)) axes[i, j].set_title(driver) plt.show() exit() # Compute the Canny filter for two values of sigma edges1 = denoise_tv_chambolle(im, weight=0.1, multichannel=False) ax1.imshow(im, cmap=plt.cm.gray) ax1.axis('off') ax1.set_title('Original image', fontsize=20) ax2.imshow(edges1, cmap=plt.cm.gray) ax2.axis('off') ax2.set_title('Filtered' + str(j), fontsize=20)
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 nl_means_denoising 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 = nl_means_denoising(noisy, 7, 9, 0.08) fig, ax = plt.subplots(ncols=2, figsize=(8, 4)) 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()
import matplotlib import numpy as np import matplotlib.pyplot as plt from skimage import io im = io.imread('datasetpaper.jpg') plt.imshow(im, cmap='gray') from skimage import restoration from skimage import img_as_float im_float = img_as_float(im) im_denoised = restoration.nl_means_denoising(im_float, h=0.05) plt.imshow(im_denoised, cmap='gray') ax = plt.axis('off') plt.imshow(im_denoised, cmap='gray') click_markers = plt.ginput(n=-1, timeout=-1) x, y = np.load('indices_markers.npy').T plt.imshow(im_denoised, cmap='gray') plt.plot(y, x, 'or', ms=4) from skimage import morphology markers = np.zeros(im_denoised.shape, dtype=np.int) markers[x.astype(np.int), y.astype(np.int)] = np.arange(len(x)) + 1 markers = morphology.dilation(markers, morphology.disk(7))
patch. As a result, this algorithm can restore well textures, that would be 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 nl_means_denoising 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 = nl_means_denoising(noisy, 7, 9, 0.08) fig, ax = plt.subplots(ncols=2, figsize=(8, 4)) 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,