Introduction to Color Space

Gray color space

Single channel, value range [0,255]

RGB color space

RGB color space can also be described as a three-dimensional cube. When the three primary color components are all 0(weakest), the mixed light is black; When all three primary colors are K (maximum value determined by storage space), the mixture is white light. The Red,Green, and Blue channels constitute the R,G, and B channels in opencv. The value range of these three channels is [0,255]. (Baidu Encyclopedia)

HSV

It is proposed for digital image, can not well represent the human eye interpretation image process H (Hue) Hue :[0,360] S (Saturation), that is, color purity, 0 Saturation is white V (Value/Brightness), 0 Brightness is pure black

In OpenCV, color range: H = [0,179] S = [0,255] V = [0,255]

Opencv – HSV color space

HSV(Hue,saturation,value) model of the color space corresponds to a conical subset in the cylindrical coordinate system, and the top face of the cone should be V=1. It contains R=1, G=1, and B=1 in the RGB model, which represent lighter colors. The color H is given by the Angle of rotation about the V axis. Red corresponds to an Angle of 0°, green to an Angle of 120°, and blue to an Angle of 240°.

In the HSV color model, each color differs 180° from its complement. The saturation S values from 0 to 1, so the radius of the top surface of the cone is 1. At the vertex of the cone (i.e., the origin), V=0,H and S are undefined and represent black. At the center of the top surface of the cone, S=0, V=1, and H is undefined, representing white. From this point to the origin represents dimming gray, that is, gray with different shades of gray. For these points, S=0, the value of H is undefined. It can be said that the V-axis in the HSV model corresponds to the main diagonal in the RGB color space. The color on the circumference of the top surface of the cone, V=1, S=1, is a solid color.

The HSV color space can be understood from the tapered drill-change model: The hue channel ranges from 0 to 360 degrees, and the singular value ranges from [0,180] in OpencV.

(Baidu Encyclopedia)

Some simple operations

Color space conversion

The most common two are HSV and RGB YUV and RGB

def color_space_demo(image): # Color space conversion CV.imshow("Source Pic",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)
    cv.waitKey(0)

Copy the code

Process video to track certain colors

def extract_object_demo(Videopath): # Process video tracking with some kind of color Capture= cv.VideoCapture(Videopath)
    while(True):
        ret,frame=capture.read()
        if ret == False:
            breakCvtColor (frame, CV.COLOR_BGR2HSV) lower_hsv = nparray([37.43.46])# low value (look up table) high_hsv = nparray([77.255.255Mask = cv.inrange (HSV,lowerb = lower_hsv,upperb = high_hsv)"video",frame)
        cv.imshow("mask", mask)
        c = cv.waitKey(50)
        if(c==27) :break
Copy the code

Separate the RGB image three channels

Use the split () function

ef split_pitcure(src): # Separate the RGB image channels b,g,r=cv.split(src)
    cv.imshow("blue",b)
    cv.imshow("green", g)
    cv.imshow("red", r)
Copy the code

# current code:

import cv2  as cv
import  numpy as np
def main(a):
    Videopath = E:/ movie /[sunnymovie www.ygdy8.com]. Hurrah. Bd.720p. Chinese and English subtitles. MKV"
    pitcurepath = "C:/Users/POG/Pictures/Autumn is coming WallPack/Timon Studler Mod.jpg"
    src = cv.imread(pitcurepath)
    cv.namedWindow("input image", cv.window_autosize) # Create window#cv.imshow("input image", SRC) # Display the initial image# Access_pixels (SRC) #Retrieve inverse(SRC) image 2# Video_demo (Videopath) # Output video #extract_object_demo(Videopath) # Process video #create_iamge() # Create new picture #color_space_demo(SRC) # Color space Conversion Split_pitcure (SRC)0# wait for cV.destroyAllWindows () # destroy window # get_image_info(SRC) # output image information#cv.imwrite("D:/result.png", SRC) # Save imageDef get_image_info(image):# print(type(image)) print(image.shape) print(image.size) print(image.dtype) pixel_data = np.array(image) print(pixel_data) def video_demo(Videopath):while(True): ret,frame = capture. Read () # frame=cv.flip(frame,1# Read video FPS, FPS = the capture size. Get (CV. CAP_PROP_FPS) size = (. Capture the get (CV. CAP_PROP_FRAME_WIDTH), the capture, the get (CV. CAP_PROP_FRAME_HEIGHT)) print("fps: {}\nsize: {}".format(FPS,size)) # Video read duration (frames) total =int(capture.get(cv.CAP_PROP_FRAME_COUNT))
        print("[INFO] {} total frames in video".format(total))
        cv.imshow("video",frame)
        c = cv.waitKey(50)
        if (c==27) :# esc to 27
            breakDef def pixels(image):# print(image.shape) height = image.shape0]
    width = image.shape[1]
    channels = image.shape[2] #blue , green , red
    for row in range(height):
        for col in range(width):
            for c in range(channels):
                pv = image[row,col,c]
                image[row,col,c] = 255-pv
    cv.imshow("pixels_demo"Def inverse(image):#2
    dst = cv.bitwise_not(image)
    cv.imshow("inverse_demo"Def create_iamge():# img = np.zeros([image] def create_iamge():400.400.3# create image img[:, :,0] = np.ones([400.400]) *255The three channels are0.1.2
    cv.imshow("new"Gray = cv.cvtcolor (image, CV.COLOR_BGR2GRAY) cv.imshow(image, CV."gray",gray)
    hsv=cv.cvtColor(image,cv.COLOR_BGR2HSV)
    cv.imshow("HSV",image)
    yuv=cv.cvtColor(image,cv.COLOR_BGR2YUV)
    cv.imshow("yuv"Def extract_object_demo(Videopath): # Process video tracking with certain color capture = cV.videocapture (Videopath)while(True):
        ret,frame=capture.read()
        if ret == False:
            breakCvtColor (frame, CV.COLOR_BGR2HSV) lower_hsv = nparray([37.43.46])# low value (look up table) high_hsv = nparray([77.255.255Mask = cv.inrange (HSV,lowerb = lower_hsv,upperb = high_hsv)"video",frame)
        cv.imshow("mask", mask)
        c = cv.waitKey(50)
        if(c==27) :breakB,g,r=cv.split(SRC) cv.imshow();"blue",b)
    cv.imshow("green", g)
    cv.imshow("red", r)


if __name__ =="__main__":
    main()
Copy the code