Advertisement
acclivity

pyJoinSpotCentres

Jul 22nd, 2023
1,348
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | Software | 1 0
  1. import cv2
  2. import math
  3. def getDist(coord1, coord2):
  4.     return ((coord1[0] - coord2[0]) ** 2 + (coord1[1] - coord2[1]) ** 2) ** 0.5
  5.  
  6. def getAngle(coord1, coord2):
  7.     return math.atan2(coord1[0] - coord2[0], coord1[1] - coord2[1])
  8.  
  9. img = cv2.imread("FourSpotsV2.jpg")
  10. grayimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  11. foundSpots = {}             # Dictionary of dark spots.
  12. spotCount = 0
  13. for row, aline in enumerate(grayimg):           # aline contains one horizontal line of pixels
  14.     for col, pixel in enumerate(aline):         # pixel contains whiteness level of one pixel
  15.         if pixel < 85:                          # Find a dark pixel
  16.             for spotKey in foundSpots:          # Compare with all spots found so far
  17.                 pData = foundSpots[spotKey]     # [coords 1st pixel], [minx, miny], [maxx, maxy]
  18.                 spot = pData[0]                 # get coords of first pixel of spot
  19.                 if getDist((col, row), spot) < 100:         # Part of same spot?
  20.                     pData[1][0] = min(pData[1][0], col)         # keep minimum x coord
  21.                     pData[1][1] = min(pData[1][1], row)         # keep minimum y coord
  22.                     pData[2][0] = max(pData[2][0], col)         # keep maximum x coord
  23.                     pData[2][1] = max(pData[2][1], row)         # keep maximum y coord
  24.                     foundSpots[spotKey] = pData
  25.                     break                       # pixel is close to an already known spot
  26.             else:
  27.                 spotCount += 1                  # start of a new spot
  28.                 foundSpots[spotCount] = [[col, row], [col, row], [col, row]]            # Start a new spot
  29.  
  30. spotCentres = []
  31. for spotKey in foundSpots:              # # Find the centre pixel of each dark spot
  32.     pData = foundSpots[spotKey]
  33.     avgCol = (pData[1][0] + pData[2][0]) // 2       # mean of min and max column
  34.     avgRow = (pData[1][1] + pData[2][1]) // 2       # mean of min and max row
  35.     spotCentres.append((avgCol, avgRow))
  36.  
  37. # Find the coordinates of the centre of the whole group of spots
  38. totCol = sum([spot[0] for spot in spotCentres])     # Add up all the spot columns
  39. totRow = sum([spot[1] for spot in spotCentres])     # Add up all the spot rows
  40. figCentre = (totCol // len(spotCentres), totRow // len(spotCentres))
  41.  
  42. # Order the found spots in rotation order as viewed from the centre point
  43. spotsByAngle = sorted([(getAngle(spot, figCentre), spot) for spot in spotCentres])
  44. last = spotsByAngle[-1][1]
  45. for item in spotsByAngle:       # draw line joining spot pairs, in rotation sequence
  46.     cv2.line(img, last, item[1], (0, 0, 255), 1)
  47.     last = item[1]
  48.  
  49. cv2.imshow("img", img)
  50. cv2.waitKey(0)
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement