Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- def getDist(coord1, coord2):
- return ((coord1[0] - coord2[0]) ** 2 + (coord1[1] - coord2[1]) ** 2) ** 0.5
- grayimg = cv2.imread("FourSpots.jpg", cv2.IMREAD_GRAYSCALE)
- foundSpots = [] # list of tuples of coordinates of top left corner of each dark spot
- for row, aline in enumerate(grayimg): # aline contains one horizontal line of pixels
- for col, pixel in enumerate(aline): # pixel contains whiteness level of one pixel
- if pixel < 85: # Find a dark pixel
- for spot in foundSpots:
- if getDist((row, col), spot) < 50:
- break # pixel is close to an already known spot
- else:
- foundSpots.append((row, col)) # Last loop did NOT end with break
- baseDist = 0
- num = len(foundSpots)
- for x in range(num): # Look at all combinations of 2 spots, and check their separation
- for y in range(x+1, num):
- d = getDist(foundSpots[x], foundSpots[y])
- if d > baseDist:
- baseDist = d # keep the greatest distance
- basePair = (foundSpots[x], foundSpots[y]) # remember the base pair
- otherSpots = [spot for spot in foundSpots if spot not in basePair]
- pairs = ["A-C", "A-D", "B-C", "B-D"]
- for i in basePair:
- for j in otherSpots:
- pc = (100 * getDist(i, j)) / baseDist
- print(f"{pairs.pop(0)} is {pc:.0f}% of A-B")
- # Output:
- # A-C is 81% of A-B
- # A-D is 38% of A-B
- # B-C is 44% of A-B
- # B-D is 66% of A-B
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement