inpaintTV

PURPOSE ^

SOBOLEV Inpainting of an "image" by iterative minimization of the Total

SYNOPSIS ^

function imgInpainted = inpaintTV(img, mask, epsilon, relTolerance, maxIters, outDemoVideo)

DESCRIPTION ^

SOBOLEV Inpainting of an "image" by iterative minimization of the Total
   Variation energy

 Input:
   img: input image to be inpainted
   mask: logical mask of the same size as the input image. 1 == known pixels, 0 == unknown pixels to be
         inpainted
   relTolerance: relative tolerance, stop the gradient descent when the energy descent between iterations is less than this value
   numIters: maximum number of gradient descent iterations to perform (default = 100000)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function imgInpainted = inpaintTV(img, mask, epsilon, relTolerance, maxIters, outDemoVideo)
0002 %SOBOLEV Inpainting of an "image" by iterative minimization of the Total
0003 %   Variation energy
0004 %
0005 % Input:
0006 %   img: input image to be inpainted
0007 %   mask: logical mask of the same size as the input image. 1 == known pixels, 0 == unknown pixels to be
0008 %         inpainted
0009 %   relTolerance: relative tolerance, stop the gradient descent when the energy descent between iterations is less than this value
0010 %   numIters: maximum number of gradient descent iterations to perform (default = 100000)
0011 
0012 % Input parameters check
0013 if nargin < 3 || isempty(epsilon)
0014     epsilon = 1e-2;
0015 end
0016 if nargin < 4 || isempty(relTolerance)
0017     relTolerance = 1e-8;
0018 end
0019 if nargin < 5 || isempty(maxIters)
0020     maxIters = 100000;
0021 end
0022 createDemoVideo = false;
0023 if nargin > 5 && ~isempty(outDemoVideo)
0024    % Create the output video
0025    createDemoVideo = true;
0026    video = VideoWriter(outDemoVideo,'Motion JPEG AVI');
0027    video.Quality = 100;
0028    open(video);
0029    itersPerFrame = 10;
0030 end
0031 
0032 % Functors
0033 Pi = @(f)f.*(1-mask) + img.*mask;
0034 Amplitude = @(u)sqrt(sum(u.^2,3)+epsilon^2);
0035 Neps = @(u)u./repmat(Amplitude(u), [1 1 2]);
0036 G = @(f)-div(Neps(grad(f)));
0037 
0038 % Gradient descent step size
0039 tau = .9*epsilon/4;
0040 
0041 % Gradient descent iterations
0042 imgInpainted = img;
0043 energyPrev = 0;
0044 for i=1:maxIters
0045     energy = sum(sum(Amplitude(grad(imgInpainted))));    
0046     imgInpainted = Pi( imgInpainted - tau*G(imgInpainted) );    
0047     
0048     if abs(energy-energyPrev) < relTolerance
0049         break;
0050     end
0051     energyPrev = energy;
0052     
0053      % Save a frame of the video
0054     if createDemoVideo && mod(i-1, itersPerFrame) == 0
0055         imagesc(imgInpainted'); axis xy; colorbar;
0056         frame = getframe(gcf);        
0057         writeVideo(video, frame);
0058     end
0059 end
0060 
0061 if i == maxIters
0062     warning('Maximum number of iterations reached');
0063 end
0064 
0065 if createDemoVideo, close(video); end
0066 
0067 end

Generated on Thu 10-Dec-2020 17:34:27 by m2html © 2005