How to use linear interpolation?

5 次查看(过去 30 天)
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 个评论
Jan
Jan 2017-1-19
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.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2017-1-19
编辑:Jan 2017-1-19
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 个评论
Nicolas Perillo
Nicolas Perillo 2017-1-19
Okay I got it. Anyway I think for my implementation it would be better to consider the curvature.
Nicolas Perillo
Nicolas Perillo 2017-1-19
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.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2017-1-19

类别

Help CenterFile Exchange 中查找有关 Interpolation of 2-D Selections in 3-D Grids 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by