acclivity

pyDistanceBetween2Lines-V2

Jan 14th, 2022 (edited)
808
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Find the minimum distance between two white lines
  2. # Mike Kerry - Jan 2022 - acclivity2@gmail.com
  3. # This is an improved algorithm that works in a wider set of cases
  4.  
  5. import cv2
  6. import numpy as np
  7.  
  8. def getdist(a , b):
  9.     # compute the square of the distance between two coordinates
  10.     dsq = (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2
  11.     return dsq
  12.  
  13.  
  14. img = cv2.imread("WhiteLines.jpg")
  15.  
  16. dims = img.shape
  17. width = dims[1]
  18. height = dims[0]
  19. minsep = width // 20            # minimum separation allowed between lines is 5% of screen with
  20. minsepsq = minsep * minsep
  21. print("Width:", width, "  Height:", height)
  22.  
  23. cv2.imshow("img", img)
  24.  
  25. array_upper = []   # This array will hold all the coordinates of white pixels of the upper line
  26. array_lower = []   # This array will hold all the coordinates of white pixels of the lower line
  27.  
  28. # Scan down the image, line by line, looking for white pixels
  29. # separate into two arrays
  30. for y, line in enumerate(img):      # y is line counter (vertical coordinate)
  31.     for x, bgr in enumerate(line):  # x is the horizontal coordinate
  32.         # Is this pixel white?
  33.         if sum(bgr) > 384:  # look for pale pixels  (765 would be pure white, 0 is black)
  34.             # Is this pixel close to the last pixel for upper line array? (if any)
  35.             i = len(array_upper)
  36.             if i and getdist((y, x), array_upper[i - 1]) > minsepsq:
  37.                 # this white pixel is not close to the previous pixel in array_upper
  38.                 # therefore it belongs in the array for the lower line
  39.                 array_lower.append((y, x))
  40.             else:
  41.                 # pixel is near previous pixel in array_upper (or is the first white pixel found)
  42.                 # therefore it belongs to the array for the upper line
  43.                 array_upper.append((y, x))
  44.  
  45. # Find the closest distance between the two lines (i.e. the two arrays)
  46. mindistsq = 999999          # square of the minimum distance so far
  47. mincoord1 = (0, 0)
  48. mincoord2 = (0, 0)
  49.  
  50. for coord1 in array_lower:
  51.     for coord2 in array_upper:
  52.         distsq = getdist(coord1, coord2)    # compute the distance squared, in pixels
  53.         if distsq < mindistsq:
  54.             mindistsq = distsq
  55.             mincoord1 = coord1      # Remember the coordinates of the closest white points
  56.             mincoord2 = coord2
  57.  
  58. # Make the closest points red
  59. y, x = mincoord1
  60. for my in range(5):
  61.     for mx in range(5):
  62.         img[y - 2 + my, x - 2 + mx] = (0, 0, 255)       # make pixels red
  63.  
  64. y, x = mincoord2
  65. for my in range(5):
  66.     for mx in range(5):
  67.         img[y - 2 + my, x - 2 + mx] = (0, 0, 255)
  68.  
  69. # Draw a red line between the closest points
  70. diffy = mincoord1[0] - mincoord2[0]     # vertical distance
  71. diffx = mincoord1[1] - mincoord2[1]     # horizontal distance
  72.  
  73. for dy in range(diffy):
  74.     y = mincoord2[0] + dy
  75.     dx = int((diffx * dy) / diffy)
  76.     x = mincoord2[1] + dx
  77.     img[y][x] = (0, 0, 255)     # set pixel colour red
  78.  
  79. mindist = int(mindistsq ** 0.5)
  80. print("Minimum distance beteween the lines is:", mindist)
  81.  
  82. cv2.imshow("img", img)
  83. cv2.waitKey(0)
  84.  
RAW Paste Data