How to use linear interpolation?

4 views (last 30 days)
Nicolas Perillo
Nicolas Perillo on 19 Jan 2017
Commented: Nicolas Perillo on 19 Jan 2017
Hello everyone. I have a matrix that contains a pairs (pixel coordinates), for example M = [51,2 ; 50,2 ; 50,3 ; 51,2 ; 50,2 ; 50,1 ; 51,2 ; 52,4 ; 50,3], size(M) 9x2. The idea is that these pixels models a curve/contour extracted by a picture.
My problem is to use linear interpolation to obtain a new set of pairs of pixel coordinates such that the curve/contour is divided evenly in X segments, let's suppose X=12 segments.
In the example I'll have that each segment has a length of 9/12=0.75, this means I need to save 12 pixels starting from the position (51,2), ending at (50,3) with a rest of 10 pixels inside this interval placed "equally" with a distance of 0.75 that follow the curve/contour.
It seems impossible to obtain, save and display this new set of points because MATLAB works only with integer values. Notice that after this step, I need to use the new set of pixel coordinates to implement other functionalities (I don't need to display them and stop).
Does anyone knows a possible solution to my problem? Are there any functions that allow to use decimal coordinates (like cartesian axis)?
Thank you so much in advance.
  1 Comment
Jan
Jan on 19 Jan 2017
I cannot imagine what "because MATLAB works only with integer values" means. Of course Matlab works with floating point values also. The coordinates can be floating point values without any problems. Only the indices of arrays must be integer, but this does not matter here at all. Do I understand correctly, that you want to obtain a [12 x 2] matrix by interpolation from your [9 x 2] matrix? If you claim, that the segements have a length of 0.75 - what does this mean? 0.75 in which units? I seems like you confuse the value of the elemts with the index.

Sign in to comment.

Accepted Answer

Jan
Jan on 19 Jan 2017
Edited: Jan on 19 Jan 2017
M = [51,2 ; 50,2 ; 50,3 ; 51,2 ; 50,2 ; 50,1 ; 51,2 ; 52,4 ; 50,3];
L = size(M, 1);
MM = interp1(1:L, M, linspace(1, L, 12));
This does not consider this part of your question:
In the example I'll have that each segment has a length of 9/12=0.75,
this means I need to save 12 pixels starting from the position (51,2),
ending at (50,3) with a rest of 10 pixels inside this interval placed
"equally" with a distance of 0.75 that follow the curve/contour.
because I do not understand its meaning.
  4 Comments
Nicolas Perillo
Nicolas Perillo on 19 Jan 2017
Okay I got it. Anyway I think for my implementation it would be better to consider the curvature.
Nicolas Perillo
Nicolas Perillo on 19 Jan 2017
I just had the occasion to try your 2 simple rows of code and I think I solved all my problems! Thank you, it works like a charm.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 19 Jan 2017

Categories

Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!