MATLAB Codes for the Image Inpainting Problem
All the scripts provided are used in Partial Differential Equation Methods for Image Inpainting (Carola-Bibiane Schoenlieb, Cambridge University Press, 2015).
Authors: Simone Parisotto (email: sp751 at cam dot ac dot uk) Carola-Bibiane Schoenlieb (email: cbs31 at cam dot ac dot uk)
Address: Cambridge Image Analysis Centre for Mathematical Sciences Wilberforce Road CB3 0WA, Cambridge, United Kingdom
Date: September, 2016
Last update: October, 2018
Licence: BSD-3-Clause (https://opensource.org/licenses/BSD-3-Clause)
Contents
How to cite this work
Please use the following entry to cite this code:
@Misc{MATLABinpainting2016,
70 | author = {Parisotto, Simone and Sch\"{o}nlieb, Carola},
71 | title = {MATLAB Codes for the {Image} {Inpainting} {Problem}},
72 | howpublished = {GitHub repository, {MATLAB} Central File Exchange},
73 | month = {September},
74 | year = {2016}
75 | }
1) AMLE Inpainting (Absolute Minimizing Lipschitz Extension Inpainting)
Function used to reproduce Figure 4.10 in Partial Differential Equation Methods for Image Inpainting (Carola-Bibiane Schoenlieb, Cambridge University Press, 2015).
cleanfilename = 'amle_clean.png';
76 |
amle_clean.png :
maskfilename = 'amle_mask.png';
77 |
amle_mask.png :
[u,mask] = create_image_and_mask(cleanfilename,maskfilename);
78 | imwrite(u,'./dataset/amle_input.png')
79 |
amle_input.png :
80 | lambda = 10^2;
81 | tol = 1e-8;
82 | maxiter = 40000;
83 | dt = 0.01;
84 |
85 |
86 | inpainting_amle(u,mask,lambda,tol,maxiter,dt);
87 |
Result image:
Tic/Toc time: Elapsed time is 34.93 seconds.
See the code inpainting_amle.m.
Bibliography
- Caselles, V., Morel, J. M., & Sbert, C. (1998). An axiomatic approach to image interpolation. Image Processing, IEEE Transactions on, 7(3), 376-386.
- Almansa, A. (2002). Echantillonnage, interpolation et detection: applications en imagerie satellitaire (Doctoral dissertation, Cachan, Ecole normale superieure).
2) Harmonic Inpainting
Function used to reproduce Figure 2.2 in Partial Differential Equation Methods for Image Inpainting (Carola-Bibiane Schoenlieb, Cambridge University Press, 2015).
cleanfilename = 'harmonic_clean.png';
88 |
harmonic_clean.png :
maskfilename = 'harmonic_mask.png';
89 |
harmonic_mask.png :
[u,mask] = create_image_and_mask(cleanfilename,maskfilename);
90 | imwrite(u,'./dataset/harmonic_input.png')
91 |
harmonic_input.png :
92 | lambda = 10;
93 | tol = 1e-5;
94 | maxiter = 500;
95 | dt = 0.1;
96 |
97 |
98 | inpainting_harmonic(u,mask,lambda,tol,maxiter,dt);
99 |
Result image:
Tic/Toc time: Elapsed time is 2.14 seconds.
See the code inpainting_harmonic.m.
Bibliography
- Shen, J., & Chan, T. F. (2002). Mathematical models for local nontexture inpaintings. SIAM Journal on Applied Mathematics, 62(3), 1019-1043.
3) Mumford-Shah Inpainting
Function used to reproduce Figure 7.3 in Partial Differential Equation Methods for Image Inpainting (Carola-Bibiane Schoenlieb, Cambridge University Press, 2015).
cleanfilename = 'mumford_shah_clean.png';
100 |
mumford_shah_clean.png :
maskfilename = 'mumford_shah_mask.png';
101 |
mumford_shah_mask.png :
[u,mask] = create_image_and_mask(cleanfilename,maskfilename);
102 | imwrite(u,'./dataset/mumford_shah_input.png')
103 |
mumford_shah_input.png :
104 | maxiter = 20;
105 | tol = 1e-14;
106 | param.lambda = 10^9;
107 | param.alpha = 1;
108 | param.gamma = 0.5;
109 | param.epsilon = 0.05;
110 |
111 |
112 | inpainting_mumford_shah(u,mask,maxiter,tol,param);
113 |
Result image:
Tic/Toc time: Elapsed time is 48.99 seconds.
See the code inpainting_mumford_shah.m.
Bibliography
- Esedoglu, S., & Shen, J. (2002). Digital inpainting based on the Mumford-Shah-Euler image model. European Journal of Applied Mathematics, 13(04), 353-370.
4) Cahn-Hilliard Inpainting
Function used to reproduce Figure 5.9 in Partial Differential Equation Methods for Image Inpainting (Carola-Bibiane Schoenlieb, Cambridge University Press, 2015).
cleanfilename = 'cahn_hilliard_clean.png';
114 |
cahn_hilliard_clean.png :
maskfilename = 'cahn_hilliard_mask.png';
115 |
cahn_hilliard_mask.png :
[u,mask] = create_image_and_mask(cleanfilename,maskfilename);
116 | imwrite(u,'./dataset/cahn_hilliard_input.png')
117 |
cahn_hilliard_input.png :
118 | maxiter = 4000;
119 | param.epsilon = [100 1];
120 | param.lambda = 10;
121 | param.dt = 1;
122 |
123 |
124 | inpainting_cahn_hilliard(u,mask,maxiter,param);
125 |
Result image:
Tic/Toc time: Elapsed time is 8.35 seconds.
See the code inpainting_cahn_hilliard.m.
Bibliography
- Bertozzi, A., Esedoglu, S. & Gillette, A. (2007). Inpainting of binary images using the Cahn-Hilliard equation, IEEE Transactions on image processing 16.1 pp. 285-291 (2007).
- Schoenlieb, C.-B. & Bertozzi, A. (2011). Unconditionally stable schemes for higher order inpainting, Communications in Mathematical Sciences, Volume 9, Issue 2, pp. 413-457 (2011).
5) Transport Inpainting
Function used to reproduce Figure 6.1 in Partial Differential Equation Methods for Image Inpainting (Carola-Bibiane Schoenlieb, Cambridge University Press, 2015).
cleanfilename = 'transport_clean.png';
126 |
transport_clean.png :
maskfilename = 'transport_mask.png';
127 |
transport_mask.png :
[u,mask] = create_image_and_mask(cleanfilename,maskfilename);
128 | imwrite(u,'./dataset/transport_input.png')
129 |
transport_input.png :
130 | tol = 1e-5;
131 | maxiter = 50;
132 | dt = 0.1;
133 | param.M = 40;
134 | param.N = 2;
135 | param.eps = 1e-10;
136 |
137 |
138 | inpainting_transport(u,mask,maxiter,tol,dt,param);
139 |
Results image:
Tic/Toc time: Elapsed time is 151.69 seconds.
See the code inpainting_transport.m.
Bibliography
- Bertalmio, M. (2001). Processing of flat and non-flat image information on arbitrary manifolds using partial differential equations.PhD Thesis, 2001.
M-file that created this page
demo_html.m
--------------------------------------------------------------------------------
/matlab/html/harmonic_clean.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/harmonic_clean.png
--------------------------------------------------------------------------------
/matlab/html/harmonic_input.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/harmonic_input.png
--------------------------------------------------------------------------------
/matlab/html/harmonic_mask.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/harmonic_mask.png
--------------------------------------------------------------------------------
/matlab/html/harmonic_output.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/harmonic_output.png
--------------------------------------------------------------------------------
/matlab/html/mumford_shah_clean.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/mumford_shah_clean.png
--------------------------------------------------------------------------------
/matlab/html/mumford_shah_input.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/mumford_shah_input.png
--------------------------------------------------------------------------------
/matlab/html/mumford_shah_mask.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/mumford_shah_mask.png
--------------------------------------------------------------------------------
/matlab/html/mumford_shah_output.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/mumford_shah_output.png
--------------------------------------------------------------------------------
/matlab/html/transport_clean.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/transport_clean.png
--------------------------------------------------------------------------------
/matlab/html/transport_input.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/transport_input.png
--------------------------------------------------------------------------------
/matlab/html/transport_mask.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/transport_mask.png
--------------------------------------------------------------------------------
/matlab/html/transport_output.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/simoneparisotto/MATLAB-Python-inpainting-codes/030bb3d6957a908210ab22522fbacb4166f86212/matlab/html/transport_output.png
--------------------------------------------------------------------------------
/matlab/lib/create_image_and_mask.m:
--------------------------------------------------------------------------------
1 | %% Create data to be inpainted
2 | % part of "MATLAB Codes for the Image Inpainting Problem"
3 | %
4 | % Usage:
5 | % [u,mask,input] = create_image_and_mask(imagefilename,maskfilename)
6 | %
7 | % Authors:
8 | % Simone Parisotto (email: sp751 at cam dot ac dot uk)
9 | % Carola-Bibiane Schoenlieb (email: cbs31 at cam dot ac dot uk)
10 | %
11 | % Address:
12 | % Cambridge Image Analysis
13 | % Centre for Mathematical Sciences
14 | % Wilberforce Road
15 | % CB3 0WA, Cambridge, United Kingdom
16 | %
17 | % Date:
18 | % September, 2016
19 | %
20 | % Licence: BSD-3-Clause (https://opensource.org/licenses/BSD-3-Clause)
21 | %
22 |
23 |
24 | function [u,mask,input] = create_image_and_mask(imagefilename,maskfilename)
25 |
26 | % import a clean input to be corrupted with the mask
27 | input = im2double(imread(imagefilename));
28 |
29 | % import the mask of the inpainting domain
30 | % mask = 1 intact part
31 | % mask = 0 missing domain
32 | mask = double( mat2gray( im2double(imread(maskfilename)) ) == 1 );
33 | if size(mask,3)==1 && size(input,3)>1
34 | mask = repmat(mask,[1,1,size(input,3)]);
35 | end
36 |
37 | % create the image with the missin domain:
38 | noise = rand(size(input));
39 | u = mask.*input + (1-mask).*noise;
--------------------------------------------------------------------------------
/matlab/lib/inpainting_amle.m:
--------------------------------------------------------------------------------
1 | %% AMLE Inpainting
2 | % part of "MATLAB Codes for the Image Inpainting Problem"
3 | %
4 | % Usage:
5 | % u = inpainting_amle(u,mask,lambda,tol,maxiter,dt)
6 | %
7 | % Authors:
8 | % Simone Parisotto (email: sp751 at cam dot ac dot uk)
9 | % Carola-Bibiane Schoenlieb (email: cbs31 at cam dot ac dot uk)
10 | %
11 | % Address:
12 | % Cambridge Image Analysis
13 | % Centre for Mathematical Sciences
14 | % Wilberforce Road
15 | % CB3 0WA, Cambridge, United Kingdom
16 | %
17 | % Date:
18 | % September, 2016
19 | %
20 | % Licence: BSD-3-Clause (https://opensource.org/licenses/BSD-3-Clause)
21 | %
22 |
23 | function u = inpainting_amle(input,mask,lambda,tol,maxiter,dt)
24 |
25 | [M,N,C] = size(input);
26 |
27 | %% GRADIENT
28 | % GRID INTERVAL FOR AXIS ij
29 | h1 = 1; h2 = 1;
30 |
31 | % average upper (u_{i+1,j} - u_{ij})/hx and lower (u_{ij}-u_{i-1,j})/hx
32 | d1i_forward = spdiags([-ones(M,1),ones(M,1)],[0,1],M,M)/h1;
33 | d1i_backward = spdiags([-ones(M,1),ones(M,1)],[-1,0],M,M)/h1;
34 | % average upper (u_{i,j+1} - u_{ij})/hy and lower (u_{ij}-u_{i,j-1})/hy
35 | d1j_forward = spdiags([-ones(N,1),ones(N,1)],[0,1],N,N)/h2;
36 | d1j_backward = spdiags([-ones(N,1),ones(N,1)],[-1,0],N,N)/h2;
37 |
38 | % BACKWARD WITHOUT BOUNDARY CONDITIONS (FORWARD NOT OF INTEREST HERE)
39 | % DD1i_forward = kron(speye(N),d1i_forward);
40 | DD1i_backward = kron(speye(N),d1i_backward);
41 | % DD1j_forward = kron(d1j_forward,speye(M));
42 | DD1j_backward = kron(d1j_backward,speye(M));
43 |
44 | % NEUMANN BOUNDARY CONDITION
45 | d1i_forward(end,:) = 0; d1i_backward(1,:) = 0;
46 | d1j_forward(end,:) = 0; d1j_backward(1,:) = 0;
47 |
48 | % FORWARD AND BACKWARD WITH BOUNDARY CONDITIONS
49 | D1i_forward = kron(speye(N),d1i_forward);
50 | D1i_backward = kron(speye(N),d1i_backward);
51 | D1j_forward = kron(d1j_forward,speye(M));
52 | D1j_backward = kron(d1j_backward,speye(M));
53 |
54 | % CENTERED WITH BOUNDARY CONDITIONS
55 | D1i_centered = (D1i_forward+D1i_backward)/2;
56 | D1j_centered = (D1j_forward+D1j_backward)/2;
57 |
58 | %% FREE MEMORY
59 | clear d1i_forward d1i_backward d1j_forward d1j_backward
60 |
61 | %% INPAINTING ALGORITHM
62 | % INITIALIZATION
63 | u = reshape(input,M*N,C);
64 | input = reshape(input,M*N,C);
65 | mask = reshape(mask,M*N,C);
66 |
67 | v = zeros(M*N,2);
68 |
69 | % FOR EACH COLOR CHANNEL
70 | for c = 1:C
71 |
72 | % ITERATION
73 | for iter = 1:maxiter
74 | ux = D1i_forward*u(:,c); % forward differences along i
75 | uy = D1j_forward*u(:,c); % forward differences along j
76 |
77 | % second derivatives
78 | uxx = DD1i_backward*ux;
79 | uxy = DD1j_backward*ux;
80 | uyx = DD1i_backward*uy;
81 | uyy = DD1j_backward*uy;
82 |
83 | % create direction field Du/|Du| with central differences
84 | v(:,1) = D1i_centered*u(:,c);
85 | v(:,2) = D1j_centered*u(:,c);
86 | % normalize the direction field
87 | v = bsxfun(@rdivide,v,sqrt(sum(v.^2,2)));
88 | v(isnan(v)) = 0;
89 |
90 | % CORE ITERATION
91 | unew = u(:,c) + dt*(uxx.*v(:,1).^2+uyy.*v(:,2).^2 + (uxy+uyx) .* (v(:,1).*v(:,2)) + lambda*mask(:,c).*(input(:,c)-u(:,c)));
92 |
93 | % COMPUTE EXIT CONDITION
94 | diff = norm(unew-u(:,c))/norm(unew);
95 |
96 | % UPDATE
97 | u(:,c) = unew;
98 |
99 | % TEST EXIT CONDITION
100 | if diff