在MATLAB中获取plot()生成的中间点 [英] Getting intermediate points generated by plot() in MATLAB

查看:678
本文介绍了在MATLAB中获取plot()生成的中间点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中有一系列XY点对.这些对描述图像中形状周围的点.它们不是函数,意味着每个x值可能存在两个或更多y点.

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');

我要检索的是我自己的点值,可用于连接点,以便将其用于进一步分析.我不需要完全连接的图,我需要基于数据的东西,而不仅仅是plot()生成的图形.我只想让plot()生成这些点(似乎在后台执行),但是我尝试使用plot()返回的linseries,但它可能无法正常工作,或者只是我理解而已没有给我我想要的东西.

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.

我认为这是一个插值问题,但是这些点不包含函数.他们描述一个形状.本质上,我所需要的只是plot()似乎要计算的点;连接一系列点的直线.弯曲将是一个好处,并且可以使我免于下游的痛苦.

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.

如何在MATLAB中执行此操作?

How can I do this in MATLAB?

谢谢!

是的,图片会有所帮助:)

Yes, a picture would help :)

蓝点是实际的点值(x,y),使用上面的第一个plot()调用绘制.红色轮廓是使用上述第二种方法调用plot()的结果.我正在尝试获取红色轮廓的点数据;换句话说,连接蓝色点的点.

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.

推荐答案

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


示例:

下面是一个示例,演示上述代码的性能(我使用11对x和y坐标,为完整示例,重复其中的一对):

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

这篇关于在MATLAB中获取plot()生成的中间点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆