Skip to content

Instantly share code, notes, and snippets.

@victormurcia
Created October 20, 2022 16:48
Show Gist options
  • Select an option

  • Save victormurcia/b8e4351cd7952175ff02e54e90b412a9 to your computer and use it in GitHub Desktop.

Select an option

Save victormurcia/b8e4351cd7952175ff02e54e90b412a9 to your computer and use it in GitHub Desktop.
masking colors
#Remove green background/field from image prior to clustering
green = np.array([60,255,255]) #This is green in HSV
loGreen = np.array([30,25,25]) #low green threshold
hiGreen = np.array([90,255,255]) #Upper green threshold
loBlue = np.array([0,25,25]) #low red threshold
hiBlue = np.array([30,255,255]) #Upper red threshold
loRed = np.array([120,25,25]) #low blue threshold
hiRed = np.array([180,255,255]) #Upper blue threshold
#Convert image to HSV
hsv = cv2.cvtColor(rgb_img, cv2.COLOR_BGR2HSV)
gmask = cv2.inRange(hsv, loGreen, hiGreen)
rmask = cv2.inRange(hsv, loRed , hiRed)
bmask = cv2.inRange(hsv, loBlue , hiBlue)
gresult = rgb_img.copy()
bresult = rgb_img.copy()
rresult = rgb_img.copy()
gresult[gmask==255] = (255,255,255)
bresult[bmask==255] = (255,255,255)
rresult[rmask==255] = (255,255,255)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment