SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import cv2
  2. import numpy as np
  3.  
  4. RED = [
  5.     [160,100,100],
  6.     [179,255,255]    
  7. ]
  8.  
  9. BLUE = [
  10.     [110,50,50],
  11.     [130, 255,255]
  12. ]
  13.  
  14.  
  15.  
  16. def isolate_by_color(filename="blueball.jpg", lower_bound=[110,50,50], upper_bound=[130, 255,255], color=None):
  17.     img = cv2.imread(filename)
  18.  
  19.     hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  20.  
  21.     if color == None:
  22.         lower_color = np.array(lower_bound)
  23.         upper_color = np.array(upper_bound)
  24.  
  25.         mask = cv2.inRange(hsv, lower_color, upper_color)
  26.     else:
  27.        
  28.         lower_color1 = np.array(color[0])
  29.         upper_color1 = np.array(color[1])
  30.  
  31.         mask = cv2.inRange(hsv, lower_color1, upper_color1)
  32.         #mask = cv2.inRange(mask, lower_color1, upper_color1)
  33.  
  34.     res = cv2.bitwise_and(img, img, mask=mask)
  35.  
  36.     cv2.imshow("result",res)
  37.    
  38.  
  39.     k = cv2.waitKey(0)
  40.     if k == ord('q'):
  41.         cv2.destroyAllWindows()
  42.  
  43. if __name__ == "__main__":
  44.     #isolate_by_color("blueball.jpg")
  45.     #isolate_by_color("girl.jpeg", lower_bound=[0, 100, 100], upper_bound=[10,255,255])
  46.     isolate_by_color("girl.jpeg", color=red)
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