Advertisement
j0h

ColourPick.py

j0h
Sep 7th, 2016
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 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. #img = np.zeros((300,512,3), np.uint8)
  9. cv2.namedWindow('image')
  10. cv2.namedWindow('hsv')
  11. cv2.namedWindow('masq')
  12. cap = cv2.VideoCapture(0)
  13.  
  14. # create trackbars for color change
  15. cv2.createTrackbar('R-low','image',0,255,nothing)
  16. cv2.createTrackbar('R-high','image',255,255,nothing)
  17.  
  18. cv2.createTrackbar('G-low','image',0,255,nothing)
  19. cv2.createTrackbar('G-high','image',255,255,nothing)
  20.  
  21. cv2.createTrackbar('B-low','image',0,255,nothing)
  22. cv2.createTrackbar('B-high','image',255,255,nothing)
  23.  
  24.  
  25. while(1):
  26.     ret, img = cap.read()
  27.     # Convert BGR to HSV
  28.     hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  29.  
  30.     cv2.imshow('image',img)
  31.     k = cv2.waitKey(1) & 0xFF
  32.     if k == 27:
  33.         break
  34.  
  35.    
  36.     # get current positions of four trackbars
  37.     rl = cv2.getTrackbarPos('R-low','image')
  38.     rh = cv2.getTrackbarPos('R-high','image')
  39.  
  40.     gl = cv2.getTrackbarPos('G-low','image')
  41.     gh = cv2.getTrackbarPos('G-high','image')
  42.    
  43.     bl = cv2.getTrackbarPos('B-low','image')
  44.     bh = cv2.getTrackbarPos('B-high','image')
  45.  
  46.     lower = np.array([rl,gl,bl])
  47.     upper = np.array([rh,gh,bh])
  48.  
  49. #    print(rl)
  50.  
  51.     img[:] = [bl,gl,rl]
  52.  
  53.     # Threshold the HSV image to get only certain colors
  54.     mask = cv2.inRange(hsv, lower, upper)    
  55.  
  56.     res = cv2.bitwise_and(img,img, mask= mask)
  57.    
  58.     #cv2.imshow('res',res)
  59.     cv2.imshow('masq',mask)
  60.     cv2.imshow('hsv',hsv)
  61.  
  62.  
  63. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement