Advertisement
acclivity

pyProcessDarkSpots

Jul 20th, 2023 (edited)
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | Software | 0 0
  1. import cv2
  2. def getDist(coord1, coord2):
  3.     return ((coord1[0] - coord2[0]) ** 2 + (coord1[1] - coord2[1]) ** 2) ** 0.5
  4.  
  5. grayimg = cv2.imread("FourSpots.jpg", cv2.IMREAD_GRAYSCALE)
  6. foundSpots = []                  # list of tuples of coordinates of top left corner of each dark spot
  7. for row, aline in enumerate(grayimg):           # aline contains one horizontal line of pixels
  8.     for col, pixel in enumerate(aline):         # pixel contains whiteness level of one pixel
  9.         if pixel < 85:                          # Find a dark pixel
  10.             for spot in foundSpots:
  11.                 if getDist((row, col), spot) < 50:
  12.                     break                       # pixel is close to an already known spot
  13.             else:
  14.                 foundSpots.append((row, col))      # Last loop did NOT end with break
  15. baseDist = 0
  16. num = len(foundSpots)
  17. for x in range(num):            # Look at all combinations of 2 spots, and check their separation
  18.     for y in range(x+1, num):
  19.         d = getDist(foundSpots[x], foundSpots[y])
  20.         if d > baseDist:
  21.             baseDist = d                                    # keep the greatest distance
  22.             basePair = (foundSpots[x], foundSpots[y])       # remember the base pair
  23. otherSpots = [spot for spot in foundSpots if spot not in basePair]
  24. pairs = ["A-C", "A-D", "B-C", "B-D"]
  25. for i in basePair:
  26.     for j in otherSpots:
  27.         pc = (100 * getDist(i, j)) / baseDist
  28.         print(f"{pairs.pop(0)} is {pc:.0f}% of A-B")
  29.  
  30. # Output:
  31. # A-C is 81% of A-B
  32. # A-D is 38% of A-B
  33. # B-C is 44% of A-B
  34. # B-D is 66% of A-B
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement