Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Script to locate 3 white spots on a black background, which mark the points of a triangle
- # and determine the row and column coordinates of these spots
- # Mike Kerry Jan 2022
- import cv2
- import numpy as np
- # Function to compute the distance (in pixels) between a pair of y, x coordinates
- def distance(yx1, yx2):
- dsq = (yx1[0] - yx2[0]) ** 2 + (yx1[1] - yx2[1]) ** 2
- return round(dsq ** 0.5)
- img = cv2.imread("Triangle.jpg") # Read the image into a Numpy array
- height, width, a = img.shape # Get the number of rows and columns
- spotcoords = []
- for row, rowdata in enumerate(img):
- for col, bgr in enumerate(rowdata):
- bright = sum(bgr) # Compute whiteness by summing Blue + Green + Red
- if bright > 700:
- # See if this pixel is close to an already found spot. If not, it's a new spot
- for tup in spotcoords:
- d = distance(tup, (row, col))
- if d < 20: # Ignore white pixels close to known points
- break
- else:
- spotcoords.append((row, col)) # Append y,x tuple to our coordinates list
- print(spotcoords) # Output: [(194, 467), (317, 170), (354, 290)]
- # Now we know the y,x coordinates of the 3 spots
- cv2.imshow("img", img)
- cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement