opencv-python学习笔记四:色彩空间转换

import cv2 as cv
import numpy as np


def extrace_object_demo():
    capture = cv.VideoCapture("C:/Users/hyn/Desktop/OpenCV/IMG_1506.mp4")
    while (True):
        # 第一个参数是bool型的ret,其值为True或False,代表有没有读到图片;第二个参数是frame,是当前截取一帧的图片
        ret, frame = capture.read()
        if ret == False:
            break
        hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
        # 设置过滤的颜色的低值 参看HSV颜色对应RGB的分量范围表格
        lower_hsv = np.array([26, 34, 43])
        # 设置过滤的颜色的高值
        upper_hsv = np.array([46, 255, 255])
        # 利用inrange函数过滤视频中的颜色,实现跟踪某一颜色
        mask = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv)
        cv.imshow("video", frame)
        cv.imshow("mask", mask)
        c = cv.waitKey(40)
        # 按esc键结束运行
        if c == 27:
            break


def color_space_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    cv.imshow("gray", gray)
    # h:0-180 s:0-255 v:0-255 HSV即Hue(色调),Saturation(饱和度)和Value(亮度)三个channel
    hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
    cv.imshow("hsv", hsv)
    yuv = cv.cvtColor(image, cv.COLOR_BGR2YUV)
    cv.imshow("yuv", yuv)


src = cv.imread("C:/Users/hyn/Desktop/OpenCV/lena.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)

# color_space_demo(src)
# extrace_object_demo()

# 拆分,分别输出三个通道的图片
b, g, r = cv.split(src)
cv.imshow("blue", b)
cv.imshow("green", g)
cv.imshow("red", r)

# 修改某个通道的值
src[:, :, 2] = 0
print(src)

# 合并三个通道的图片
src = cv.merge([b, g, r])
cv.imshow("changed image", src)

cv.waitKey(0)
cv.destroyAllWindows()

你可能感兴趣的:(opencv-python学习笔记四:色彩空间转换)