SHARE
TWEET

Untitled

a guest Jul 21st, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv2 as cv
  2. import numpy as np
  3.  
  4. # optional argument for trackbars
  5. def nothing(x):
  6.     pass
  7.  
  8. # named ites for easy reference
  9. barsWindow = 'Bars'
  10. hl = 'H Low'
  11. hh = 'H High'
  12. sl = 'S Low'
  13. sh = 'S High'
  14. vl = 'V Low'
  15. vh = 'V High'
  16.  
  17. # set up for video capture on camera 0
  18. cap = cv.VideoCapture(0)
  19.  
  20. # create window for the slidebars
  21. cv.namedWindow(barsWindow, flags = cv.WINDOW_AUTOSIZE)
  22.  
  23. # create the sliders
  24. cv.createTrackbar(hl, barsWindow, 0, 179, nothing)
  25. cv.createTrackbar(hh, barsWindow, 0, 179, nothing)
  26. cv.createTrackbar(sl, barsWindow, 0, 255, nothing)
  27. cv.createTrackbar(sh, barsWindow, 0, 255, nothing)
  28. cv.createTrackbar(vl, barsWindow, 0, 255, nothing)
  29. cv.createTrackbar(vh, barsWindow, 0, 255, nothing)
  30.  
  31. # set initial values for sliders
  32. cv.setTrackbarPos(hl, barsWindow, 0)
  33. cv.setTrackbarPos(hh, barsWindow, 179)
  34. cv.setTrackbarPos(sl, barsWindow, 0)
  35. cv.setTrackbarPos(sh, barsWindow, 255)
  36. cv.setTrackbarPos(vl, barsWindow, 0)
  37. cv.setTrackbarPos(vh, barsWindow, 255)
  38.  
  39. while(True):
  40.     ret, frame = cap.read()
  41.     frame = cv.GaussianBlur(frame, (5, 5), 0)
  42.    
  43.     # convert to HSV from BGR
  44.     hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
  45.  
  46.     # read trackbar positions for all
  47.     hul = cv.getTrackbarPos(hl, barsWindow)
  48.     huh = cv.getTrackbarPos(hh, barsWindow)
  49.     sal = cv.getTrackbarPos(sl, barsWindow)
  50.     sah = cv.getTrackbarPos(sh, barsWindow)
  51.     val = cv.getTrackbarPos(vl, barsWindow)
  52.     vah = cv.getTrackbarPos(vh, barsWindow)
  53.  
  54.     # make array for final values
  55.     HSVLOW = np.array([hul, sal, val])
  56.     HSVHIGH = np.array([huh, sah, vah])
  57.  
  58.     # apply the range on a mask
  59.     mask = cv.inRange(hsv, HSVLOW, HSVHIGH)
  60.     maskedFrame = cv.bitwise_and(frame, frame, mask = mask)
  61.  
  62.     # display the camera and masked images
  63.     cv.imshow('Masked', maskedFrame)
  64.     cv.imshow('Camera', frame)
  65.  
  66.     # check for q to quit program with 5ms delay
  67.     if cv.waitKey(5) & 0xFF == ord('q'):
  68.         break
  69.  
  70. # clean up our resources
  71. cap.release()
  72. cv.destroyAllWindows()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top