MATLAB Answers


convert randomly spaced data points to an image

Asked by AP
on 13 Oct 2012

Dear All,

I have a set of randomly spaced 2D data and would like to convert it into an image. I want to create an image whose intensity is 255 on the random points and zero elsewhere. I interpolate the data into a fine and evenly spaced grid (meshgrid format) using the following code but I failed. After I interpolate, the image is not an exact match or even close to the representation of random points. If you compare subplot(2,1,1) and subplot(2,1,2) in the following code, it can be seen that all the space between the random points gets 255. Is there a better way to convert the data to an image?

I would be grateful for any help.


% Range of random number for x and y.
L = 8;
H = 6;
% The number of random numbers.
n = 100;
% Randomly spaced data.
x = rand( n, 1 )*L;
y = rand( n, 1 )*H;
% Intensity of the data points are 'White'.
intensity = ones(size(x))*255;
subplot( 2,1,1 )
plot( x, y, '.' )
% Interpolate the data into a rectangular evenly spaced grid.
[ X, Y ] = meshgrid( 0:0.01:L, 0:0.01:H );
F = TriScatteredInterp( x, y, intensity );
intensityInterp = F( X, Y );
imshow( intensityInterp )


Since points are points, and therefore occupy no area, wouldn't you expect the image to be completely zero unless one of your X,Y meshgrid sample locations happened to exactly coinicide with one of the randomly scattered points?

If so, why not just do intensityInterp=zeros(...) and be done with it?

You are right. The points' thickness are zero. How about saving subplot(2,1,1) as a jpg and work from there?


No products are associated with this question.

1 Answer

Answer by Matt J
on 13 Oct 2012

You might also try adding lots of additional scattered points with intensity zero. That will give the interpolation code information about what intensity lies in between the high intensity points.

In any case, whatever interpolator you choose, you will be accepting the interpolator's idea of how the intensity transitions from high to zero. So, to inform that choice, you have to have some idea of what you want that transition to be.


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!