Guest User

Untitled

a guest
Feb 23rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. def nothing(x):
  5. pass
  6.  
  7. # Create a black image, a window
  8. cv2.namedWindow('Trackbar window')
  9.  
  10. #Open webcam, choose the nbr change '0' to nbr of your webcam
  11. capture = cv2.VideoCapture(0)
  12.  
  13. # create trackbars for color change
  14. cv2.createTrackbar('H_high','Trackbar window',0,255,nothing)
  15. cv2.createTrackbar('S_high','Trackbar window',0,255,nothing)
  16. cv2.createTrackbar('V_high','Trackbar window',0,255,nothing)
  17. cv2.createTrackbar('H_low','Trackbar window',0,255,nothing)
  18. cv2.createTrackbar('S_low','Trackbar window',0,255,nothing)
  19. cv2.createTrackbar('V_low','Trackbar window',0,255,nothing)
  20.  
  21. #Setting the "high" trackbars to max values (255)
  22. cv2.setTrackbarPos('H_high','Trackbar window', 255)
  23. cv2.setTrackbarPos('S_high','Trackbar window', 255)
  24. cv2.setTrackbarPos('V_high','Trackbar window', 255)
  25.  
  26. while(1):
  27. _, frame = capture.read()
  28. cv2.imshow('Trackbar window', np.zeros((1,512,3), np.uint8))
  29.  
  30. _f = cv2.medianBlur(frame, 15)
  31. _f = cv2.cvtColor(_f, cv2.COLOR_BGR2HSV) #To HSV
  32.  
  33. # get current positions of four trackbars
  34. #These are HSV values
  35. h_low = cv2.getTrackbarPos('H_low','Trackbar window')
  36. s_low = cv2.getTrackbarPos('S_low','Trackbar window')
  37. v_low = cv2.getTrackbarPos('V_low','Trackbar window')
  38. h_high = cv2.getTrackbarPos('H_high','Trackbar window')
  39. s_high = cv2.getTrackbarPos('S_high','Trackbar window')
  40. v_high = cv2.getTrackbarPos('V_high','Trackbar window')
  41.  
  42. # define range of color in HSV
  43. lower_bound = np.array([h_low,s_low,v_low])
  44. upper_bound = np.array([h_high,s_high,v_high])
  45.  
  46.  
  47. mask = cv2.inRange(_f, lower_bound, upper_bound)
  48. frame = cv2.bitwise_and(frame, frame, mask = mask) #Comment this line if you won't show the frame later
  49.  
  50. #Comment the one you won't need
  51. cv2.imshow('frame',frame)
  52. #cv2.imshow('mask',mask)
  53.  
  54.  
  55. k = cv2.waitKey(1) & 0xFF
  56. if k == 27: #press escape to exit
  57. break
  58.  
  59.  
  60. capture.release() #Release the camera
  61. cv2.destroyAllWindows() #Close all windows
Add Comment
Please, Sign In to add comment