Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import time
  4.  
  5. cap = cv2.VideoCapture(0)
  6. upperOrange = np.array([10,255,255])
  7. lowerOrange = np.array([0,150,150])
  8. cnts = []
  9. while(1):
  10. ret, img = cap.read()
  11. k = cv2.waitKey(30) & 0xff
  12. frame = cv2.resize(img, (0,0), fx=0.25, fy=0.25)
  13. if k == 27:
  14. break
  15. start = time.time()
  16. hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  17. mask = cv2.inRange(hsv, lowerOrange, upperOrange)
  18. _, cnts, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  19. if len(cnts) > 0:
  20. # find the largest contour in the mask, then use
  21. # it to compute the minimum enclosing circle and
  22. # centroid
  23. c = max(cnts, key=cv2.contourArea)
  24. ((x, y), radius) = cv2.minEnclosingCircle(c)
  25. M = cv2.moments(c)
  26. if M["m00"] != 0:
  27. center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
  28.  
  29. # only proceed if the radius meets a minimum size
  30. if radius > 3:
  31. # draw the circle and centroid on the frame,
  32. # then update the list of tracked points
  33. cv2.circle(frame, (int(x), int(y)), int(radius),
  34. (0, 255, 255), 2)
  35. cv2.circle(frame, center, 5, (0, 0, 255), -1)
  36. end = time.time()
  37. print(end-start)
  38. cv2.imshow('frame',frame)
  39.  
  40.  
  41. cap.release()
  42. cv2.destroyAllWindows()
  43. cv2.waitKey(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement