计算图像和多边形之间的重叠的简单方法是什么? [英] What is an simple way to compute the overlap between an image and a polygon?

查看:188
本文介绍了计算图像和多边形之间的重叠的简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个封闭的非自相交多边形.它的顶点保存在两个向量X和Y中.最后,X和Y的值绑定在0和22之间.

I have a closed non-self-intersecting polygon. Its vertices are saved in two vectors X, and Y. Finally the values of X and Y are bound between 0 and 22.

我想构造一个尺寸为22x22的矩阵,如果多边形的一部分与该bin重叠,则将每个bin的值设置为true,否则为false.

I'd like to construct a matrix of size 22x22 and set the value of each bin equal to true if part of the polygon overlaps with that bin, otherwise false.

我最初的想法是生成一个用[a, b] = meshgrid(1:22)定义的点的网格,然后使用inpolygon来确定网格中的哪些点.

My initial thought was to generate a grid of points defined with [a, b] = meshgrid(1:22) and then to use inpolygon to determine which points of the grid were in the polygon.

[a b] = meshgrid(1:22);
inPoly1 = inpolygon(a,b,X,Y);

但是,只有当bin的中心包含在多边形中时,它才返回true,即,它在下图中返回红色形状.但是,更需要的是绿色形状的线(尽管它仍然是不完整的解决方案).

However this only returns true if if the center of the bin is contained in the polygon, ie it returns the red shape in the image below. However what need is more along the lines of the green shape (although its still an incomplete solution).

要获取绿色斑点,我对inpolygon进行了四个调用.对于每次比较,我将点网格分别移动NE,NW,SE或SW 1/2.这等效于测试垃圾箱的角是否在多边形中.

To get the green blob I performed four calls to inpolygon. For each comparison I shifted the grid of points either NE, NW, SE, or SW by 1/2. This is equivalent to testing if the corners of a bin are in the polygon.

inPoly2 = inpolygon(a-.5,b-.5,X,Y) | inpolygon(a+.5,b-.5,X,Y) | inpolygon(a-.5,b+5,X,Y) | inpolygon(a+.5,b+.5,X,Y);

这确实为我提供了部分解决方案,但是当顶点包含在仓中但没有仓角时,它会失败.

While this does provide me with a partial solution it fails in the case when a vertex is contain in a bin but none of the bin corners are.

是否存在一种更直接的方法来解决此问题,最好使用一种能够产生更具可读性的代码的解决方案?

Is there a more direct way of attacking this problem, with preferably a solution that produces more readable code?

此情节是用以下内容绘制的:

This plot was drawn with:

imagesc(inPoly1 + inPoly2); hold on;
line(a, b, 'w.');
line(X, Y, 'y); 

推荐答案

一个建议是使用polybool函数(在2008b或更早版本中不可用).它找到两个多边形的交点并返回结果顶点(如果不存在顶点,则返回空向量).要在这里使用它,我们对网格中的所有正方形进行迭代(使用arrayfun),以检查polybool的输出参数是否为空(例如无重叠).

One suggestion is to use the polybool function (not available in 2008b or earlier). It finds the intersection of two polygons and returns resulting vertices (or an empty vector if no vertices exist). To use it here, we iterate (using arrayfun) over all of the squares in your grid check to see whether the output argument to polybool is empty (e.g. no overlap).

N=22;
sqX = repmat([1:N]',1,N);
sqX = sqX(:);
sqY = repmat(1:N,N,1);
sqY = sqY(:);

intersects = arrayfun((@(xs,ys) ...
      (~isempty(polybool('intersection',X,Y,[xs-1 xs-1 xs xs],[ys-1 ys ys ys-1])))),...
      sqX,sqY);

intersects = reshape(intersects,22,22);

这是生成的图像:

绘图代码:

imagesc(.5:1:N-.5,.5:1:N-.5,intersects');
hold on;
plot(X,Y,'w');
for x = 1:N
    plot([0 N],[x x],'-k');
    plot([x x],[0 N],'-k');
end
hold off;

这篇关于计算图像和多边形之间的重叠的简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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