4.87097

4.9 | 32 ratings Rate this file 187 downloads (last 30 days) File Size: 269.05 KB File ID: #4551

inpaint_nans

by John D'Errico

 

29 Feb 2004 (Updated 28 Jun 2006)

Code covered by BSD License  

Interpolates (& extrapolates) NaN elements in a 2d array.

Editor's Notes:


This is a File Exchange Select file.

Select files are submissions that have been peer-reviewed and approved as meeting a high standard of utility and quality.

Download Now | Watch this File

File Information
Description

Interpolate NaN elements in a 2-d array using non-NaN elements. Can also extrapolate, as it does not use a triangulation of the data. Inpaint_nans offers several different approaches to the interpolation, which give tradeoffs in accuracy versus speed and memory required. All the methods currently found in inpaint_nans are based on sparse linear algebra and PDE discretizations. In essence, a PDE is solved to be consistent with the information supplied.

Acknowledgements
This submission has inspired the following:
Inpainting nan elements in 3-d
MATLAB release MATLAB 5.2 (R10)
Other requirements Written in version 5.2, tested up to release 14 SP1
Zip File Content  
Published M Files inpaint_nans_demo
Other Files
Inpaint_nans/.DS_Store,
Inpaint_nans/garden50.jpg,
Inpaint_nans/inpaint_nans.m,
Inpaint_nans/monet_adresse.jpg,
Inpaint_nans/demo/.DS_Store,
Inpaint_nans/demo/inpaint_nans_demo.m,
Inpaint_nans/doc/Nomination comments.rtf,
Inpaint_nans/doc/methods_of_inpaint_nans.m,
Inpaint_nans/test/.DS_Store,
Inpaint_nans/demo/html/inpaint_nans_demo_05.png,
Inpaint_nans/demo/html/inpaint_nans_demo_06.png,
Inpaint_nans/demo/html/inpaint_nans_demo.png,
Inpaint_nans/demo/html/inpaint_nans_demo_01.png,
Inpaint_nans/demo/html/inpaint_nans_demo_02.png,
Inpaint_nans/demo/html/inpaint_nans_demo_03.png,
Inpaint_nans/demo/html/inpaint_nans_demo_04.png,
Inpaint_nans/test/test_main.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (38)
03 Mar 2004 Urs Schwarz (us)

A very useful and meticulously coded, data-driven function. A must to look at and learn from for anybody who is working with incomplete 2D data sets, in particular, dirty images.

28 Jun 2004 Evan Zamir

Nice! Especially method 4 using the virtual springs.

10 Aug 2004 Tom Reiser

I love it!

29 Oct 2004 d d  
30 Oct 2004 jing tian

excellent!

06 Dec 2004 gok gok

how to handle non-rectangular areas? Triangle for example.
I'm getting holes from DELAUNAY and your routine will be very helpful to fix this bug.

14 Feb 2005 K. Sturm

Well done !

16 Jun 2005 Cris C.

Really a good work. Thank you.

19 Apr 2006 sacrosancttayyar sacrosancttayyar  
21 Apr 2006 Rajeev Srivastava

excellent work.

21 Apr 2006 Ian Howat

very fast, thanks!

31 Aug 2006 Mike Kadour

Just what I was looking for! Many thanks!

13 Oct 2006 mario fiorini

great!

17 Jan 2007 Amar chawla

Great piece of work! really helped....

24 Sep 2007 Emily Spahn

Thank you! This does exactly what I was hoping it would!

31 Oct 2007 martin klöppel

realy useful

30 Dec 2007 Sara Alexander

Really helpful with minimal fuss!

08 Feb 2008 D. G.

Simply excellent. Nothing to declare.

08 Mar 2008 Carlos Adrián Vargas Aguilera

Here you have my 5 stars... Congratulations, and thanks for sharing it...

16 Apr 2008 Bob Field

Magical

13 Aug 2008 E E

Comment=[T H A N NaN Y O U V E NaN Y M NaN NaN H];

[M,N] = size(Comment);
 for k = 1:length(M);
 Comment=inpaint_nans(Comment);
 end

Comment=
         T H A N K Y O U V E R Y M U C H

16 Sep 2008 Sherif Abd El-Gawad

Thanks a lot and Good Contribution

07 Oct 2008 Frederic Moisy

Thank you once again John,
exactly what I needed.

Please TMW: include this in next release of matlab!

16 Dec 2008 M P  
16 Dec 2008 Kenneth Eaton  
19 Dec 2008 M P

The big question is: what is the meaning of del^2?

I've other questions.

Method zero.

First example: my input matrix is A=NaN

A =

   NaN

nn =

   Empty matrix: 0-by-2

??? Improper assignment with rectangular empty matrix.

A matrix 1x1, only one element, is not a 2D matrix in your interpretation?

The error is in "sub2ind" command; it has "nn" in input.

Second example: my input matrix is A=9

A =

     9

neighbors_list =

     []

neighbors_list =

     []

all_list =

     []

??? Index exceeds matrix dimensions.

The error is in the "find" command.

3° example: my input matrix is a 2x2

A =

   NaN 1
   NaN 2

B =

   NaN
   NaN
     1
     2

Warning: Rank deficient, rank = 0.

??? Improper assignment with rectangular empty matrix.

The error is in "fda" command.

4° example: my input matrix is 3x2

A =

   NaN 1
   NaN 2
   NaN 3

B =

   NaN
   NaN
   NaN
     1
     2
     3

Warning: Rank deficient, rank = 1.
 
In this case not error but warning and the row is the same of the 3° case ("fda" command).

5° example: input matrix is 3x3

A =

   NaN 1 1
   NaN 2 2
   NaN 3 3

B =

    1.0000
    2.0000
    3.0000
    1.0000
    2.0000
    3.0000
    1.0000
    2.0000
    3.0000

In this case the output B is ok; my question is: why a triple NaN is just 1 2 3?

6° example: input matrix is 3x3, I'm finding to understand the strategy of NaNs substitution.

A =

   NaN 1 4
   NaN 2 5
   NaN 3 6

B =

    -2
    -1
     0
     1
     2
     3
     4
     5
     6

Ok, program is ok. Not error. Why NaNs value are -2 -1 0?

Other important question: why my input A (3x3) is now an output B (9x1)?

7° example: input matrix 10x10 of zeros with only one NaN in (5,5) position.

A =

     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 NaN 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0
 
B =

     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0

Output matrix is a vector 100x1 with all zeros. Why NaN is just zero?

8° example: like the 7° but with A of ones

A =

     1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1
     1 1 1 1 NaN 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 1 1 1

B =

     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1
     1

I've obtained NaN=1.

So I try with an image, because in the image's file there are 3 images.

9° example: matrix 3x3x3

A(:,:,1) =

   NaN 1 4
   NaN 2 5
   NaN 3 6

A(:,:,2) =

   NaN 1 4
   NaN 2 5
   NaN 3 6

A(:,:,3) =

   NaN 1 4
   NaN 2 5
   NaN 3 6

B =

   -2.0000 <-- why negative?
   -1.0000 <-- why negative?
         0 <-- why?
    1.0000
    2.0000
    3.0000
    4.0000
    5.0000
    6.0000
    2.5000 <-- why .5?
    3.5000 <-- why .5?
    4.5000 <-- why .5?
    1.0000
    2.0000
    3.0000
    4.0000
    5.0000
    6.0000
    2.5000 <-- why?
    3.5000 <-- why?
    4.5000 <-- why?
    1.0000
    2.0000
    3.0000
    4.0000
    5.0000
    6.0000

In your vision an image is 2D?
I think is affirmative answer because program is in 2D but if is so the triple color-profundity is a 3=1 formula?
How is possible to show an image if NaN is transformed in a number with decimal numbers?
NaN value is or is not a number? If yes, then I can put in the NaN position any other number?
NaN is 0/0, is certainly a number but PC don't say what number is it. Then is all numbers, in my vision.
Is possible it is 1 but also 2 and 3 etc etc.

...
NaN=-1 --> N=2
NaN=0 --> N=1 --------> this is half X
NaN=1 --> N=0 --------> this is the other half X
NaN=2 --> N=-1
NaN=3 --> N=-2
...
NaN=10 --> N=-9
...

N=Number is my input. And NaN is a natural bent of PC.
Say to me, if you want.
Regards
CU

19 Dec 2008 V. Poor  
19 Dec 2008 alain boyer  
19 Dec 2008 Kenneth Eaton

@Marco,

You appear to have completely missed the point of this function. It is designed to REMOVE NaNs appearing in a matrix, overwriting those values with interpolated values obtained from neighboring entries. You seem to think that John is arbitrarily redefining what NaN is... he is not, because that would be a silly waste of time. The numbers he computes to REPLACE the NaNs are approximated from the surrounding values with a number of different methods.

As far as some of the errors you got, John could probably place error checks in the function to allow for a more graceful and informative exit. However, there's no reason you would/should ever enter some of those arguments in the first place. For example, entering a scalar NaN is silly, since the program has no nearby non-NaN elements to use for extrapolating replacement values.

This submission certainly doesn't deserve the low rating you gave it, especially since the misunderstanding lies with your own misinterpretation of what it does.

19 Dec 2008 M P

"is arbitrarily redefining what NaN is"... no, I don't think this. I would understand what is "neighboring" strategy; if I've an image with four azure points and I put a NaN in one of this points the program answer is the transformation of NaN in an azure point or a near color azure? The NaN point with "image" command is a black point and an image with 3 azure points and a black point is an original image so. If I change the NaN point with a number I can choose a value in the Inf sector of the values. This I say. And I repeat the question: what is neighboring strategy? Is del^2. Can I have more informations?
"surrounding values"..... surround=margin..... can you write one of my examples with explanations?
"different methods".... I'm speacking of zero method, the default method
"entering a scalar NaN is silly".... why? Or also, what number is not-silly? Take an example.
"misunderstanding lies with your own misinterpretation of what it does".... lies? where are?
Interpretation..... the comment is genuine

19 Dec 2008 Kenneth Eaton

@Marco,

If you want more specific information about the methods than what is in the help documentation, you would have to either delve into the code itself or have John explain it to you.

Also, the word "lies" is a conjugation of the verb "to lay", not a noun meaning "falsehoods". To rewrite my statement in another way: "...the misunderstanding is a result of your own misinterpretation."

14 Jan 2009 C Schwalm

John, I saw in your zip file how you are thinking of removing method 5. Let me make a counter suggestion: expand it. I can think many of reasons and applications where a neighborhood average would be useful. In terms of expansion: Well, you could allow for the size of the neighborhood, its orientation (forward, backward, center), and the function (sum, mean, var, mode, etc.) to be passed as additional arguments. Either way, this function is very useful and I use it often in place of standard Matlab functions.

24 Mar 2009 Karel  
28 Mar 2009 Stuart  
25 May 2009 Stefan  
25 May 2009 Stefan

Hello, I recently started studying about inpainting and i ran across Mr. D'Errico's program and I have to say it's pretty cool, tried it on a few pics of mine and i wase pleased with the results.
I need some help about understanding the basic theory on the subject, how the elliptic PDE's are implemented in inpainting etc. and if anyone could give me a hint on where i could find some books, documents on the matter i would be very grateful. Regards

09 Jun 2009 Andrey Rubshtein  
28 Sep 2009 Jon  
Please login to add a comment or rating.
Updates
18 Jul 2005

This version of inpaint_nans will also handle
inpainting (interpolating/extrapolating) simple
vectors with NaN elements.

17 Apr 2006

Release 2:
Bug fix for method 5. Clean up help. Add HTML, screenshot, examples, demo comparison to griddata.

28 Jun 2006

Created Select directory structure

Tag Activity for this File
Tag Applied By Date/Time
approximation John D'Errico 22 Oct 2008 07:15:13
interpolation John D'Errico 22 Oct 2008 07:15:13
extrapolates John D'Errico 22 Oct 2008 07:15:13
nan John D'Errico 22 Oct 2008 07:15:13
triangulation John D'Errico 22 Oct 2008 07:15:13
approximation Patrick HannaSecure 31 Oct 2008 14:25:15
extrapolates Patrick HannaSecure 31 Oct 2008 14:25:21
inpaint Oliver Woodford 26 Jan 2009 19:02:25
file exchange select Oliver Woodford 27 Jan 2009 08:05:11
extrapolates Seth 18 Jun 2009 14:58:49
approximation Seth 18 Jun 2009 14:58:52
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com