title: How can I get the perpendicular distances between each boundary point to the hough line?
body: <p>This is my input picture.</p>
  <p><a href="https://i.stack.imgur.com/JL1St.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JL1St.png" alt="enter image description here" /></a></p>
  <p>The yellow points on the curve are given by the &quot;drawcountours&quot; method and the blue straight line is obtained by the hough line.
  here is the following code:</p>
  <pre><code>import numpy as np
  import cv2
  from matplotlib import pyplot as plt
  # from scipy.cluster.vq import vq, kmeans
  import numpy as np
  import glob
  
  
  
  images = glob.glob(r'C:\Users\Desktop\dist.png')
  
  for fname in images:
  
    Image = cv2.imread(fname)
  
    gray = cv2.cvtColor(Image, cv2.COLOR_BGR2GRAY)
    ret,seg = cv2.threshold(gray,40,250,cv2.THRESH_BINARY)
  
    # cv2.imshow('H',seg)
    # cv2.waitKey(0)
  
  
    total=0
    _, contours, hierarchy = cv2.findContours(seg, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for c in contours:
      if cv2.contourArea(c) &gt;80 and cv2.contourArea(c) &lt;2000:
        M = cv2.moments(c)
        cX = int(M[&quot;m10&quot;] / M[&quot;m00&quot;])
        cY = int(M[&quot;m01&quot;] / M[&quot;m00&quot;])
  
        epsilon = 0.001 * cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, epsilon, True)
        total+=1
  
        cv2.drawContours(Image, approx, -1, (0, 255, 255), 5)
  
    cv2.imshow('l',Image)
    cv2.waitKey(0)
  
    lines = cv2.HoughLines(seg, 7, np.pi / 270, 300)
  
    for rho, theta in lines[0]:
      a = np.cos(theta)
      b = np.sin(theta)
      x0 = a * rho
      y0 = b * rho
      x1 = int(x0 + 1000* (-b))
      y1 = int(y0 + 1000* (a))
      x2 = int(x0 -1000 * (-b))
      y2 = int(y0 -1000 * (a))
  
      cv2.line(Image, (x1, y1), (x2,y2), (255, 255, 0), 2)
    cv2.imshow('l', Image)
    cv2.waitKey(0)
  
   ######get line equation#####
    points = [(x1, y1), (x2, y2)]
    x_coords, y_coords = zip(*points)
    A = vstack([x_coords, ones(len(x_coords))]).T
    m, c = lstsq(A, y_coords)[0]
    print(&quot;Line Solution is y = {m}x + {c}&quot;.format(m=m, c=c))
   
    #####to convert the obtained line equation to Ax+By+C=0 form###### 
    A = m
    B = -1
    C = 0
    for i in range(1, approx.shape[0]):
        
        
        per_dis = ((A * approx[i][0][0]) + (B * approx[i][0][1]) + C) / math.sqrt((A ** 2) + (B ** 2)) # the perpendicular distance
        inter_x = int(((B * (approx[i][0][0] - (A * approx[i][0][1]))) - (A * C)) / ((A ** 2) + (B ** 2)))#the x coordinate of the intersection point
        inter_y = int((A * ((-B * approx[i][0][0]) + (A * approx[i][0][1])) - (B * C)) / ((A ** 2) + (B ** 2)))#the y coordinate of the intersection point
       
        cv2.line(Image, (approx[i][0][0],approx[i][0][1]), (inter_x,inter_y), (255, 0, 255), 2)# to draw the green line 
  
    cv2.imshow('l', Image)
    cv2.waitKey(0)
  </code></pre>
  <p>How can I obtain the perpendicular distances from those <code>contour</code> points( yellow points) to the <code>hough</code> line? In other words, I want the following output.</p>
  <p><a href="https://i.stack.imgur.com/Z10ZX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z10ZX.png" alt="enter image description here" /></a></p>
  <p>I want to display the green lines and also want to know the length of each green line.</p>
  <p>I tried calculating the the perpendicular distance using the formulae from  Wikipedia but got the following output:</p>
  <p><a href="https://i.stack.imgur.com/BQTI3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BQTI3.png" alt="enter image description here" /></a></p>