?? index.html
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"><html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document. --> <title>Variational Inpainting</title> <meta name="generator" content="MATLAB 7.4"> <meta name="date" content="2008-10-17"> <meta name="m-file" content="index"> <LINK REL="stylesheet" HREF="style.css" TYPE="text/css"> </head> <body> <div class="content"> <h1>Variational Inpainting</h1> <introduction> <p>This numerical tour explores the use of variational energies (Sobolev, Total variation and sparsity) to regularize the image inpaiting problem. </p> </introduction> <h2>Contents</h2> <div> <ul> <li><a href="#1">Installing toolboxes and setting up the path.</a></li> <li><a href="#8">Missing Pixels and Inpaiting</a></li> <li><a href="#12">Inpainting by Blurring</a></li> <li><a href="#16">Sobolev Impainting</a></li> <li><a href="#22">Inpainting with Sparsity</a></li> </ul> </div> <h2>Installing toolboxes and setting up the path.<a name="1"></a></h2> <p>You need to download the <a href="../toolbox_general.zip">general purpose toolbox</a> and the <a href="../toolbox_signal.zip">signal toolbox</a>. </p> <p>You need to unzip these toolboxes in your working directory, so that you have <tt>toolbox_general/</tt> and <tt>toolbox_signal/</tt> in your directory. </p> <p><b>For Scilab user:</b> you must replace the Matlab comment '%' by its Scilab counterpart '//'. </p> <p><b>Recommandation:</b> You should create a text file named for instance <tt>numericaltour.sce</tt> (in Scilabe) or <tt>numericaltour.m</tt> to write all the Scilab/Matlab command you want to execute. Then, simply run <tt>exec('numericaltour.sce');</tt> (in Scilab) or <tt>numericaltour;</tt> (in Matlab) to run the commands. </p> <p>Execute this line only if you are using Matlab.</p><pre class="codeinput">getd = @(p)path(path,p); <span class="comment">% scilab users must *not* execute this</span></pre><p>Then you can add these toolboxes to the path.</p><pre class="codeinput"><span class="comment">% Add some directories to the path</span>getd(<span class="string">'toolbox_signal/'</span>);getd(<span class="string">'toolbox_general/'</span>);</pre><h2>Missing Pixels and Inpaiting<a name="8"></a></h2> <p>Inpainting corresponds to filling holes in images.</p> <p>First we load the image to be inpainted.</p><pre class="codeinput">n = 128;M = load_image(<span class="string">'lena'</span>);M = rescale(crop(M,n));</pre><p>Then we construct a mask by removing random pixels.</p><pre class="codeinput"><span class="comment">% amount of removed pixels.</span>rho = .7;<span class="comment">% random mask, mask==1 for removed pixels</span>mask = zeros(n,n);sel = randperm(n^2); sel = sel(1:round(rho*n^2));mask(sel) = 1;</pre><p>Damaged observation (no noise)</p><pre class="codeinput"><span class="comment">% remove pixels</span>y = M;y(mask==1) = 0;<span class="comment">% display</span>clf;imageplot(M, <span class="string">'Image M'</span>, 1,2,1);imageplot(y, <span class="string">'Observation y'</span>, 1,2,2);</pre><img vspace="5" hspace="5" src="index_01.png"> <h2>Inpainting by Blurring<a name="12"></a></h2> <p>A simple way to recover the missing values of the image is simply to perform a blurring. To progressively fill the hole, the blurring must be repeated. </p> <p>A simple way to remove the hole is simply to perform a blurring.</p><pre class="codeinput">Mb = y;<span class="comment">% put average value inside the hole</span>Mb(mask==1) = mean(y(mask==0));<span class="comment">% blur the whole image</span>s = 3; <span class="comment">% number of pixels</span>Mb = perform_blurring(Mb,3);<span class="comment">% impose the known values</span>Mb(mask==0) = y(mask==0);</pre><p>You can iterate this several time.</p><pre class="codeinput">niter = 100;err = [];<span class="keyword">for</span> i=1:niter <span class="comment">% blur the whole image</span> Mb = perform_blurring(Mb,s); <span class="comment">% impose the known values</span> Mb(mask==0) = y(mask==0); <span class="comment">% compute the error</span> err(i) = snr(M, Mb);<span class="keyword">end</span><span class="comment">% display error decay</span>clf;plot(1:niter, err);set_label(<span class="string">'#iteration'</span>, <span class="string">'SNR'</span>);</pre><img vspace="5" hspace="5" src="index_02.png"> <p>Display.</p><pre class="codeinput">clf;imageplot(y, <span class="string">'Obervations y'</span>, 1,2,1);imageplot(Mb, strcat([<span class="string">'Inpainted, SNR='</span> num2str(snr(M,Mb),3) <span class="string">'dB'</span>]), 1,2,2);</pre><img vspace="5" hspace="5" src="index_03.png"> <h2>Sobolev Impainting<a name="16"></a></h2> <p>The simplest regularization is simply the Sobolev <tt>E(M) = \int grad(M)^2</tt> energy. Its gradient is the Laplacian, and the gradient descent corresponds to an iterated blurring with a small 3x3 mask. </p> <p>The gradient of the Dirichlet energy is simply the Laplacian. A step of gradient descent is computing y+tau*Laplacian, which perform a small blurring. </p><pre class="codeinput"><span class="comment">% use centered differences with reflecting boundary conditions</span>options.bound = <span class="string">'sym'</span>;options.order = 1;<span class="comment">% laplacian</span>LM = div(grad(M,options),options);Ly = div(grad(y,options),options);<span class="comment">% display</span>clf;imageplot(LM, <span class="string">'Laplacian of M'</span>, 1,2,1);imageplot(Ly, <span class="string">'Laplacian of y'</span>, 1,2,2);</pre><img vspace="5" hspace="5" src="index_04.png"> <p><i>Exercice 1:</i> (the solution is <a href="../private/inverse_inpainting/exo1.m">exo1.m</a>) Perform the inpainting by a projected gradient descent. After each step of descent, you obtain a solution <tt>Msob</tt> that is projected on the constraint by setting <tt>Msob(mask==0)=y(mask==0)</tt>. Keep track of the Dirichlet energy. The gradient step size must be less than 1/4, according to the CLF condition. </p><pre class="codeinput">exo1;</pre><img vspace="5" hspace="5" src="index_05.png"> <p>Display the result</p><pre class="codeinput">clf;imageplot(y, <span class="string">'Obervations y'</span>, 1,2,1);imageplot(Mb, strcat([<span class="string">'Inpainted, SNR='</span> num2str(snr(M,Msob),3) <span class="string">'dB'</span>]), 1,2,2);</pre><img vspace="5" hspace="5" src="index_06.png"> <p>We load another image with a non-random mask.</p><pre class="codeinput">n = 256;M = load_image(<span class="string">'parrot'</span>, n);<span class="keyword">if</span> nb_dims(M)==3 <span class="comment">% flatten color images</span> M = sum(M,3);<span class="keyword">end</span>M = rescale( M );mask1 = load_image(<span class="string">'parrot-mask'</span>, n);mask1 = double(rescale(mask1)<.5);</pre><p><i>Exercice 2:</i> (the solution is <a href="../private/inverse_inpainting/exo2.m">exo2.m</a>) Perform Sobolev inpainting on the image 'parrot' using as mask the binary image 'perrot-mask'. What do notice ? </p><pre class="codeinput">exo2;</pre><img vspace="5" hspace="5" src="index_07.png"> <h2>Inpainting with Sparsity<a name="22"></a></h2> <p>A non-linear prior replaces the Sobolev energy by the L1 norm of the wavelet coefficients in a translation invariant wavelet tight frames. </p> <p>Load the image</p><pre class="codeinput">n = 128;M = load_image(<span class="string">'lena'</span>);M = rescale(crop(M,n));y = M;y(mask==1) = 0;</pre><p>We use iterative thresholding to find the sparse wavelet coefficients <tt>MW</tt> of the image. Since there is no noise, the threshold is progressively decayed to 0 during the iterations. </p><pre class="codeinput">niter = 200;options.ti = 1;Jmax = log2(n)-1;Jmin = Jmax-2;Tlist = linspace(.1,0, niter);Mspars = Msob;MW = perform_wavelet_transf(Mspars,Jmin,+1,options);MWsob = perform_wavelet_transf(Msob,Jmin,+1,options);err = [];<span class="keyword">for</span> i=1:niter <span class="comment">% residual R = wav( y - PhiWAV(MW) ) )</span> R = y - perform_wavelet_transf(MW,Jmin,-1,options); R(mask==1) = 0; MW = MW + perform_wavelet_transf(R,Jmin,+1,options); MW = perform_thresholding( MW, Tlist(i), <span class="string">'soft'</span> ); MW(:,:,1) = MWsob(:,:,1); <span class="comment">% reconstruct</span> Mspars = perform_wavelet_transf(MW,Jmin,-1,options); err(i) = snr(M,Mspars);<span class="keyword">end</span>clf;imageplot(clamp(Msob), strcat([<span class="string">'Sobolev, SNR='</span> num2str(snr(M,Msob))]), 1,2,1);imageplot(clamp(Mspars), strcat([<span class="string">'Sparsity, SNR='</span> num2str(snr(M,Mspars))]), 1,2,2);</pre><img vspace="5" hspace="5" src="index_08.png"> <p class="footer"><br> Copyright ® 2008 Gabriel Peyre<br></p> </div> <!--##### SOURCE BEGIN #####%% Variational Inpainting% This numerical tour explores the use of variational energies (Sobolev,% Total variation and sparsity) to regularize the image inpaiting problem.%% Installing toolboxes and setting up the path.%%% You need to download the % <../toolbox_general.zip general purpose toolbox>% and the <../toolbox_signal.zip 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 Scilabe) or |numericaltour.m| to write all the% Scilab/Matlab command 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%%% Then you can add these toolboxes to the path.% Add some directories to the pathgetd('toolbox_signal/');getd('toolbox_general/');%% Missing Pixels and Inpaiting% Inpainting corresponds to filling holes in images.%%% First we load the image to be inpainted.n = 128;M = load_image('lena');M = rescale(crop(M,n));%%% Then we construct a mask by removing random pixels.% amount of removed pixels.rho = .7;% random mask, mask==1 for removed pixelsmask = zeros(n,n);sel = randperm(n^2); sel = sel(1:round(rho*n^2));mask(sel) = 1;%%% Damaged observation (no noise)% remove pixelsy = M; y(mask==1) = 0;% displayclf;imageplot(M, 'Image M', 1,2,1);imageplot(y, 'Observation y', 1,2,2);%% Inpainting by Blurring% A simple way to recover the missing values of the image is simply to perform a blurring.% To progressively fill the hole, the blurring must be repeated.%%% A simple way to remove the hole is simply to perform a blurring.Mb = y;% put average value inside the holeMb(mask==1) = mean(y(mask==0));% blur the whole images = 3; % number of pixelsMb = perform_blurring(Mb,3);% impose the known valuesMb(mask==0) = y(mask==0);%%% You can iterate this several time.niter = 100;err = [];for i=1:niter % blur the whole image Mb = perform_blurring(Mb,s); % impose the known values Mb(mask==0) = y(mask==0); % compute the error err(i) = snr(M, Mb);end% display error decayclf;plot(1:niter, err);set_label('#iteration', 'SNR');%% % Display.clf;imageplot(y, 'Obervations y', 1,2,1);imageplot(Mb, strcat(['Inpainted, SNR=' num2str(snr(M,Mb),3) 'dB']), 1,2,2); %% Sobolev Impainting% The simplest regularization is simply the Sobolev |E(M) = \int grad(M)^2|% energy. Its gradient is the Laplacian, and the gradient descent% corresponds to an iterated blurring with a small 3x3 mask.%%% The gradient of the Dirichlet energy is simply the Laplacian.% A step of gradient descent is computing y+tau*Laplacian, which perform a% small blurring.% use centered differences with reflecting boundary conditionsoptions.bound = 'sym';options.order = 1;% laplacianLM = div(grad(M,options),options);Ly = div(grad(y,options),options);% displayclf;imageplot(LM, 'Laplacian of M', 1,2,1);imageplot(Ly, 'Laplacian of y', 1,2,2);%%% _Exercice 1:_ (the solution is <../private/inverse_inpainting/exo1.m exo1.m>)% Perform the inpainting by a projected gradient descent. After each step% of descent, you obtain a solution |Msob| that is projected on the% constraint by setting |Msob(mask==0)=y(mask==0)|. Keep track of the% Dirichlet energy. The gradient step size must be less than 1/4,% according to the CLF condition.exo1;%%% Display the resultclf;imageplot(y, 'Obervations y', 1,2,1);imageplot(Mb, strcat(['Inpainted, SNR=' num2str(snr(M,Msob),3) 'dB']), 1,2,2); %%% We load another image with a non-random mask.n = 256;M = load_image('parrot', n);if nb_dims(M)==3 % flatten color images M = sum(M,3);endM = rescale( M );mask1 = load_image('parrot-mask', n);mask1 = double(rescale(mask1)<.5);%%% _Exercice 2:_ (the solution is <../private/inverse_inpainting/exo2.m exo2.m>)% Perform Sobolev inpainting on the image 'parrot' using as mask the binary image% 'perrot-mask'. What do notice ?exo2;%% Inpainting with Sparsity% A non-linear prior replaces the Sobolev energy by the L1 norm of the% wavelet coefficients in a translation invariant wavelet tight frames.%%% Load the imagen = 128;M = load_image('lena');M = rescale(crop(M,n)); y = M;y(mask==1) = 0;%%% We use iterative thresholding to find the sparse wavelet coefficients% |MW| of the image. Since there is no noise, the threshold is% progressively decayed to 0 during the iterations.niter = 200;options.ti = 1;Jmax = log2(n)-1;Jmin = Jmax-2;Tlist = linspace(.1,0, niter);Mspars = Msob;MW = perform_wavelet_transf(Mspars,Jmin,+1,options);MWsob = perform_wavelet_transf(Msob,Jmin,+1,options);err = [];for i=1:niter % residual R = wav( y - PhiWAV(MW) ) ) R = y - perform_wavelet_transf(MW,Jmin,-1,options); R(mask==1) = 0; MW = MW + perform_wavelet_transf(R,Jmin,+1,options); MW = perform_thresholding( MW, Tlist(i), 'soft' ); MW(:,:,1) = MWsob(:,:,1); % reconstruct Mspars = perform_wavelet_transf(MW,Jmin,-1,options); err(i) = snr(M,Mspars);endclf;imageplot(clamp(Msob), strcat(['Sobolev, SNR=' num2str(snr(M,Msob))]), 1,2,1);imageplot(clamp(Mspars), strcat(['Sparsity, SNR=' num2str(snr(M,Mspars))]), 1,2,2);##### SOURCE END #####--> </body></html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -