Python-OpenCV 图像处理(三):常见图像色彩空间转换(RGB\HSV)

import cv2
import numpy as np

__author__ = "zxsuperstar"
__email__ = "[email protected]"


"""
常见图像色彩空间转换
"""

def color_space_demo(image):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    cv2.imshow("gray",gray)
    hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
    cv2.imshow("hsv",hsv)  #h:0-100,s:0-255,v:2-255
    yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
    cv2.imshow("yuv", yuv)


def extrace_object_demo():
    capture = cv2.VideoCapture("")  # 视频
    while(True):
        ret, frame = capture.read()
        if ret == False:
            break
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        lower_hsv = np.array([35,43,46])
        upper_hsv = np.array([77,255,255])
        mask = cv2.inRange(hsv,lowerb=lower_hsv,upperb=upper_hsv)
        cv2.imshow("video",frame)
        cv2.imshow("mask",mask)
        c = cv2.waitKey(40)
        if c == 27:
            break


if __name__ == "__main__":
    src = cv2.imread("1.jpg") #blue green red
    cv2.namedWindow("input image", cv2.WINDOW_AUTOSIZE)
    cv2.imshow("input image",src)
    
    b,g,r = cv2.split(src)
    cv2.imshow("b",b)
    cv2.imshow("g",g)
    cv2.imshow("r",r)
    
    src = cv2.merge([b,g,r])
    cv2.imshow("src1",src)
    
    # color_space_demo(src)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Python-OpenCV 图像处理(三):常见图像色彩空间转换(RGB\HSV)_第1张图片

你可能感兴趣的:(python,图像处理)