Skip to content

Instantly share code, notes, and snippets.

@joydeepsaha05
Created August 1, 2017 12:48
Show Gist options
  • Save joydeepsaha05/386fd9a3e7a266b06536ebbe7b1ac4d5 to your computer and use it in GitHub Desktop.
Save joydeepsaha05/386fd9a3e7a266b06536ebbe7b1ac4d5 to your computer and use it in GitHub Desktop.
Detects Magenta and Green colours using the webcam. Get the colours from here: https://www.ledr.com/colours/multi.htm
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while 1:
# Take each frame
_, frame = cap.read()
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of magenta color in HSV
lower_magenta = np.array([140, 100, 100])
upper_magenta = np.array([160, 255, 255])
lower_green = np.array([50, 100, 100])
upper_green = np.array([70, 255, 255])
# Threshold the HSV image to get only magenta colors
mask_magenta = cv2.inRange(hsv, lower_magenta, upper_magenta)
mask_green = cv2.inRange(hsv, lower_green, upper_green)
mask = mask_magenta + mask_green
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame, frame, mask=mask)
cv2.imshow('frame', frame)
cv2.imshow('mask_magenta', mask_magenta)
cv2.imshow('mask_green', mask_green)
cv2.imshow('res', res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment