如何为任何二维形状(或曲线)生成阶梯曲线(轮廓)? [英] How to generate the stair step curve(outline) for any 2d shape(or curve)?

查看:178
本文介绍了如何为任何二维形状(或曲线)生成阶梯曲线(轮廓)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有任意二维形状的轮廓上的点的坐标,我如何找到组成阶梯曲线轮廓的点的坐标,这最好地表示原始轮廓,但只能使用一组已知坐标(xi,i = 1,...,n和yi,i = 1,...,m)。例如,原始三角形由粗实蓝线表示。如果我的理解是正确的,它与matlab阶梯函数不同。
的matlab代码会很好,但在其他语言中也可以,算法是最重要的。谢谢。



解决方案

首先,根据您的情节定义一组示例数据。假设像素中心以整数值对齐(遵循MATLAB惯例),并且左下角位于(0.5,0.5)处,这里是我们得到的数据:

  vx = [1.5; 9.7; 3.7; 1.5]; %三角形顶点的X值
vy = [8.3; 6.0; 1.7; 8.3]; %三角形顶点的值
x = 1:10; %X像素中心坐标
y = 1:9; %Y像素中心坐标

请注意,顶点坐标从三角形的左上角开始排序并顺时针前进,在结尾处重复第一个顶点以关闭多边形。



获得掩码(简单部分):

如果您有



xOutline yOutline 中的轮廓坐标从逆时针的绿色圆圈开始排序在掩码区域周围。


If I have the coordinates of the points on the outline of an arbitrary 2D shape, how can I find the coordinates of points composing the outline of a stair step curve, which best represents the original outline, but only use a set of known coordinates (xi, i=1,...,n and yi, i=1,...,m). For example the original triangle is represented by the thick solid blue line. it's different from the matlab stairs function, if my understanding is correct. matlab code will be nice, but in other language is also ok, algorithm is most important.Thanks.

解决方案

I'll start by defining a set of sample data based on your plot. Assuming that the pixel centers are aligned at integer values (the convention MATLAB follows) and that the lower left corner is at (0.5, 0.5), here's the data we get:

vx = [1.5; 9.7; 3.7; 1.5];  % X values of triangle vertices
vy = [8.3; 6.0; 1.7; 8.3];  % Y values of triangle vertices
x = 1:10;                   % X pixel center coordinates
y = 1:9;                    % Y pixel center coordinates

Note that the vertex coordinates are ordered starting at the top left corner of the triangle and proceeding clockwise, repeating the first vertex at the end to close the polygon.

Getting the mask (the easy part):

There is an easy way to compute the dark gray mask if you have the Image Processing Toolbox: use poly2mask:

mask = poly2mask(vx, vy, numel(y), numel(x));

The algorithm this function uses is discussed here. However, if you'd like to use a pure MATLAB approach that requires no special toolboxes, you can use inpolygon instead:

[cx, cy] = meshgrid(x, y);         % Generate a grid of x and y values
mask = inpolygon(cx, cy, vx, vy);

In this case, a pixel is included in the mask as long as its center point lies within the polygon. In this particular example these two approaches yield the same resulting mask, but they won't always due to the differences in their criteria for deciding if a pixel is included or not.

Getting the outline coordinates:

It's a little more involved to get the coordinates of the mask outline, ordered appropriately around the perimeter. To accomplish this, we can represent the mask as a series of vertices and triangular facets (using the triangulation function), then compute the free boundary (i.e. edges that are only present on one triangular facet):

% Create raw triangulation data:
[cx, cy] = meshgrid(x, y);
xTri = bsxfun(@plus, [0; 1; 1; 0], cx(mask).');
yTri = bsxfun(@plus, [0; 0; 1; 1], cy(mask).');
V = [xTri(:) yTri(:)];
F = reshape(bsxfun(@plus, [1; 2; 3; 1; 3; 4], 0:4:(4*nnz(mask)-4)), 3, []).';

% Trim triangulation data:
[V, ~, Vindex] = unique(V, 'rows');
V = V-0.5;
F = Vindex(F);

% Create triangulation and find free edge coordinates:
TR = triangulation(F, V);
freeEdges = freeBoundary(TR).';
xOutline = V(freeEdges(1, [1:end 1]), 1);  % Ordered edge x coordinates
yOutline = V(freeEdges(1, [1:end 1]), 2);  % Ordered edge y coordinates

And we can plot this like so:

imagesc(x, y, mask);
axis equal
set(gca, 'XLim', [min(x)-0.5 max(x)+0.5], ...
         'YLim', [min(y)-0.5 max(y)+0.5], ...
         'XTick', x, 'YTick', y, 'YDir', 'normal');
colormap([0.9 0.9 0.9; 0.6 0.6 0.6]);
hold on;
plot(xOutline, yOutline, 'b', 'LineWidth', 2);
plot(xOutline(1), yOutline(1), 'go', 'LineWidth', 2);
plot(vx, vy, 'r', 'LineWidth', 2);

The outline coordinates in xOutline and yOutline are ordered starting from the green circle going counter-clockwise around the mask region.

这篇关于如何为任何二维形状(或曲线)生成阶梯曲线(轮廓)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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