Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. cap = cv2.VideoCapture('/home/hydrofire/Study/ITS/Lab5/Material-lab5-2018/motion.avi')
  5.  
  6. tracking_params = dict(winSize = (10, 10),
  7. maxLevel = 4,
  8. criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
  9.  
  10. feature_params = dict( maxCorners = 50,
  11. qualityLevel = 0.15,
  12. minDistance = 15)
  13.  
  14. _, old_frame = cap.read()
  15. old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
  16. p0 = cv2.goodFeaturesToTrack(old_gray, **feature_params)
  17.  
  18. red = [0,0,255]
  19. green = [0,255,0]
  20.  
  21. mask = np.zeros_like(old_frame)
  22. while cap.isOpened():
  23. _, frame = cap.read()
  24.  
  25.  
  26. if frame is not None:
  27.  
  28. frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  29.  
  30. p0 = cv2.goodFeaturesToTrack(frame_gray, **feature_params)
  31. p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **tracking_params)
  32.  
  33.  
  34. for i, (new, old) in enumerate(zip(p1,p0)):
  35. a, b = new.ravel()
  36. c, d = old.ravel()
  37. #frame = cv2.circle(frame, (a, b), 3, (0, 255, 0), -1)
  38. #frame = cv2.circle(frame, (c, d), 3, (0, 0, 255), -1)
  39. mask = cv2.line(mask, (a, b), (c, d), red, 2)
  40. frame = cv2.circle(frame, (a, b), 5, green)
  41.  
  42. img = cv2.add(frame, mask)
  43. cv2.imshow('frame', img)
  44.  
  45. #cv2.imshow('frame', frame)
  46. old_gray = frame_gray.copy()
  47.  
  48.  
  49. if cv2.waitKey(1) & 0xFF == ord('q'):
  50. break
  51.  
  52.  
  53.  
  54. cap.release()
  55. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement