pythontypescriptgeometryhough-transform

Find closest point on HoughLine


Given a hough line as {rho: number, theta: number} and a coordinate as {x: number, y: number}, how would I find the closest point on that hough line to the given coordinate?

So in the following sample graphic, I have the blue line and the red dot, and I'm looking for the white cross.

Graphic

I'm using Typescript, but I'd appreciate answers in any language or format, since this is technically more of a math problem.


Solution

  • White cross is projection of red point C onto the line.

    At first get components of normal vector to line

    nx = cos(theta)
    ny = sin(theta)
    

    and direction vector of line is

    dx = -ny
    dy = nx
    

    To find this projection, we need some point A at the line. Let's this point is

    ax = rho * nx
    ay = rho * ny
    

    and get vector AC

    acx = cx - ax
    acy = cy - ay
    

    Now calculate projection using dot product

    coeff = dx*acx + dy*acy
    px = ax + dx * coeff
    py = ay + dy * coeff
    

    Quick-made Python test (not well tested)

    import math
    def projection_onto_houghline(rho, theta, cx, cy):
       dx = -math.sin(theta)
       dy = math.cos(theta)
       ax = rho * dy
       ay = -rho * dx
       acx = cx - ax
       acy = cy - ay
       coeff = dx*acx + dy*acy
       px = ax + dx * coeff
       py = ay + dy * coeff
       return(px, py)
    
    print(projection_onto_houghline(math.sqrt(2), math.pi/4, 0, 0))
    print(projection_onto_houghline(math.sqrt(2), math.pi/4, 2.5, 2))
    
    (1.0000000000000002, 1.0000000000000002)
    (1.2500000000000002, 0.7500000000000002)   
    

    enter image description here