# Importing a library OpenCV for working with images and videos
import cv2
# Importing a library NumPy for working with numeric data
import numpy as np
# Capture streaming video from camcorder 0
cap = cv2.VideoCapture(0)
# Creating an endless loop
while(1):
# Receive video frame parameter size is ignored
_, frame = cap.read()
# Convert image frame from BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of color in HSV
lower_red = np.array([0,0,0])
upper_red = np.array([255,255,180])
# Threshold HSV image to get only the selected color
mask = cv2.inRange(hsv, lower_red, upper_red)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)
# Show the video frame of the mask used and the result of the mask overlay
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
# Pause for 5 milliseconds if ESC is pressed, exit from endless cycle
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
# Closing all windows created OpenCV
cv2.destroyAllWindows()
# Freeing up the resources of the involved video camera
cap.release()
Comments
Leave a comment