Image interpolation from random pixels
The regular interp2
will not work here, since your points are not located at regular intervals (Not sitting on a grid).
You can either try TriScatteredInterp
or download inpaint_nans
from the file exchange.
Here is the solution in your case with TriScatteredInterp
:
function solveStackOverflowProblem()
im = imread('http://i.stack.imgur.com/lMaYR.png');
im = im(:,:,2);
[i,j] = find(im);
y = j; x = i;
indexes = sub2ind(size(im),i,j);
interpolator = TriScatteredInterp(x,y,double(im(indexes)));
[Y,X] = meshgrid( 1:size(im,2),1:size(im,1));
reconstructedImage = interpolator(X,Y);
figure;imshow(reconstructedImage/255)
end
Your best solution is to use gridfit. Its designed to improve on all the native Matlab functions like TriScatteredInterp and griddata.
Here is a solution based on using Radial Basis Functions ( in this case a Gaussian ) to build interpolations for randomly spaced points with varying intensities.
Essentially this drops a Gaussian on each point, weights it by the point intensity and sums the result.
The sharpness of the interpolation function is controllable via the standard deviation of the chosen Gaussian function.
The points:
The interpolation:
Viewed in 3D:
With a smaller standard deviation:
The code:
pts = Table[{{RandomReal[{0, 200}], RandomReal[{0, 200}]},
RandomReal[]}, {20}]
dists = Function[points,
Plus @@ ((PDF[
MultinormalDistribution[#, 200 IdentityMatrix[2]], {x,
y}] & /@ points[[All, 1]] ) points[[All, 2]])/Length@points]
DensityPlot[dists[pts], {x, 0, 200}, {y, 0, 200}, PlotPoints -> 100]