File Exchange

## Inpaint over missing data in 1-D, 2-D, 3-D,... ND arrays

version 1.4.2 (11.4 KB) by

Y = INPAINTN(X) computes the missing data in the N-D array X.

4.90909
25 Ratings

Updated

Y = INPAINTN(X) replaces the missing data in X by extra/interpolating the non-missing elements. The non finite values (NaN or Inf) in X are considered as missing data. X can be any N-D array.
Type "help inpaintn" in the Matlab command windows for several examples.
INPAINTN (no input/output argument) runs a 3-D example.

Important note:
----------------
INPAINTN uses an iterative process that converges toward the solution. Y = INPAINTN(X,N) uses N iterations. By default, N = 100. If you estimate that INPAINTN did not totally converge, then increase N: Y = INPAINTN(X,1000);

-----
When using this algorithm, please refer to these 2 papers:

1) Garcia D. Robust smoothing of gridded data in one and higher dimensions with missing values.
Comput Statist Data Anal, 2010;54:1167-1178
http://www.biomecardio.com/publis/csda10.pdf

2) Wang G, Garcia D et al. A three- dimensional gap filling method for large geophysical datasets: Application to global satellite soil moisture observations.
Environ Modell Softw, 2012;30:139-142.
http://www.biomecardio.com/publis/envirmodellsoftw12.pdf.pdf
-----

A series of examples is available here:
http://www.biomecardio.com/matlab/inpaintn_doc.html

Example:
--------
%% ---- 2-D data ---- %%
n = 256;
y0 = peaks(n);
y = y0;
I = randperm(n^2);
y(I(1:n^2*0.5)) = NaN; % lose 1/2 of data
y(40:90,140:190) = NaN; % create a hole
z = inpaintn(y,200); % inpaint data
subplot(2,2,1:2), imagesc(y), axis equal off
title('Corrupt data')
subplot(223), imagesc(z), axis equal off
title('Recovered data ...')
subplot(224), imagesc(y0), axis equal off
title('... compared with original data')

------
http://www.biomecardio.com
-----

zhengwei yang

it's working!

sal

### sal (view profile)

Ajey Venkataraman

Derya

### Derya (view profile)

Works excellent. Thank you!

Shahar Barak

### Shahar Barak (view profile)

I just downloaded this and when I try to run it, I get the following message:
To use 'bwdist', the following product must be both licensed and installed:
Image Processing Toolbox

Error in inpaintn>InitialGuess (line 177)
[~,L] = bwdist(I);

Error in inpaintn (line 142)
[y,s0] = InitialGuess(x,isfinite(x));

I ma using Matlab R2016b

Bao Thai

### Bao Thai (view profile)

Not sure why does not work for me

Jayasankar T Pillai

### Jayasankar T Pillai (view profile)

Thanks for the code, great work

Dipankar Kumar

Great work.

Matt Molteno

### Matt Molteno (view profile)

Great submission!
If you have BWDIST license checkout problems.
It is probably because the current test does not check if all of the licenses are checked out.

Simply change:
line 175>> if license('test','image_toolbox')
to
line 175>> if license('checkout','image_toolbox')

Farah Bazzi

### Farah Bazzi (view profile)

Thank you so much for such a successful code !!

Kaushik Sampath

Oron Nir

KSSV

### KSSV (view profile)

Amazing piece of code. Good documentation. Also available with reference papers. Worth the code Damien Garcia.

Eric P

### Eric P (view profile)

Thank you for this, it is a great function that was just what I needed to fill in the spaces in a bathymetric DEM of Krakatau. It ran so quickly on my 481 x 601 matrix that I was surprised! Very easy to use.

Eric P

Keith

### Keith (view profile)

Great work, been running well for me until I ran into an issue today. I received the error code:

??? Subscript indices must either be real positive integers or logicals.

Error in ==> inpaintn>InitialGuess at 179
z(~I) = y(L(~I));

Error in ==> inpaintn at 142
[y,s0] = InitialGuess(x,isfinite(x));

Error in ==> test at 32
betas_paint2(:,k) = inpaintn(betas_paint(:,k));

Was trying to run inpaintn through an external loop feeding it vectors. It made it through the first 8 then gave me an error. Any help is appreciated.

Ryan Muir

### Ryan Muir (view profile)

This is a great program! It is easy to use, and is really impressively fast.

I was using it for 3D and 4D interpolation of rather large problems, and was taking hours per image to process. I found that I could offload much of the problem to the GPUs, and get an order of magnitude more speed with a couple of lines of extra code! It's a bit sloppy, but putting y = gpuArray(y) near the beginning of the cdtn() and icdtn() functions bought me this performance enhancement. There are likely more efficient and elegant ways of sending it to the GPU, though this way worked great in a pinch. Small problems (including the example problem) run a bit slower with this modification, so this really is for large problems only.

Jeff

### Jeff (view profile)

I'm incorrect. Apologies.

Although I had downloaded Damien's new version of inpaintn, I had earlier used the older version (that I had neglected to delete), in a folder that put it ahead of the new version.

Jeff

### Jeff (view profile)

The call to test4DCTNANDIDCTN can be eliminated because the functions dctn and idctn are now included in the inpaintn.m file.

Otherwise, works well. Results in my application compare reasonably with John D'Errico's inpaint_nans3, and is faster.

Marc Crapeau

### Marc Crapeau (view profile)

Very good submission. I was working on my own version of this problem when a google research send me here and save me several hours of work. Thanks!

Shrinivas

### Shrinivas (view profile)

I am getting error
??? Error: File: inpaintn.m Line: 176 Column: 7
Expression or statement is incorrect--possibly unbalanced (, {, or [.

Scott Richardson

### Scott Richardson (view profile)

Hi, thank you for sharing this.

Do you happen to have onion.png, and wind.mat to try the other examples?

yo

### yo (view profile)

hey everyone, the looks great, but unfotunately i've been getting the following error:

Error in ==> inpaintn at
140
y =
RF*idctn(Gamma.*dctn(W.*(x-y)+y))
+ (1-RF)*y;

since i don't feel capable enpugh changing the code on my own, can someone tell me what went wrong?

Thanks alot,

yoav

Kyle

### Kyle (view profile)

Very useful script. I will never use matlab extrapolation routines ever again.

Kyle

tudor dima

### tudor dima (view profile)

Great work, extremely useful, it works out of the box.
Thanks a lot for posting it!

Would there be a simple way to restrict calculations inside a "gating mask"?

Bohuslav Ruzek

### Bohuslav Ruzek (view profile)

I had to interpolate missing 3D geophysical data in order to enable standard filtration process. Inpaint code worked very well for this, the results look pretty well. Using inpaint is easy.
I recommend using this script also for other interested users.

Leonardo

### Leonardo (view profile)

Very useful, works nicely. The only issue is that a couple of additional functions (dctn and idctn) have to be downloaded from another site for this to work