Class 2 颜色过滤,只保留蓝色

This commit is contained in:
Lion Chan 2018-01-28 20:12:02 +08:00
parent 2b3811c8dd
commit 9911effb38
1 changed files with 38 additions and 0 deletions

38
Class002/pcvc002.py Executable file
View File

@ -0,0 +1,38 @@
#!/usr/bin/python3
# coding=utf-8
"""
Python OpenCV Class 002
"""
import cv2
import numpy as np
CAP = cv2.VideoCapture(0)
# 设置蓝色阈值
LOWER_BLUE = np.array([78, 43, 46])
UPPER_BLUE = np.array([110, 255, 255])
while 1:
# 获取一帧图像并且显示
RET, FRAME = CAP.read()
cv2.imshow('Capture', FRAME)
FRAME = cv2.GaussianBlur(FRAME, (5, 5), 1.5)
cv2.imshow('Gaussian', FRAME)
# 切换到 HSV 模式
HSV = cv2.cvtColor(FRAME, cv2.COLOR_BGR2HSV)
# 获取 Mask
MASK = cv2.inRange(HSV, LOWER_BLUE, UPPER_BLUE)
cv2.imshow('Mask', MASK)
# Mask
RES = cv2.bitwise_and(FRAME, FRAME, mask=MASK)
cv2.imshow('Result', RES)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
CAP.release()
cv2.destroyAllWindows()