Guest User

Untitled

a guest
Mar 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. import math
  4. colors = [(255,0,0), (125,125,125), (0,255,0), (0,0,255), (125,125,0)]
  5. new = 0
  6. track = []
  7. radius = 18
  8. thickness = 5
  9. cap = cv2.VideoCapture(0)
  10. for i in range(930): ret, img = cap.read()
  11. while True:
  12. ret, img = cap.read()
  13. img = img[0:480, 100:700]
  14. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  15. ret, thresh = cv2.threshold(gray, 233, 255, 0)
  16. contours, h, g = cv2.findContours(thresh, cv2.RETR_TREE,
  17. cv2.CHAIN_APPROX_SIMPLE)
  18. locations = []
  19. for i in h:
  20. area = cv2.contourArea(i)
  21. if area > 300:
  22. location = cv2.minEnclosingCircle(i)
  23. locations.append(location[0])
  24. if len(locations) < len(track):
  25. print("'problem'")
  26. min_distances = []
  27. for i in track:
  28. x,y = i[1][0], i[1][1]
  29. distance = []
  30. for c in locations:
  31. x1, y1 = c[0], c[1]
  32. distance.append(math.sqrt((x-x1)**2 + (y-y1)**2))
  33. if not distance: continue
  34. min_distances.append(min(distance))
  35. index = min_distances.index(max(min_distances))
  36. locations.append(track[index][1])
  37. if new == 0:
  38. for i in range(len(locations)):
  39. track.append((colors[i], locations[i]))
  40. new = 1
  41. num_balls = len(locations)
  42. else:
  43. new_track = []
  44. for i in locations:
  45. x,y = i[0], i[1]
  46. distance = []
  47. for c in track:
  48. x1, y1 = c[1][0], c[1][1]
  49. distance.append(math.sqrt((x-x1)**2+(y-y1)**2))
  50. index = distance.index(min(distance))
  51. new_track.append((track[index][0], (x,y)))
  52. track = new_track
  53.  
  54. for i in track:
  55. center = int(i[1][0]),int(i[1][1])
  56. cv2.circle(img, center, radius, i[0], thickness)
  57. cv2.imshow('Detected Balls', img)
  58. cv2.waitKey(100)
  59. cv2.destroyAllWindows()
  60. cv2.release(cap)
Add Comment
Please, Sign In to add comment