Advertisement
Guest User

Untitled

a guest
May 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3.  
  4. cap = cv2.VideoCapture('videos/slow_traffic_small.mp4')
  5.  
  6. # take first frame of the video
  7. ret,frame = cap.read()
  8.  
  9. # setup initial location of window
  10. # r,h,c,w - region of image
  11. # simply hardcoded the values
  12. r,h,c,w = 200,20,300,20
  13. track_window = (c,r,w,h)
  14.  
  15. # set up the ROI for tracking
  16. roi = frame[r:r+h, c:c+w]
  17. hsv_roi = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  18. mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
  19. roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
  20. cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
  21.  
  22. # Setup the termination criteria, either 10 iteration or move by at least 1 pt
  23. term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
  24.  
  25. while(1):
  26. ret ,frame = cap.read()
  27.  
  28. if ret == True:
  29. hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  30. dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
  31.  
  32. # apply meanshift to get the new location
  33. ret, track_window = cv2.meanShift(dst, track_window, term_crit)
  34.  
  35. # Draw it on image
  36. x,y,w,h = track_window
  37. img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2)
  38. cv2.imshow('img2',img2)
  39.  
  40. k = cv2.waitKey(60) & 0xff
  41. if k == 27:
  42. break
  43. else:
  44. cv2.imwrite(chr(k)+".jpg",img2)
  45.  
  46. else:
  47. break
  48.  
  49. cv2.destroyAllWindows()
  50. cap.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement