Vous êtes sur la page 1sur 8

A Numerical Tour of Signal Processing: Image Denoising with Linear Methods - File Exchange - MATLAB Central

Search:

File Exchange File Exchange Create Account

Go
Log In

File Exchange

Answers

Newsgroup

Link Exchange

Blogs

Trendy

Cody

Contest

MathWorks.com

from A Numerical Tour of Signal Processing

Download Submission

Code covered by the BSD License

by Gabriel Peyre A set of Matlab experiments that illustrates advanced computational signal and image processing.

Highlights from

A Numerical Tour of Signal Processing


[p,ellipse]=phantom(varar... PHANTOM Generate a head phantom image. b=dct(a,n) DCT Discrete cosine transform. b=dct2(arg1,mrows,ncols) DCT2 Compute 2-D discrete cosine transform. cat3(d, a,b) cat3 - synonymous with cat cconv(x,h,d) cconv - spatial domain circular convolution cell_add(A,B,a,b) cell_add - add two cell arrays cell_get(x,i) cell_get - get value cell_set(x,i,v) cell_set - assign value cell_sub(x,sel) cell_sub - extract a sub-cell array clamp(x,a,b)

Image Denoising with Linear Methods

Image Denoising with Linear Methods


This numerical tour introduces some basics about image denoising.

Contents
Installing toolboxes and setting up the path. Noise in Signal and Image Linear Signal Denoising Linear Image Denoising Wiener filtering

Installing toolboxes and setting up the path.


You need to download the general purpose toolbox and the signal toolbox. You need to unzip these toolboxes in your working directory, so that you have toolbox_general/ and toolbox_signal/ in your directory. For Scilab user: you must replace the Matlab comment '%' by its Scilab counterpart '//'. Recommandation: You should create a text file named for instance numericaltour.sce (in Scilab) or numericaltour.m (in Matlab) to write all the Scilab/Matlab commands you want to execute. Then, simply run exec('numericaltour.sce'); (in Scilab) or numericaltour; (in Matlab) to run the commands. Execute this line only if you are using Matlab. getd = @(p)path(path,p); % scilab users must *not* execute this

compute_conditional_histo... compute_conditional_histogram compute conditional histograms compute_entropy(M,T) compute_entropy - compute the entropy of a signal compute_gaussian_filter(n... compute_gaussian_filter - compute a 1D or 2D Gaussian filter. compute_histogram(M, opti... compute_histogram - compute the (symmetric) histogram of a vector after threshold. compute_hufftree(p) compute_hufftree - build a huffman tree compute_l2err(MF, donorma... compute_l2err - non linear approximation error compute_max(X,d) compute_max - compute maximum along dimension d compute_min(X,d) compute_min - compute min along dimension d compute_quadsel(j,q, opti... compute_quadsel - compute the indices for selecting subband of a wavelet transform. compute_shape_boundary(M) compute_shape_boundary - extract boundary points of a shape compute_total_variation(y... compute_total_variation - compute the total variation of an image compute_wavelet_filter(Ty... compute_wavelet_filter - Generate Orthonormal QMF Filter for Wavelet Transform crop(M,n,c) crop - crop an image to reduce its size det3(A) det3 - 3x3 determinant div(Px,Py, options) div - divergence operator extend_stack_size(mult) does nothing gamrnd(a,b,m,n); GAMRND Random matrices from

Then you can add these toolboxes to the path. % Add some directories to the path getd('toolbox_signal/'); getd('toolbox_general/');

Noise in Signal and Image


In these numerical tour, we simulate noisy acquisition by adding some white noise (each pixel is corrupted by adding an independant Gaussian variable). This is helpful since we know the original, clean, image, and can test and compare several algorihtms by computing the recovery error. We load a signal. name = 'piece-regular'; n = 1024; x0 = load_signal(name,n); x0 = rescale(x0);

We add some noise to it. sigma = .04; % noise level x = x0 + sigma*randn(size(x0)); clf; subplot(2,1,1); plot(x0); axis([1 n -.05 1.05]); subplot(2,1,2); plot(x); axis([1 n -.05 1.05]);

http://www.mathworks.com/matlabcentral/fileexchange/9554-a-numerical-tour-of-signal-processing/content/numerical-tour/denoising_linear/index.html[7/29/2013 7:31:52 PM]

A Numerical Tour of Signal Processing: Image Denoising with Linear Methods - File Exchange - MATLAB Central

gamma distribution. getoptions(options, name,... getoptions - retrieve options parameter grad(M, options) grad - gradient, forward differences huffman_gencode(T) huffman_gencode - generate a code associated to a Hufmann tree T idct(b,n) IDCT Inverse discrete cosine transform. We load an image. name = 'boat'; n = 256; M0 = load_image(name,n); M0 = rescale( M0, .05, .95 ); idct2(arg1,mrows,ncols) IDCT2 Compute 2-D inverse discrete cosine transform. image_resize(M,p1,q1,r1) image_resize - resize an image using bicubic interpolation imageplot(M,str, a,b,c) Then we add some gaussian noise to it. sigma = .08; % noise level M = M0 + sigma*randn(size(M0)); clf; imageplot(M0, 'Original', 1,2,1); imageplot(clamp(M), 'Noisy', 1,2,2); imageplot - diplay an image and a title iradon(varargin) IRADON Compute inverse Radon transform. load_image(type, n, optio... load_image - load benchmark images. load_signal(name, n, opti... load_signal - load a 1D signal load_sound(name, n0, opti... mad(x,flag) MAD Mean/median absolute deviation. make_sparse(i,j,x) make_sparse - synonymous with sparse(i,j,x) max3(x,v) max3 - synonymouse with max nb_dims(x) nb_dims - debugged version of ndims. peform_wiener_filtering(x... peform_wiener_filtering - perform Wiener filtering perform_arith_coding(xC, ... perform_arithmetic_coding_slow perform adaptive arithmetic coding perform_arith_fixed(x, h, n) perform_arith_fixed - arithmetic coding perform_blurring(M, sigma... perform_blurring - gaussian blurs an image

Linear Signal Denoising


A translation invariant linear denoising is necessarely a convolution with a kernel h . It correspond to a linear diagonal operation over the Fourier domain that multiply each noisy Fourier coefficient by the Fourier transform of h . In practice, one uses a Gaussian fitler h , and the only parameter is the width (variance) of the filter. % width of the filter mu = 4; % compute a Gaussian filter of width mu t = (-length(x)/2:length(x)/2-1)'; h = exp( -(t.^2)/(2*mu^2) ); h = h/sum(h(:));

perform_cg(A,y,options) perform_convolution(x,h, ... perform_convolution - compute convolution with centered filter. perform_faces_reorientati... perform_faces_reorientation reorient the faces with respect to the center of the mesh perform_haar_transf(f, Jm... perform_haar_transf - peform fast Haar transform perform_hist_eq(x,y,options) perform_histogram_equalization perform histogram equalization

The Fourier transform of a Gaussian discrete filter is nearly a Gaussian whose width is proportional to 1/mu . % Fourier transform of the (centered) filter hf = real( fft(fftshift(h)) ); hf = fftshift(hf); % display clf; subplot(2,1,1); plot( t,h ); axis('tight'); title('Filter h');

perform_homotopy(D,y,opti... perform_homotopy - compute the L1 regularization path perform_huffcoding(x,T,dir) perform_huffcoding - perform huffman coding perform_median_filtering(... perform_median_filtering - perform moving average median perform_omp(D,Y,options)

http://www.mathworks.com/matlabcentral/fileexchange/9554-a-numerical-tour-of-signal-processing/content/numerical-tour/denoising_linear/index.html[7/29/2013 7:31:52 PM]

A Numerical Tour of Signal Processing: Image Denoising with Linear Methods - File Exchange - MATLAB Central
subplot(2,1,2); plot( t,hf ); axis('tight'); title('Fourier transform');

perform_omp - perform orthogonal matching pursuit perform_saturation(x,tau,... perform_saturation - saturate a vector for better contrast perform_stft(x, w,q, opti... perform_stft - compute a local Fourier transform perform_tensor_decomp(T,o... perform_tensor_decomp - perform an eigendecomposition. perform_tensor_recomp(e1,... perform_tensor_recomp - create the tensor field corresponding to the given eigendecomposition. perform_thresholding(x, t... perform_thresholding - perform hard or soft thresholding perform_tv_denoising(x,op... perform_tv_denoising - denoising with TV minimization perform_wavelet_transf(x,... perform_wavelet_transf - peform fast lifting transform perform_wavortho_transf(f... perform_wavortho_transf - compute orthogonal wavelet transform plot_dictionnary(D,X,nb, ...

Since we use periodic boundary condition, the convolution of x with h can be computer over the Fourier domain. % Fourier coefficients of the noisy signal xf = fft(x); % Fourier coefficients of the denoised signal xhf = xf .* fft(fftshift(h)); % Denoised signal xh = real( ifft(xhf) );

plot_dictionnary - display a dictionary of images plot_hufftree(T,p) plot_hufftree - plot a huffman tree plot_levelset(M,t,A) plot_levelset - display the level set of an image plot_sparse_diracs(x, opt... plot_sparse_diracs - plot sparse sets of diracs

We display the denoised signal. Although most of the noise is removed, the singularity have been blurred. clf; subplot(2,1,1); plot( t,x ); axis('tight'); title('Noisy'); subplot(2,1,2); plot( t,xh ); axis('tight'); title('Denoised');

plot_spectrogram(S,f, opt... plot_spectrogram - display the spectrogram plot_vf(vf, M, options) plot_vf - plot a vector field with plot_wavelet(M, Jmin, opt... plot_wavelet - plot 2D wavelet transform stored using Mallat's ordering. poissrnd(lambda,m,n) POISSRND Random matrices from Poisson distribution. progressbar(n,N,w) progressbar - display a progress bar psnr(x,y, vmax) psnr - compute the Peack Signal to Noise Ratio r=binornd(n,p,mm,nn) BINORND Random matrices from a binomial distribution. radon(IMG, THETA) This MATLAB function takes an image matrix and vector of angles and then rand_discr(p, m) rand_discr - discrete random generator read_bin(name,options) read_bin - read bin file rescale(x,a,b) reverse(x) flip a vector set_axis(v) set_axis - draw axis on/off set_colormap(a) set_colormap - set colors for display set_graphic_sizes(h,fs,lw) set_graphic_sizes - enlarge the size of the fonts set_label(xstr,ystr,zstr) set_label - set the label for a plot set_linewidth(h,lw) set_rand_seeds(a,b) set_rand_seeds - initialize rand and randn snr(x,y) snr - signal to noise ratio subsampling(x,d,p)

We display the noisy and denoised Fourier coefficients. One can see that the denoising remove the high frequency coefficients. % log of Fourier transforms epsilon = 1e-10; L0 = log10(epsilon+abs(fftshift(fft(x0)))); L = log10(epsilon+abs(fftshift(xf))); Lh = log10(epsilon+abs(fftshift(xhf))); % display Fourier transforms clf; subplot(2,1,1); plot( t, L, '-' ); axis([-length(x)/2 length(x)/2 -4 max(L)]); title('log of noisy Fourier coefs.'); subplot(2,1,2); plot( t, Lh, '-' ); axis([-length(x)/2 length(x)/2 -4 max(L)]); title('log of denoised Fourier coefs.');

http://www.mathworks.com/matlabcentral/fileexchange/9554-a-numerical-tour-of-signal-processing/content/numerical-tour/denoising_linear/index.html[7/29/2013 7:31:52 PM]

A Numerical Tour of Signal Processing: Image Denoising with Linear Methods - File Exchange - MATLAB Central
downsampling - subsampling along dimension d subselectdim(f,sel,d) subselectdim - select along a dimension sum3(M,k) for Scilab compatibility tet2tri(facet, vertex, ke... tet2tri - convert a tet mesh to a tri mesh upsampling(x,d,p) upsampling - add p zeros between samples along dimension d using_matlab() using_matlab - return 1 for Matlab, 0 for Scilab write_bin(M, name) write_bin - write a file to .bin format readable by scilab. batch_convert_bin.m View all files

It is non-trivial to select the width parameter mu to minimize the denoising error. It should account for both the variance of the noise and the power spectrum of the image. We display the blurring for an increasing value of mu . mulist = linspace(.5,4,4); clf; for i=1:length(mulist) mu = mulist(i); % compute the filter h = exp( -(t.^2)/(2*mu^2) ); h = h/sum(h(:)); % perform the blurring xh = real( ifft( fft(x) .* fft(fftshift(h)) )); subplot( 4,1,i ); plot(t, clamp(xh) ); axis('tight'); title(strcat(['\mu=' num2str(mu)])); end

Exercice 1: (the solution is exo1.m) Try for various Gaussian variance mu to compute the denoising xh . Compute, in an oracle manner, the best variance muopt by computing the residual error snr(x0,xh) . exo1;

The optimal smoothing width is 1.2333 pixels, SNR=24.7343dB.

http://www.mathworks.com/matlabcentral/fileexchange/9554-a-numerical-tour-of-signal-processing/content/numerical-tour/denoising_linear/index.html[7/29/2013 7:31:52 PM]

A Numerical Tour of Signal Processing: Image Denoising with Linear Methods - File Exchange - MATLAB Central

Display the results. % compute the optimal filter h = exp( -(t.^2)/(2*muopt^2) ); h = h/sum(h(:)); % perform blurring xh = real( ifft( fft(x) .* fft(fftshift(h)) )); % display clf; subplot(2,1,1); plot(t, clamp(x)); axis('tight'); title('Noisy'); subplot(2,1,2); plot(t, clamp(xh)); axis('tight'); title('Denoised');

Linear Image Denoising


We denoise similarely a 2D image using a 2D Gaussian filter whose width mu is optimized to match the noise level and the regularity of the signal. We use a simple gaussian blur to denoise an image. % we use cyclic boundary condition since it is quite faster options.bound = 'per'; % number of pixel of the filter mu = 10; Mh = perform_blurring(M,mu,options); clf; imageplot(clamp(M), 'Noisy', 1,2,1); imageplot(clamp(Mh), 'Blurred', 1,2,2);

http://www.mathworks.com/matlabcentral/fileexchange/9554-a-numerical-tour-of-signal-processing/content/numerical-tour/denoising_linear/index.html[7/29/2013 7:31:52 PM]

A Numerical Tour of Signal Processing: Image Denoising with Linear Methods - File Exchange - MATLAB Central

We display the blurring for an increasing value of mu . mulist = linspace(3,15,6); clf; for i=1:length(mulist) mu = mulist(i); Mh = perform_blurring(M,mu,options); imageplot(clamp(Mh), strcat(['\mu=' num2str(mu)]), 2,3,i); end

Exercice 2: (the solution is exo2.m) Try for various Gaussian variance to compute the denoising Mh . Compute, in an oracle manner, the best variance muopt by computing the residual error snr(M0,Mh) . exo2;

The optimal smoothing width is 2.77 pixels, SNR=22.3095dB.

http://www.mathworks.com/matlabcentral/fileexchange/9554-a-numerical-tour-of-signal-processing/content/numerical-tour/denoising_linear/index.html[7/29/2013 7:31:52 PM]

A Numerical Tour of Signal Processing: Image Denoising with Linear Methods - File Exchange - MATLAB Central

Display the results % optimal filter Mgauss = perform_blurring(M,muopt,options); % display clf; imageplot(M, strcat(['Noisy, SNR=' num2str(snr(M0,M)) 'dB']), 1,2,1); imageplot(Mgauss, strcat(['Gaussian denoise, SNR=' num2str(snr(M0,Mgauss)) 'dB']), 1,2,2);

Wiener filtering
In a probabilistic setting, for translation invariant signal distributions, the Wiener filtering is the optimal filtering. Perform the wiener filtering [Mwien,Hwien] = peform_wiener_filtering(M0,M,sigma);

display the filter k = 5; clf; imageplot(Hwien(n/2-k+2:n/2+k,n/2-k+2:n/2+k), 'Wiener filter (zoom)');

http://www.mathworks.com/matlabcentral/fileexchange/9554-a-numerical-tour-of-signal-processing/content/numerical-tour/denoising_linear/index.html[7/29/2013 7:31:52 PM]

A Numerical Tour of Signal Processing: Image Denoising with Linear Methods - File Exchange - MATLAB Central

display the result % display clf; imageplot( clamp(Mgauss), strcat(['Gaussian denoise, SNR=' num2str(snr(M0,Mgauss)) 'dB']), 1,2,1); imageplot( clamp(Mwien), strcat(['Wiener denoise, SNR=' num2str(snr(M0,Mwien)) 'dB']), 1,2,2);

Copyright 2008 Gabriel Peyre

Contact us

1994-2013 The MathWorks, Inc. Featured MathWorks.com Topics: Site Help Patents Trademarks Support Privacy Policy Preventing Piracy Webinars Terms of Use Newsletters MATLAB Trials Careers

New Products

Documentation

Training

http://www.mathworks.com/matlabcentral/fileexchange/9554-a-numerical-tour-of-signal-processing/content/numerical-tour/denoising_linear/index.html[7/29/2013 7:31:52 PM]

Vous aimerez peut-être aussi