Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Dec 19 22:43:24 2014
  4.  
  5. """
  6.  
  7. import cv2
  8. import numpy as np
  9.  
  10. def nothing(x): #needed for the trackbars
  11.     pass
  12.  
  13. cv2.namedWindow('Calibration')
  14.  
  15. # create trackbars for color change
  16. cv2.createTrackbar('Hmin','Calibration',0,179,nothing)
  17. cv2.createTrackbar('Hmax','Calibration',0,179,nothing)
  18.  
  19. cv2.createTrackbar('Smin','Calibration',0,255,nothing)
  20. cv2.createTrackbar('Smax','Calibration',0,255,nothing)
  21.  
  22. cv2.createTrackbar('Vmin','Calibration',0,255,nothing)
  23. cv2.createTrackbar('Vmax','Calibration',0,255,nothing)
  24.  
  25. webcam = cv2.VideoCapture(0)
  26. webcam.grab()
  27.  
  28. while(1):
  29.  
  30.     k = cv2.waitKey(1) & 0xFF
  31.     if k == 27:
  32.         break
  33.  
  34.     ret, frame = webcam.retrieve(channel=0)
  35.     calibration_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  36.    
  37.     # get current positions of trackbars
  38.     hmin = min(cv2.getTrackbarPos('Hmin','Calibration'),cv2.getTrackbarPos('Hmax','Calibration'))
  39.     hmax = max(cv2.getTrackbarPos('Hmax','Calibration'),cv2.getTrackbarPos('Hmin','Calibration'))
  40.    
  41.     smin = min(cv2.getTrackbarPos('Smin','Calibration'),cv2.getTrackbarPos('Smax','Calibration'))
  42.     smax = max(cv2.getTrackbarPos('Smax','Calibration'),cv2.getTrackbarPos('Smin','Calibration'))
  43.  
  44.     vmin = min(cv2.getTrackbarPos('Vmin','Calibration'),cv2.getTrackbarPos('Vmax','Calibration'))
  45.     vmax = max(cv2.getTrackbarPos('Vmax','Calibration'),cv2.getTrackbarPos('Vmin','Calibration'))
  46.  
  47.     # Threshold the HSV image to get only selected color
  48.     lower_thr = np.array([hmin, smin, vmin], dtype=np.uint8)
  49.     upper_thr = np.array([hmax,smax,vmax], dtype=np.uint8)
  50.     mask = cv2.inRange(calibration_hsv, lower_thr, upper_thr)
  51.    
  52.     cv2.imshow('Calibration',mask)
  53.  
  54. webcam.release()
  55. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement