Color space conversion, simple color tracking and channel separation and combination of pictures

Keywords: PHP OpenCV Attribute

Color space conversion of a picture

  • Gray color space

Single channel, value range [0255] 0 black 255 white

  • RGB color space (BGR is used in opencv)

The range of R, G and B channels in opencv is [0255].

 

  • HSV/HSL color space

     H: 0-180  S: 0-255 V: 0-255

HSV is a representation of points in RGB color space in an inverted cone. HSV is hue, saturation and value, also known as HSB(B is Brightness). Hue is the basic attribute of color, that is, the name of color, such as red, yellow, etc. Saturation (S) refers to the purity of the color. The higher the color is, the purer the color is, and the lower the color is, the graying gradually. Take a value of 0-100%. Lightness (V), take 0-max (the value range of HSV in the computer is related to the length of storage). HSV color space can be described by a cone space model. At the apex of the cone, V=0, H and S are undefined, representing black. V=max, S=0 at the center of the top surface of the cone, H is undefined and represents white.

 

1 code

import cv2 as cv

#Transformation of color space
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('hsv',yuv)

src=cv.imread('./yiner.jpg')
cv.namedWindow('before',cv.WINDOW_NORMAL)
cv.imshow('before',src)

color_space_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

 

 

 

Two: color space conversion, using the inrange function to filter the color in the video, to track a color

Code

#Color space conversion,utilize inrange Function to filter colors in video,Implement tracking color
import cv2 as cv
import numpy as np

def catch_pen():
    capture=cv.VideoCapture('./1.mp4')#Import files

    while True:
        ret,frame=capture.read()
        if ret==False:
            break
        #Color space conversion to HSV
        hsv=cv.cvtColor(frame,cv.COLOR_BGR2HSV)
        # Set black range, track black in video
        lower_hsv=np.array([0,0,0]) #Set the low value of the filtered color
        upper_hsv=np.array([180,255,46])#Set the high value of the filtered color
        # Adjust image color information( H),Saturation ( S),Brightness ( V)Section, select black area
        mask=cv.inRange(hsv,lower_hsv,upper_hsv)
        cv.imshow('video',frame)
        cv.imshow('mask',mask)
        if cv.waitKey(50)&0xFF==ord('q'):
            break

catch_pen()
cv.waitKey()
cv.destroyAllWindows()

 

HSV lower and uppe HSV query tables

 

Separate and merge three channels, modify a channel

Channel separation: split() divides the color image into three channels

merge()

import cv2 as cv
import numpy as np

src=cv.imread('hua.jpg')
cv.namedWindow('before',cv.WINDOW_NORMAL)
cv.imshow('before',src)

#Channel separation
b,g,r=cv.split(src)
cv.imshow('blue',b)
cv.imshow('green',g)
cv.imshow('red',r)

#Channel merging
src=cv.merge([b,g,r])
cv.imshow('merge',src)

# Modify a channel
src[:,:,2]=100
cv.imshow('single',src)

cv.waitKey(0)
cv.destroyAllWindows()

 

Effect display

Posted by raven2009 on Fri, 25 Oct 2019 10:05:19 -0700