import numpy as np
import cv2
import time
cam = cv2.VideoCapture('ling-f1-good1.mp4')
while(cam.isOpened()):#Repeat until the end of the video
t = time.time()
#Time measurement
ret, img = cam.read()
#Cut into image
if not ret:
break
#End if the video is the last
img = cv2.resize(img, (640, 380))
#Image resizing
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV_FULL)
#Convert to HSV
h = hsv[:, :, 0] #H is the hue
s = hsv[:, :, 1]
mask = np.zeros(h.shape, dtype=np.uint8)
#Create an empty array for the mask
mask[((h < 20) | (h > 200)) & (s > 60)] = 255
#Create a mask at 200 to 20 degrees * Mask other than red
#Masking process
masked_img = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow("Frame", masked_img)
print(time.time() - t, ret)
if cv2.waitKey(1) & 0xFF == ord('q'): break
#Finish when "q" is pressed
Recommended Posts