Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting intermediate points generated by plot() in MATLAB

Tags:

plot

matlab

I've got a series of XY point pairs in MATLAB. These pairs describe points around a shape in an image; they're not a function, meaning that two or more y points may exist for each x value.

I can plot these points individually using something like

plot(B(:,1),B(:,2),'b+');

I can also use plot to connect the points:

plot(B(:,1),B(:,2),'r');

What I'm trying to retrieve are my own point values I can use to connect the points so that I can use them for further analysis. I don't want a fully connected graph and I need something data-based, not just the graphic that plot() produces. I'd love to just have plot() generate these points (as it seems to do behind the scenes), but I've tried using the linseries returned by plot() and it either doesn't work as I understand it or just doesn't give me what I want.

I'd think this was an interpolation problem, but the points don't comprise a function; they describe a shape. Essentially, all I need are the points that plot() seems to calculate; straight lines connecting a series of points. A curve would be a bonus and would save me grief downstream.

How can I do this in MATLAB?

Thanks!

Edit: Yes, a picture would help :)

The blue points are the actual point values (x,y), plotted using the first plot() call above. The red outline is the result of calling plot() using the second approach above. I'm trying to get the point data of the red outline; in other words, the points connecting the blue points. alt text

like image 967
Mike O'Malley Avatar asked Feb 25 '23 14:02

Mike O'Malley


1 Answers

Adrien definitely has the right idea: define a parametric coordinate then perform linear interpolation on the x and y coordinates separately.

One thing I'd like to add is another way to define your parametric coordinate so you can create evenly-spaced interpolation points around the entire shape in one pass. The first thing you want to do, if you haven't already, is make sure the last coordinate point reconnects to the first by replicating the first point and adding it to the end:

B = [B; B(1,:)];

Next, by computing the total distance between subsequent points then taking the cumulative sum, you can get a parametric coordinate that makes small steps for points close together and larger steps for points far apart:

distance = sqrt(sum(diff(B,1,1).^2,2));  %# Distance between subsequent points
s = [0; cumsum(distance)];               %# Parametric coordinate

Now, you can interpolate a new set of points that are evenly spaced around the edge along the straight lines joining your points using the function INTERP1Q:

sNew = linspace(0,s(end),100).';   %'# 100 evenly spaced points from 0 to s(end)
xNew = interp1q(s,B(:,1),sNew);     %# Interpolate new x values
yNew = interp1q(s,B(:,2),sNew);     %# Interpolate new y values

These new sets of points won't necessarily include the original points, so if you want to be sure the original points also appear in the new set, you can do the following:

[sAll,sortIndex] = sort([s; sNew]);  %# Sort all the parametric coordinates
xAll = [B(:,1); xNew];               %# Collect the x coordinates
xAll = xAll(sortIndex);              %# Sort the x coordinates
yAll = [B(:,2); yNew];               %# Collect the y coordinate
yAll = yAll(sortIndex);              %# Sort the y coordinates


EXAMPLE:

Here's an example to show how the above code performs (I use 11 pairs of x and y coordinates, one of which is repeated for the sake of a complete example):

B = [0.1371 0.1301; ...  %# Sample data
     0.0541 0.5687; ...
     0.0541 0.5687; ...  %# Repeated point
     0.0588 0.5863; ...
     0.3652 0.8670; ...
     0.3906 0.8640; ...
     0.4090 0.8640; ...
     0.8283 0.7939; ...
     0.7661 0.3874; ...
     0.4804 0.1418; ...
     0.4551 0.1418];
%# Run the above code...
plot(B(:,1),B(:,2),'b-*');  %# Plot the original points
hold on;                    %# Add to the plot
plot(xNew,yNew,'ro');       %# Plot xNew and yNew

alt text

like image 119
gnovice Avatar answered Mar 05 '23 16:03

gnovice