Advertisement
acclivity

pyDistanceBetween2Lines

Jan 12th, 2022
1,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. # Find the minimum distanve between two white lines
  2. # Mike Kerry - Jan 2022 - acclivity2@gmail.com
  3.  
  4. import cv2
  5. import numpy as np
  6.  
  7. img = cv2.imread("WhiteLinesCrop.jpg")
  8. dims = img.shape
  9. width = dims[1]
  10. height = dims[0]
  11. print("Width:", width, "  Height:", height)
  12.  
  13. cv2.imshow("img", img)
  14.  
  15. array_tr = []   # This array will hold all the coordinates of white pixels in the upper right line
  16. array_bl = []   # This array will hold all the coordinates of white pixels in the lower left line
  17.  
  18. for y, line in enumerate(img):          # y is line counter (vertical coordinate)
  19.     for x, rgb in enumerate(line):      # x is the horizontal coordinate
  20.         # Is this pixel white?
  21.         if sum(rgb) > 384:              # look for pale pixels  (765 would be pure white, 0 is black)
  22.             # Got a white pixel
  23.             # Is this pixel nearer to the top right, or the bottom left?
  24.             dist_bl = (height - y) ** 2 + x ** 2
  25.             dist_tr = y ** 2 + (width - x) ** 2
  26.             if dist_bl < dist_tr:
  27.                 # This pixel is in the lower left line. Add its coordinates to the bottom left array
  28.                 array_bl.append((y, x))
  29.                 # TESTING ... make bottom left BLUE
  30.                 # img[y, x] = (255, 0, 0)
  31.             else:
  32.                 # this pixel is in the upper right line
  33.                 array_tr.append((y, x))         # Add its coordinates to the top right array
  34.  
  35. # Find the closest distance between the two lines (i.e. the two arrays)
  36. mindistsq = 999999          # square of the minimum distance
  37. mincoord1 = (0, 0)
  38. mincoord2 = (0, 0)
  39.  
  40. for coord1 in array_bl:
  41.     for coord2 in array_tr:
  42.         # compute the distance squared, in pixels
  43.         distsq = (coord1[0] - coord2[0]) ** 2 + (coord1[1] - coord2[1]) ** 2
  44.         if distsq < mindistsq:
  45.             mindistsq = distsq
  46.             mincoord1 = coord1      # Remember the coordinates of the closest white points
  47.             mincoord2 = coord2
  48.  
  49. # Make the closest points red
  50. y, x = mincoord1
  51. for my in range(5):
  52.     for mx in range(5):
  53.         img[y - 2 + my, x - 2 + mx] = (0, 0, 255)       # make pixels red
  54.  
  55. y, x = mincoord2
  56. for my in range(5):
  57.     for mx in range(5):
  58.         img[y - 2 + my, x - 2 + mx] = (0, 0, 255)
  59.  
  60. mindist = int(mindistsq ** 0.5)
  61. print("Minimum distance beteween the lines is:", mindist)
  62.  
  63. cv2.imshow("img", img)
  64. cv2.waitKey(0)
  65.  
  66.  
  67. input("Enter:")
  68.  
  69.  
  70.  
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement