# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:tzting
@File: 色彩空间.py
@Time: 2020/11/11 21:20
"""
import tensorflow as tf
import cv2 as cv
import numpy as np
"""
色彩空间
常见色彩空间 RGB,HSV(H:0-180,S:0-255,V:0-255)归一化,主要用来找某一特征颜色,明显差异化,HIS,YCrCb检测皮肤颜色
代码层面知识点:
1、色彩空间转换API
2、学会用inRange色彩空间分离
3、通道分离与合并
"""
# 色彩空间转换
def color_space_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow("gray",gray)
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)
# 读视频
def extract_object_demo():
capture = cv.VideoCapture("C:/Users/tzt/Desktop/opencv-python/ex.mp4")
while(True):
ret, frame = capture.read() # 如果有的话返回真,如果没有的话,返回flase,frame返回图像,ret返回值
if ret == False:
break;
# 用inrange色彩空间分离,不过要先转化为hsv图像空间!!!
hsv = cv.cvtColor(frame,cv.COLOR_BGR2HSV)
lower_hsv = np.array([35,43,46]) # 绿色的低值
higher_hsv = np.array([77,255,255]) # 绿色的高值
mask = cv.inRange(hsv, lower_hsv,higher_hsv) # 得到二值图像
cv.imshow("video",frame)
cv.imshow("mask",mask)
c = cv.waitKey(40)
if c == 27: # braek的值
break;
src = cv.imread("C:/Users/tzt/Desktop/opencv-python/girl.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
# extract_object_demo()
# 通道分离与合并
b, g, r =cv.split(src) # 分离
cv.imshow("blue",b)
cv.imshow("green",g)
cv.imshow("red", r)
# 对某一通道进行赋值
# src[:,:, 2] = 0 # 将红色通道赋值为0
src = cv.merge([b, g, r]) # 通道合并
src[:,:, 2] = 0 # 将红色通道赋值为0
cv.imshow("changed image", src)
cv.waitKey(0)
cv.destroyAllWindows()
附一张hsv空间色彩对应的图