Skip to content

Instantly share code, notes, and snippets.

@socratesk
Created September 18, 2018 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save socratesk/fd6354507ee3c9cc122655d7f391a5fb to your computer and use it in GitHub Desktop.
Save socratesk/fd6354507ee3c9cc122655d7f391a5fb to your computer and use it in GitHub Desktop.
This snippet is used as part of blog post related to HSV
# Initalize webcam. 0 starts built-in camera
cap = cv2.VideoCapture(0)
# Specify HSV range of Tennis Ball
ballHSVLower = np.array([25, 75, 85])
ballHSVUpper = np.array([50, 220, 255])
while True:
# Read captured webcam frame
_, frame = cap.read()
# Convert color frame into HSV
hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Threshold the HSV image to get Tennis ball only
mask = cv2.inRange(hsv_img, ballHSVLower, ballHSVUpper)
# Find contours from HSV masked image
_, contours, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment