6

编辑:更改了标题。我对这两个段是否相同不太感兴趣,而是如果它们彼此共线,则在一定的公差范围内。如果是这样,那么这些线应该聚集在一起作为一个单独的段。

编辑:我想一个简短的说法:我试图以一种有效的方式将相似的线段聚集在一起。

假设我有线段f (fx0, fy0)(fx1, fy1)g (gx0, gy0)(gx1, gy1)

这些来自诸如计算机视觉算法边缘检测器之类的东西,在某些情况下,两条线基本相同,但由于像素容差而被视为两条不同的线。

有几种情况

  • fg共享完全相同的端点,例如:f = (0,0), (10,10) g = (0,0), (10,10)
  • fg共享大致相同的端点和大致相同的长度,例如:f = (0,0.01), (9.95,10) g = (0,0), (10,10)
  • f是 的子集g,这意味着它的端点落在该段内g并与该段共享相同的斜率g。想象一条粗略的线,笔在其中来回移动以使其变粗。例如:f = (4.00, 4.02), (9.01, 9.02) g = (0,0), (10,10)

以下将被视为相同:

  • f并且g有超过一定的斜率差tolerance
  • f并且g可能具有相同的斜率但相隔一段距离tolerance,即平行线
  • f并且g在同一平面和同一坡度上,但根本不重叠……即虚线内的一组线段。

判断它们是否相同的最简单方法是如果gx1 - fx1 <= tolerance(对其他三个点重复),但在某些情况下,线f可能比线短g(同样,由于像素差异和/或照片扫描不佳)。

那么将这两个线段转换成极坐标并比较角度会更好吗?在这种情况下,两个 rho 将在容差范围内。但是你必须确保两条线段具有相同的“方向”,这在笛卡尔坐标或极坐标中计算是微不足道的。

所以这很容易找到一种方法,但我只是想知道是否有一种更清洁的方法,基于我早已忘记的线性代数?

4

3 回答 3

2

Your problem is two-fold: you want to compare both the difference in length and difference in angles. To compute the difference in length, you'd take the length of the first line and divide it by the length of the second line.

To take the difference in angle, you can use atan or, my favourite:

angle = acos(abs((u dot v)/(u.length * v.length)))

Hopefully this helps. Sorry for the mistaken answer earlier.

Old answer:

Here's an idea for you: why not compare the difference in the start and end points of the two line segments to the total length of one of the lines? Then your difference function would look something like:

def difference(Line l1, Line l2):
    # Distance between first point on first line and first point on second line
    first_point_diff = (Line(l1.x1, l2.x1, l1.y1, l2.y1).length())

    # Distance between first point on first line and first point on second line
    second_point_diff = (Line(l1.x2, l2.x2, l1.y2, l2.y2).length())

    return (first_point_diff + second_point_diff)/l1.length()

This function will return the "difference" between two lines as a fraction of the total length of the first line.

于 2012-07-10T17:45:32.483 回答
2

Can you use the coordinates to define equations for the lines? If so, then you could use the two equations in a system of equations, solve the system, and find out if and where the lines intersect. If the lines do not intersect at all but the distance between them is very small, or with in the tolerance you could consider them a single line.

于 2012-07-10T18:01:22.530 回答
0

If all you want is to see if they are in the same direction, couldn't you just consider the dot product divided by the magnitudes? Closer it is to 1, the closer the alignment between the two lines.

于 2012-07-10T19:09:48.960 回答