Example #1
0
def lineIntersection(line1, line2):
    '''
    Finds the intersection point of two 2D lines in (r, theta) form,
     where the line is parallel to the vector denoted by (r, theta)
    '''
    p1a = x1, y1 = toXY(line1)
    p1b = x1 - y1, x1 + y1
    p2a = x3, y3 = toXY(line2)
    p2b = x3 - y3, x3 + y3

    return util.lineIntersectionPoints(p1a, p1b, p2a, p2b)
Example #2
0
def lineIntersection(line1, line2):
    '''
    Finds the intersection point of two 2D lines in (r, theta) form,
     where the line is parallel to the vector denoted by (r, theta)
    '''
    p1a = x1, y1 = toXY(line1)
    p1b = x1-y1, x1+y1
    p2a = x3, y3 = toXY(line2)
    p2b = x3-y3, x3+y3

    return util.lineIntersectionPoints(p1a, p1b, p2a, p2b)
Example #3
0
def lineToVisibleSegment(line, threshold_img, eps=1e-8):
    '''
    Returns the two endpoints of the line segment in the frame
    line is in (r, theta) form
    return value is two points in ((x1, y1) (x2, y2)), where x1 <= x2
    '''
    width, height = cv.GetSize(threshold_img)

    # Get two points on the line
    p1a = x1, y1 = toXY(line)
    p1b = x1 - y1, x1 + y1

    # Find top-left point a
    # First try to intersect with each edge of image
    candidates = [
        util.lineIntersectionPoints(p1a, p1b, (0, 0), (width, 0)),
        util.lineIntersectionPoints(p1a, p1b, (0, 0), (0, height)),
        util.lineIntersectionPoints(p1a, p1b, (0, height), (width, height)),
        util.lineIntersectionPoints(p1a, p1b, (width, 0), (width, height))
    ]
    # There should be only 2 intersection points with the image rectangle,
    #  (or 1 if it's tangent to a corner, or inf if it lies on an edge, but
    #   we'll ignore those cases for now since they're improbable)
    # Expand bounds by epsilon=1e-8 to account for floating point imprecision
    points = filter(lambda x: (-eps <= x[0] <= width+eps) \
                      and (-eps <= x[1] <= height+eps),
                    candidates)
    if len(points) != 2:
        print 'Line is not within in image bounds!'
        return None

    # Try to iterate over pixel values starting at points[0]
    m = x1 / -y1 if y1 != 0 else float('inf')
    b = m * -x1 + y1
    startPt = None
    endPt = None
    # Line is more horizontal: iterate over x values
    if m < 1:
        # Ensure that point[0] is leftmost point
        if points[0][0] > points[1][0]:
            points[0], points[1] = points[1], points[0]

        # Look for start point from leftmost point
        x, y = points[0]
        while x < points[1][0] and startPt is None:
            if getPx(threshold_img, x, y) != 0:
                startPt = (x, y)
            x += 1
            y = m * x + b

        # Look for end point from rightmost point
        x, y = points[1]
        while x > points[0][0] and endPt is None:
            if getPx(threshold_img, x, y) != 0:
                endPt = (x, y)
            x -= 1
            y = m * x + b
    # Line is more vertical: iterate over y values
    else:
        m = y1 / x1
        b = m * -y1 + x1

        # Ensure that point[0] is topmost point
        if points[0][1] > points[1][1]:
            points[0], points[1] = points[1], points[0]

        # Look for start point from topmost point
        x, y = points[0]
        while y < points[1][1] and startPt is None:
            if getPx(threshold_img, x, y) != 0:
                startPt = (x, y)
            y += 1
            x = m * y + b

        x, y = points[1]
        while y < points[0][1] and endPt is None:
            if getPx(threshold_img, x, y) != 0:
                endPt = (x, y)
            y -= 1
            x = m * y + b

    if startPt is None or endPt is None:
        print 'Line segment not found: startPt=%s, endPt=%s' % (startPt, endPt)
        return None

    return startPt, endPt
Example #4
0
def lineToVisibleSegment(line, threshold_img, eps=1e-8):
    '''
    Returns the two endpoints of the line segment in the frame
    line is in (r, theta) form
    return value is two points in ((x1, y1) (x2, y2)), where x1 <= x2
    '''
    width, height = cv.GetSize(threshold_img)

    # Get two points on the line
    p1a = x1, y1 = toXY(line)
    p1b = x1-y1, x1+y1

    # Find top-left point a
    # First try to intersect with each edge of image
    candidates = [util.lineIntersectionPoints(p1a, p1b, (0, 0), (width, 0)),
              util.lineIntersectionPoints(p1a, p1b, (0, 0), (0, height)),
              util.lineIntersectionPoints(p1a, p1b, (0, height), (width, height)),
              util.lineIntersectionPoints(p1a, p1b, (width, 0), (width, height))]
    # There should be only 2 intersection points with the image rectangle,
    #  (or 1 if it's tangent to a corner, or inf if it lies on an edge, but
    #   we'll ignore those cases for now since they're improbable)
    # Expand bounds by epsilon=1e-8 to account for floating point imprecision
    points = filter(lambda x: (-eps <= x[0] <= width+eps) \
                      and (-eps <= x[1] <= height+eps),
                    candidates)
    if len(points) != 2:
      print 'Line is not within in image bounds!'
      return None

    # Try to iterate over pixel values starting at points[0]
    m = x1 / -y1 if y1 != 0 else float('inf')
    b = m * -x1 + y1
    startPt = None
    endPt = None
    # Line is more horizontal: iterate over x values
    if m < 1:
      # Ensure that point[0] is leftmost point
      if points[0][0] > points[1][0]:
        points[0], points[1] = points[1], points[0]

      # Look for start point from leftmost point
      x, y = points[0]
      while x < points[1][0] and startPt is None:
        if getPx(threshold_img, x, y) != 0:
          startPt = (x, y)
        x += 1
        y = m * x + b

      # Look for end point from rightmost point
      x, y = points[1]
      while x > points[0][0] and endPt is None:
        if getPx(threshold_img, x, y) != 0:
          endPt = (x, y)
        x -= 1
        y = m * x + b
    # Line is more vertical: iterate over y values
    else:
      m = y1 / x1
      b = m * -y1 + x1

      # Ensure that point[0] is topmost point
      if points[0][1] > points[1][1]:
        points[0], points[1] = points[1], points[0]

      # Look for start point from topmost point
      x, y = points[0]
      while y < points[1][1] and startPt is None:
        if getPx(threshold_img, x, y) != 0:
          startPt = (x, y)
        y += 1
        x = m * y + b
      
      x, y = points[1]
      while y < points[0][1] and endPt is None:
        if getPx(threshold_img, x, y) != 0:
          endPt = (x, y)
        y -= 1
        x = m * y + b

    if startPt is None or endPt is None:
      print 'Line segment not found: startPt=%s, endPt=%s' % (startPt, endPt)
      return None

    return startPt, endPt