HSV
hue 色调
saturation 饱和度
value
OpenCV是0-180,主要是为了可以用Uint8,一个字节表示
做颜色物体跟踪的关键步骤
YCrCb
提取人的皮肤
最常见的HSV与RGB YUV与RGB转换
# -*- coding:utf-8 -*-
# Linda Li 2019/8/15 10:19 cv_01_色彩空间 PyCharm
import cv2 as cv
# RGB色彩空间 黑色(0,0,0) 白色 255,255,255
def color_space_demo(image):
"""BGR图像转换其他色彩空间"""
# 转换为灰度图像
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow("gray", gray)
# 转换为hsv图像
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
cv.imshow("hsv", hsv)
yuv = cv.cvtColor(image, cv.COLOR_BGR2YUV)
cv.imshow("yuv", yuv)
ycrcb = cv.cvtColor(image, cv.COLOR_BGR2YCrCb)
cv.imshow("Ycrcb", ycrcb)
print("-----Hello Python----")
src = cv.imread("../cv_02/1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
color_space_demo(src)
# 如果设置waitKey(0),则表示程序会无限制的等待用户的按键事件
cv.waitKey(0)
cv.destroyAllWindows()
绿色二值化视频
# -*- coding:utf-8 -*-
# Linda Li 2019/8/15 15:04 cv_11_inchange PyCharm
import cv2 as cv
import numpy as np
def extract_object_demo():
capture = cv.VideoCapture("../cv_02/cv_110.mp4")
while True:
ret, frame = capture.read()
# ret为FALSE的时候说明播放完毕了,就退出视频, if ret == False
if not ret:
break
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
# 绿色的范围对应hsv表格找
lower_hsv = np.array([35, 43, 46])
upper_hsv = np.array([77, 255, 255])
mask = cv.inRange(hsv, lower_hsv, upper_hsv)
cv.imshow("video", frame)
cv.imshow("hsv_mask", mask)
# 在这个程序中,我们告诉OpenCv等待用户触发事件,等待时间为40ms,
# 如果在这个时间段内, 用户按下ESC(ASCII码为27),则跳出循环,否则,则跳出循环
c = cv.waitKey(40)
if c == 27:
break
print("-------hello python-------")
extract_object_demo()
cv.waitKey(0)
cv.destroyAllWindows()
split分离通道
# -*- coding:utf-8 -*-
# Linda Li 2019/8/15 10:19 cv_01_色彩空间 PyCharm
import cv2 as cv
print("-----Hello Python----")
src = cv.imread("../cv_02/1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
b, g, r = cv.split(src)
cv.imshow("blue", b)
cv.imshow("green", g)
cv.imshow("red", r)
# 将最后一个通道red设置为0,相当于将红色的通道值没有了,blue和green的合并时发青色
src[:, :, 2] = 0
cv.imshow("changed image", src)
# 如果设置waitKey(0),则表示程序会无限制的等待用户的按键事件
cv.waitKey(0)
cv.destroyAllWindows()
merge 合并通道
# -*- coding:utf-8 -*-
# Linda Li 2019/8/15 10:19 cv_01_色彩空间 PyCharm
import cv2 as cv
print("-----Hello Python----")
src = cv.imread("../cv_02/1.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
b, g, r = cv.split(src)
src1 = cv.merge([b, g, r])
cv.imshow("merge", src1)
# 如果设置waitKey(0),则表示程序会无限制的等待用户的按键事件
cv.waitKey(0)
cv.destroyAllWindows()