Advertisement
Guest User

Untitled

a guest
Mar 17th, 2017
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. """
  2. How to use it:
  3. - python camshift.py
  4. - press `i` button
  5. - choose 4 points around detected object
  6. - press `enter`
  7.  
  8. """
  9.  
  10. import numpy as np
  11. import cv2
  12.  
  13.  
  14. frame = None
  15. roiPts = []
  16. inputMode = False
  17.  
  18. # Getting the camera reference
  19. cap = cv2.VideoCapture(0)
  20.  
  21. # Callback function to get the ROI by clicking into four points
  22. def click_and_crop(event, x, y, flags, param):
  23.     # grab the reference to the current frame, list of ROI
  24.     # points and whether or not it is ROI selection mode
  25.     global frame, roiPts, inputMode
  26.  
  27.     # if we are in ROI selection mode, the mouse was clicked,
  28.     # and we do not already have four points, then update the
  29.     # list of ROI points with the (x, y) location of the click
  30.     # and draw the circle
  31.     if inputMode and event == cv2.EVENT_LBUTTONDOWN and len(roiPts) < 4:
  32.         roiPts.append((x, y))
  33.         cv2.circle(frame, (x, y), 4, (0, 255, 0), 2)
  34.         cv2.imshow("image", frame)
  35.  
  36. # Attaching the callback into the video window
  37. cv2.namedWindow("image")
  38. cv2.setMouseCallback("image", click_and_crop)
  39.  
  40.  
  41. # Setup the termination criteria, either 10 iteration or move by atleast 1 pt
  42. term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
  43. roiBox = None
  44.  
  45.  
  46. # Main loop
  47. while(1):
  48.     ret ,frame = cap.read()
  49.  
  50.     if roiBox is not None:
  51.         # Making the frame into HSV and backproject the HSV frame
  52.         hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  53.         dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
  54.  
  55.         # Apply meanshift to get the new location
  56.         ret, roiBox = cv2.CamShift(dst, roiBox, term_crit)
  57.  
  58.         # Draw it on image
  59.         pts = cv2.cv.BoxPoints(ret)
  60.         pts = np.int0(pts)
  61.         cv2.polylines(frame,[pts],True, 255,2)
  62.  
  63.         # Draw the center
  64.         cx = (pts[0][0]+pts[1][0])/2
  65.         cy = (pts[0][1]+pts[2][1])/2
  66.         cv2.circle(frame, (cx, cy), 4, (0, 255, 0), 2)
  67.         # cv2.imshow('img2',frame)
  68.  
  69.     # handle if the 'i' key is pressed, then go into ROI
  70.     # selection mode
  71.     cv2.imshow("image", frame)
  72.     key = cv2.waitKey(1) & 0xFF
  73.     if key == ord("i") and len(roiPts) < 4:
  74.         # indicate that we are in input mode and clone the
  75.         # frame
  76.         inputMode = True
  77.         orig = frame.copy()
  78.          
  79.         # keep looping until 4 reference ROI points have
  80.         # been selected; press any key to exit ROI selction
  81.         # mode once 4 points have been selected
  82.         while len(roiPts) < 4:
  83.             cv2.imshow("image", frame)
  84.             cv2.waitKey(0)
  85.          
  86.         # determine the top-left and bottom-right points
  87.         roiPts = np.array(roiPts)
  88.         s = roiPts.sum(axis = 1)
  89.         tl = roiPts[np.argmin(s)]
  90.         br = roiPts[np.argmax(s)]
  91.          
  92.         # grab the ROI for the bounding box and convert it
  93.         # to the HSV color space
  94.         roi = orig[tl[1]:br[1], tl[0]:br[0]]
  95.         roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
  96.          
  97.         # compute a HSV histogram for the ROI and store the
  98.         # bounding box
  99.         roi_hist = cv2.calcHist([roi], [0], None, [16], [0, 180])
  100.         roi_hist = cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
  101.         roiBox = (tl[0], tl[1], br[0], br[1])
  102.  
  103.     # k = cv2.waitKey(60) & 0xff
  104.     if key == 27:
  105.         break
  106.        
  107.  
  108. cv2.destroyAllWindows()
  109. cap.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement