Advertisement
acclivity

pyFindCoordsOfWhiteSpots

Feb 4th, 2022
1,612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. # Script to locate 3 white spots on a black background, which mark the points of a triangle
  2. # and determine the row and column coordinates of these spots
  3.  
  4. # Mike Kerry Jan 2022
  5.  
  6. import cv2
  7. import numpy as np
  8.  
  9. # Function to compute the distance (in pixels) between a pair of y, x coordinates
  10. def distance(yx1, yx2):
  11.     dsq = (yx1[0] - yx2[0]) ** 2 + (yx1[1] - yx2[1]) ** 2
  12.     return round(dsq ** 0.5)
  13.  
  14. img = cv2.imread("Triangle.jpg")        # Read the image into a Numpy array
  15. height, width, a = img.shape            # Get the number of rows and columns
  16.  
  17. spotcoords = []
  18. for row, rowdata in enumerate(img):
  19.     for col, bgr in enumerate(rowdata):
  20.         bright = sum(bgr)               # Compute whiteness by summing Blue + Green + Red
  21.         if bright > 700:
  22.             # See if this pixel is close to an already found spot. If not, it's a new spot
  23.             for tup in spotcoords:
  24.                 d = distance(tup, (row, col))
  25.                 if d < 20:              # Ignore white pixels close to known points
  26.                     break
  27.             else:
  28.                 spotcoords.append((row, col))       # Append y,x tuple to our coordinates list
  29.  
  30. print(spotcoords)            # Output:  [(194, 467), (317, 170), (354, 290)]
  31. # Now we know the y,x coordinates of the 3 spots
  32.  
  33. cv2.imshow("img", img)
  34. cv2.waitKey(0)
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement