Search code examples
matlabimage-processinginterpolationbicubicbilinear-interpolation

Bilinear and bicubic interpolation of 2D matrix in matlab


I am trying to apply Bilinear and bicubic interpolation on my data set which is stored in text files. These text files are in a folder and are named output_00.text to output_23.text. Each text file consist of three columns. First is Latitude, second is longitude and third column is temperature value at this latitude and longitude (position over earth).

Temperature column contain -9999.000 as not a number or NaN values. This NaN value appears for random rows in each file.

I want to interpolate these NaN values with bilinear/ bicubic interpolation technique.This code will read each text file and interpolate it with bilinear method and save it with method_00.text.

My one text file look like this

21.500  60.500  295.867
21.500  61.500  295.828
21.500  62.500  295.828
21.500  63.500  295.867
21.500  64.500  296.102
21.500  65.500  296.234
21.500  66.500  296.352
21.500  67.500  296.336
21.500  68.500  296.305
21.500  69.500  298.281
21.500  70.500  301.828
21.500  71.500  302.094
21.500  72.500  299.469
21.500  73.500  301.711
21.500  74.500  -9999.000
21.500  75.500  -9999.000
21.500  76.500  -9999.000
21.500  77.500  -9999.000
21.500  78.500  -9999.000
22.500  60.500  295.477
22.500  61.500  295.484
22.500  62.500  295.516
22.500  63.500  295.547
22.500  64.500  295.852
22.500  65.500  295.859
22.500  66.500  295.852
22.500  67.500  295.711
22.500  68.500  295.969
22.500  69.500  298.562
22.500  70.500  300.828
22.500  71.500  302.352
22.500  72.500  300.570
22.500  73.500  301.383
22.500  74.500  -9999.000
22.500  75.500  -9999.000
22.500  76.500  -9999.000
22.500  77.500  -9999.000
22.500  78.500  -9999.000
23.500  60.500  294.906
23.500  61.500  294.898
23.500  62.500  295.000
23.500  63.500  295.078
23.500  64.500  295.297
23.500  65.500  295.359
23.500  66.500  295.297
23.500  67.500  295.312
23.500  68.500  296.664
23.500  69.500  298.781
23.500  70.500  299.211
23.500  71.500  300.109
23.500  72.500  301.000
23.500  73.500  301.594
23.500  74.500  302.000
23.500  75.500  -9999.000
23.500  76.500  -9999.000
23.500  77.500  -9999.000
23.500  78.500  -9999.000
24.500  60.500  294.578
24.500  61.500  294.516
24.500  62.500  294.734
24.500  63.500  294.789
24.500  64.500  294.844
24.500  65.500  294.562
24.500  66.500  294.734
24.500  67.500  296.367
24.500  68.500  297.438
24.500  69.500  298.531
24.500  70.500  298.453
24.500  71.500  299.195
24.500  72.500  300.062
24.500  73.500  -9999.000
24.500  74.500  -9999.000
24.500  75.500  -9999.000
24.500  76.500  -9999.000
24.500  77.500  -9999.000
24.500  78.500  -9999.000
25.500  60.500  296.258
25.500  61.500  296.391
25.500  62.500  296.672
25.500  63.500  296.398
25.500  64.500  295.773
25.500  65.500  295.812
25.500  66.500  296.609
25.500  67.500  297.977
25.500  68.500  297.109
25.500  69.500  297.828
25.500  70.500  298.430
25.500  71.500  298.836
25.500  72.500  298.703
25.500  73.500  -9999.000
25.500  74.500  -9999.000
25.500  75.500  -9999.000
25.500  76.500  -9999.000
25.500  77.500  -9999.000
25.500  78.500  299.023
26.500  60.500  -9999.000
26.500  61.500  298.266
26.500  62.500  296.773
26.500  63.500  -9999.000
26.500  64.500  -9999.000
26.500  65.500  -9999.000
26.500  66.500  297.250
26.500  67.500  296.188
26.500  68.500  295.938
26.500  69.500  296.906
26.500  70.500  297.828
26.500  71.500  299.312
26.500  72.500  299.359
26.500  73.500  -9999.000
26.500  74.500  -9999.000
26.500  75.500  -9999.000
26.500  76.500  -9999.000
26.500  77.500  298.875
26.500  78.500  296.773
27.500  60.500  -9999.000
27.500  61.500  -9999.000
27.500  62.500  -9999.000
27.500  63.500  -9999.000
27.500  64.500  -9999.000
27.500  65.500  -9999.000
27.500  66.500  -9999.000
27.500  67.500  295.352
27.500  68.500  295.148
27.500  69.500  295.750
27.500  70.500  295.750
27.500  71.500  296.070
27.500  72.500  295.227
27.500  73.500  -9999.000
27.500  74.500  -9999.000
27.500  75.500  -9999.000
27.500  76.500  -9999.000
27.500  77.500  -9999.000
27.500  78.500  296.609
28.500  60.500  -9999.000
28.500  61.500  -9999.000
28.500  62.500  -9999.000
28.500  63.500  -9999.000
28.500  64.500  -9999.000
28.500  65.500  -9999.000
28.500  66.500  -9999.000
28.500  67.500  295.773
28.500  68.500  295.375
28.500  69.500  295.438
28.500  70.500  294.664
28.500  71.500  294.906
28.500  72.500  294.812
28.500  73.500  295.805
28.500  74.500  -9999.000
28.500  75.500  -9999.000
28.500  76.500  -9999.000
28.500  77.500  -9999.000
28.500  78.500  -9999.000
29.500  60.500  -9999.000
29.500  61.500  -9999.000
29.500  62.500  -9999.000
29.500  63.500  -9999.000
29.500  64.500  -9999.000
29.500  65.500  -9999.000
29.500  66.500  -9999.000
29.500  67.500  295.719
29.500  68.500  296.797
29.500  69.500  293.375
29.500  70.500  294.305
29.500  71.500  294.070
29.500  72.500  293.750
29.500  73.500  295.539
29.500  74.500  -9999.000
29.500  75.500  -9999.000
29.500  76.500  -9999.000
29.500  77.500  -9999.000
29.500  78.500  -9999.000
30.500  60.500  -9999.000
30.500  61.500  -9999.000
30.500  62.500  -9999.000
30.500  63.500  -9999.000
30.500  64.500  -9999.000
30.500  65.500  -9999.000
30.500  66.500  -9999.000
30.500  67.500  -9999.000
30.500  68.500  -9999.000
30.500  69.500  -9999.000
30.500  70.500  293.320
30.500  71.500  292.930
30.500  72.500  293.570
30.500  73.500  294.648
30.500  74.500  295.383
30.500  75.500  -9999.000
30.500  76.500  -9999.000
30.500  77.500  -9999.000
30.500  78.500  -9999.000
31.500  60.500  -9999.000
31.500  61.500  -9999.000
31.500  62.500  -9999.000
31.500  63.500  -9999.000
31.500  64.500  -9999.000
31.500  65.500  -9999.000
31.500  66.500  -9999.000
31.500  67.500  -9999.000
31.500  68.500  -9999.000
31.500  69.500  -9999.000
31.500  70.500  293.992
31.500  71.500  293.422
31.500  72.500  294.438
31.500  73.500  294.141
31.500  74.500  -9999.000
31.500  75.500  -9999.000
31.500  76.500  -9999.000
31.500  77.500  -9999.000
31.500  78.500  -9999.000
32.500  60.500  -9999.000
32.500  61.500  -9999.000
32.500  62.500  -9999.000
32.500  63.500  -9999.000
32.500  64.500  -9999.000
32.500  65.500  -9999.000
32.500  66.500  -9999.000
32.500  67.500  -9999.000
32.500  68.500  -9999.000
32.500  69.500  -9999.000
32.500  70.500  -9999.000
32.500  71.500  294.312
32.500  72.500  294.812
32.500  73.500  -9999.000
32.500  74.500  -9999.000
32.500  75.500  -9999.000
32.500  76.500  -9999.000
32.500  77.500  -9999.000
32.500  78.500  -9999.000
33.500  60.500  -9999.000
33.500  61.500  -9999.000
33.500  62.500  -9999.000
33.500  63.500  -9999.000
33.500  64.500  -9999.000
33.500  65.500  -9999.000
33.500  66.500  -9999.000
33.500  67.500  -9999.000
33.500  68.500  -9999.000
33.500  69.500  -9999.000
33.500  70.500  -9999.000
33.500  71.500  -9999.000
33.500  72.500  -9999.000
33.500  73.500  -9999.000
33.500  74.500  -9999.000
33.500  75.500  -9999.000
33.500  76.500  -9999.000
33.500  77.500  -9999.000
33.500  78.500  -9999.000
34.500  60.500  -9999.000
34.500  61.500  -9999.000
34.500  62.500  -9999.000
34.500  63.500  -9999.000
34.500  64.500  -9999.000
34.500  65.500  -9999.000
34.500  66.500  -9999.000
34.500  67.500  -9999.000
34.500  68.500  -9999.000
34.500  69.500  -9999.000
34.500  70.500  -9999.000
34.500  71.500  -9999.000
34.500  72.500  -9999.000
34.500  73.500  -9999.000
34.500  74.500  -9999.000
34.500  75.500  -9999.000
34.500  76.500  -9999.000
34.500  77.500  -9999.000
34.500  78.500  -9999.000
35.500  60.500  -9999.000
35.500  61.500  -9999.000
35.500  62.500  -9999.000
35.500  63.500  -9999.000
35.500  64.500  -9999.000
35.500  65.500  -9999.000
35.500  66.500  -9999.000
35.500  67.500  -9999.000
35.500  68.500  -9999.000
35.500  69.500  -9999.000
35.500  70.500  -9999.000
35.500  71.500  -9999.000
35.500  72.500  -9999.000
35.500  73.500  -9999.000
35.500  74.500  -9999.000
35.500  75.500  -9999.000
35.500  76.500  -9999.000
35.500  77.500  -9999.000
35.500  78.500  -9999.000
36.500  60.500  276.742
36.500  61.500  274.406
36.500  62.500  -9999.000
36.500  63.500  -9999.000
36.500  64.500  -9999.000
36.500  65.500  272.219
36.500  66.500  273.023
36.500  67.500  275.875
36.500  68.500  -9999.000
36.500  69.500  -9999.000
36.500  70.500  -9999.000
36.500  71.500  -9999.000
36.500  72.500  -9999.000
36.500  73.500  -9999.000
36.500  74.500  -9999.000
36.500  75.500  -9999.000
36.500  76.500  -9999.000
36.500  77.500  -9999.000
36.500  78.500  -9999.000
37.500  60.500  277.406
37.500  61.500  277.547
37.500  62.500  276.375
37.500  63.500  275.484
37.500  64.500  276.820
37.500  65.500  275.312
37.500  66.500  274.875
37.500  67.500  275.875
37.500  68.500  -9999.000
37.500  69.500  -9999.000
37.500  70.500  -9999.000
37.500  71.500  -9999.000
37.500  72.500  -9999.000
37.500  73.500  -9999.000
37.500  74.500  -9999.000
37.500  75.500  -9999.000
37.500  76.500  -9999.000
37.500  77.500  -9999.000
37.500  78.500  -9999.000

On google i found a tool for bilinear interpolation over a image or matrix. For the time shake this code can be modify to my requirement. But How?

function interVal=interpImg(img,yx,zpad)
% BiLinear interpolation using 4 pixels around the target location with ceil convention
% RGB = 1 for gray scale images.
% img can be a single layer matrix or a RGB layer colored image
% yx =[y_value, x_value]; It can be either horizontal or vertical vector
%
% zpad is a boolean variable. if true, zeros are used for pixel values
% outside of the given img. If false, the nearest edge value is repeated.
%
% Example:
% [m,n]=meshgrid(1:3);img=[m+n]
% --> 2     3     4
%     3     4     5
%     4     5     6
% interpImg(img,[2.4,2.2])
% --> 4.6
%
%                                   Disi A, Sep,16th,2013
%                                   [email protected]

if nargin<4,RGB=ndims(img);RGB(RGB<3)=1; end
if nargin<3,zpad=true; end

yx0=floor(yx);
wt=yx-yx0; wtConj=1-wt;
interTop=wtConj(2)*pixLookup(img,yx0(1),yx0(2),zpad,RGB)+wt(2)*pixLookup(img,yx0(1),yx(2),zpad,RGB);
interBtm=wtConj(2)*pixLookup(img,yx(1),yx0(2),zpad,RGB)+wt(2)*pixLookup(img,yx(1),yx(2),zpad,RGB);
interVal=wtConj(1)*interTop+wt(1)*interBtm;

end


function pixVal=pixLookup(img,y,x,zpad,RGB)
% This helper function looks up a pixel value from a given input image
% img is the input image (RGB or Grayscale)
% yx is the coordinate and repEdge tells the condition for pixel values out
% side of img (Use round up convention)
% For grayscale use RGB =1
if nargin<4,RGB=3;end

pixVal=zeros(1,1,RGB); %Initialize the pixel 

if nargin<3
    zpad=true; %pad with black pixels
end

if RGB==3
    [ROW,COL,~]=size(img);
else
    [ROW,COL]=size(img);
end
% If the pixel value is outside of image given
if (x<=0)||(x>COL)||(y<=0)||(y>ROW) 
    if zpad
        pixVal(:)=0;
    else
        y0=y;x0=x;
        y0(y0<1)=1; x0(x0<1)=1;
        y0(y0>ROW)=ROW;x0(x0>COL)=COL;
        pixVal=img(y0,x0,:);
    end
else
    pixVal=img(ceil(y),ceil(x),:);
end

end

. Link of this tool is here http://www.mathworks.com/matlabcentral/fileexchange/43533-bilinear-interpolation-of-an-image-or-matrix


Solution

  • You're going to want to read in the data, identify all the rows where the last column is unknown. Then using the "good" data points you can construct a 2D interpolant (f(x,y)) to sample at the unknown points.

    You'll have to use griddata rather than interp2 since your data is scattered. You'll want to use the linear interpolation method (the default).

    The code below should achieve this result.

    % Read in data from file into an M x 3 matrix
    fid = fopen('data.txt', 'rb');
    data = textscan(fid, '%f %f %f');
    data = cat(2, data{:});
    
    % Find rows where third column is unknown
    isUnknown = data(:,3) == -9999;
    isKnown = ~isUnknown;
    
    % Perform scattered interpolation AT unknown location USING known locations
    newValues = griddata(data(isKnown,1), data(isKnown,2), data(isKnown,3), ...
                         data(isUnknown,1), data(isUnknown, 2), 'linear');
    
    % Replace the unknown values with the interpolated values
    data(isUnknown, 3) = newValues;
    

    If we visualize this we will see the following.

    enter image description here

    Unfortunately, a lot of your empty data values actually lie outside of the range of the data that you know. griddata will fill these in with NaNs. In order to fill these in, you can't just rely on interpolation but rather must try to use extrapolation.

    If you actually want to do this (rather than leaving them as NaN's), you will want to use scatteredInterpolant with linear extrapolation.

    interpolant = scatteredInterpolant(data(isKnown,1:2), data(isKnown,3), 'linear', 'linear');
    newValues = interpolant(data(isUnknown,1:2));
    
    data(isUnknown,3) = newValues;
    

    Now you can see that those values have been extrapolated.

    enter image description here

    You should choose whichever method is more appropriate for your use case.