2

I have an image in which some pixels were changed to zero intentionally. Now I want to make a bilinear interpolation to find out new pixel values using the neighborhood as the bilinear interpolation does. However, I don't want to resize the image (MATLAB can do bilinear interpolation only with resize function).

Is it possible to make a bilinear interpolation in MATLAB without resizing? I read that a convolution with the bilinear kernel would solve this. Do you know which kernel is this? is it possible to do what I want?

mad
  • 2,677
  • 8
  • 35
  • 78
  • 1
    You might find [inplant_nans](https://uk.mathworks.com/matlabcentral/fileexchange/4551-inpaint-nans) useful. Instead of 0s make these pixels NaN, and you should be able to use the function. – Vahe Tshitoyan Sep 01 '17 at 09:01
  • Thank you so much for your comment. However, I am implementing an image processing approach that asks me to use bicubic or bilinear interpolation. Does the code you suggested perform these interpolation techniques? – mad Sep 01 '17 at 09:27
  • 1
    I don't think it has those specific methods unfortunately, I just use it myself a lot for image interpolations and it is quite good. If you have that specific requirement you might want to look somewhere else. – Vahe Tshitoyan Sep 01 '17 at 09:31
  • 1
    Please see this related [answer](https://stackoverflow.com/a/12429348/7906900). In short, you can use inplant_nans ([methods overview](https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/4551/versions/2/previews/Inpaint_nans/inpaint_nans.m/index.html?access_key=) )or TriScatteredInterp/scatteredInterpolant, both support linear interpolation and do not support cubic. – Gryphon Sep 01 '17 at 10:26
  • 1
    What does it mean "without resizing"? what do you want to interpolate, if you dont create new values? This needs a bit more explanation. interp2 does various types of interpolation – Ander Biguri Sep 01 '17 at 17:06
  • 2
    @AnderBiguri They want to replace the 0 values with the values found by interpolating their neighbors. – beaker Sep 01 '17 at 17:51
  • 1
    @beaker right, but that is not really interpolation nor you can just use "bilinear" for this, right? what are the neighbours? You can use just 4 points for bilinear. What if the zero is in a corner? etc etc – Ander Biguri Sep 01 '17 at 17:54

1 Answers1

4

You can try to use one of the options supported by griddata:

griddata(..., METHOD) where METHOD is one of
    'nearest'   - Nearest neighbor interpolation
    'linear'    - Linear interpolation (default)
    'natural'   - Natural neighbor interpolation
    'cubic'     - Cubic interpolation (2D only)
    'v4'        - MATLAB 4 griddata method (2D only)
defines the interpolation method. The 'nearest' and 'linear' methods 
have discontinuities in the zero-th and first derivatives respectively, 
while the 'cubic' and 'v4' methods produce smooth surfaces.  All the 
methods except 'v4' are based on a Delaunay triangulation of the data.

Example

% create sample data
[X, Y] = meshgrid(1:10, 1:10);
Z_original = X.*Y;

% remove a data point
Z_distorted = Z_original;
Z_distorted(5, 5) = nan;

% reconstruct
valid = ~isnan(Z_distorted);
Z_reconstructed = Z_distorted;
Z_reconstructed(~valid) = griddata(X(valid),Y(valid),Z_distorted(valid),X(~valid),Y(~valid));

% plot the result
figure
surface(Z_original);

figure
surface(Z_distorted);

figure
surface(Z_reconstructed);

enter image description here enter image description here

m7913d
  • 10,244
  • 7
  • 28
  • 56
  • In your example, you have matrices X, Y, and Z, and Z is formed by X and Y. I have only one matrix Z with 1500x1500x3 dimensions. How can I split my Z matrix in matrices X and Y as in your example? – mad Sep 04 '17 at 00:46
  • `[X, Y] = meshgrid(1:1500, 1:1500)`. Note that X, Y are the coordinates for each point where you evaluated Z. You probably want to interpolate the third dimension independently. – m7913d Sep 04 '17 at 08:03