opencv+ image processing (Image Processing in OpenCV) 4-0 change color space

Code address of this column https://github.com/xiawei20161308104/xv_opencv_tutorials
Code path of this section xv_opencv_tutorials/ImageProcessinginOpenCV/changing_colorspaces.py
Reference图像工程第4版,张毓晋,清华大学出版社

0. The new opencv functions involved in this section

  • Color Space Conversion Functioncv.cvtColor()

1. Formation of color

“Indeed rays,properly expressed,are not colored”——牛顿。

Light is electromagnetic waves of different frequencies. Human vision can feel the stimulation of light and perceive electromagnetic waves of different frequencies as different colors. Therefore, colors only exist in human eyes and brains, and the physical world is distributed with different radiations. Energy rays.

2. Color space

The color model is built on the color space, and the concepts of the color model and the color space are not distinguished.
According to the principle can be divided into 4 categories, according to the application can be divided into 2 categories

According to the principle model principle example
Colorimetric Model/Chromaticity Model Based on physical measurements of spectral reflectance For the color "id", the most accurate expression of the color
Physiological model Based on the 3 basic color perception cones present in the human retina RGB
psychophysical model / psychophysical model Based on human perception of color HSI,HCV,L*a*b*
opposite model Based on perception experiments HSB
According to the application model application example
For hardware monitor, printer RGB,CMY,CMYK
visually oriented animation, image processing HSI,HSB,L*a*b*

3. Why do you need to choose a suitable color space

Human perception of color is related to the spectral energy distribution of electromagnetic radiation that stimulates the retina, human neuropsychology, and human physiological behavior. Perceiving color is a very complicated process, and its applications are also diverse. CIE defines a number of color models, each of which is only applicable to specific situations,
and no general model can satisfy all situations. Choosing an appropriate color model based on color principles and color applications can unify the dimensions of the problem.

4. How to choose a suitable color space

Know your needs and find a color space with corresponding characteristics. For example, the RGB model is particularly suitable for applications such as image acquisition input and image output display, but it is somewhat different from human visual perception, that is, when we see an image, we cannot perceive how much each of its red, green, and blue components have. , at this time the HSV model is more suitable, H represents hue, S represents saturation, V represents intensity, the model established in this way is highly compatible with human perception.
Analyze the image in HSV mode, get the V value of the image, and reduce it by 100 pixels to v1, and compare and observe, it can be found that the human eye can clearly perceive the change in brightness.

def main():  
    # 读取图像  
    src = cv.imread("../imgs/opencv.png")  
    # 转换HSV空间  
    hsv = cv.cvtColor(src, cv.COLOR_BGR2HSV)  
    # 分离通道  
    h, s, v = cv.split(hsv)  
    cv.namedWindow("v", cv.WINDOW_NORMAL)  
    cv.imshow("v", v)  
    # 减小亮度  
    v1 = v - 30  
    cv.namedWindow("v1", cv.WINDOW_NORMAL)  
    cv.imshow("v1", v1)  
  
    cv.waitKey(0)  
    cv.destroyAllWindows()

insert image description here

5. Commonly used color spaces

RGB

insert image description here
(From Image Engineering 4th Edition, Zhang Yujin, Tsinghua University Press)

  • Based on human cone cells , a model closely related to the structure of the human visual system
  • The wavelength response curves SML of three different color-sensing cone cells in the retina basically correspond to the three primary colors RGB
  • The RGB model is established in the Cartesian coordinate system
  • The origin is black, the vertex farthest from the origin is white, and the line connecting these two points is gray in different shades

CMY

  • The three-color light is superimposed in two to produce the three-complementary color CMY, C is blue-green (green + blue), M is magenta (red + blue), and Y is yellow (red + green)
  • Generally used for printing in publishing houses, image processing is not commonly used

HSI

insert image description here

  • HSI corresponds to human perception and is called a user-oriented color model
  • Describe color with Hue, Saturation and Intensity
  • Display independent
  • Advantages of HSI:
    1. The intensity component has nothing to do with the color information of the image and is independent, that is, the transformation of the intensity/brightness component does not change the hue and saturation of the image.
    2. H and S are independent and adapt to human senses, making the HSI model very suitable for image algorithms that process and analyze color perception characteristics based on the human visual system.
  • Disadvantages of HSI:
    1. It is not a uniform color space model - the degree of color difference between two points perceived by people corresponds to the Euclidean distance between two points in the color space.

In order to experience the adaptation of the HSI color model to human senses more intuitively, the code splits the RGB components and HSV components of the same image.
insert image description hereThe darker the color, the larger the proportion of the component value. As can be seen, the three plots of HSI look quite different, denoting, 人眼能感觉到的HSI三个分量的区别要比RGB三个分量的区别要大.

L*a*b*

  • From the perspective of human intuition, the description of color by the color model should be as synchronized with human perception of color as possible, L\*a\*b\*是一个均匀彩色空间that is, the degree of color difference between two points perceived by humans and the Euclidean distance between two points in this space Corresponding
  • It is also a color space for human vision, which has nothing to do with equipment and is suitable for applications close to natural lighting

6. Transform color space math formula

The range of components defined by each toolkit tool is different. For example, the hue H can be 0-180 or 0-360. Mathematically, they can be converted to each other. So when opencv is processing, for 8-bit and 16-bit images, R, G and B are converted to floating point format, and scaled and normalized to 0-1.
Part of the conversion formula is as follows:

  • RGB2Gray

insert image description hereinsert image description here

  • RGB2CIE XYZ
    insert image description here

  • RGB2HSV

7.opencv transform color space code + comment + effect

The cvtColor function of opencv realizes the conversion of the color space, and provides 150种the conversion method of the color space, and only needs to fill in the corresponding conversion identifier in the position cvtColorof the function . flagThe conversion ID is obtained as follows.

import cv2 as cv  
flags = [i for i in dir(cv) if i.startswith('COLOR_')]  
#这里会输出150种flag,他们是opencv定义的彩色空间转换flag,是cv.cvtColor(input_image, flag)的第二个参数值。
print( flags )

You can get it yourself through the above code, or directly query the official document

Here are two cases of conversion codes and effects, namely BGR2Grayand BGR2HSV(HSI is not provided in opencv, but these two color spaces are similar

#BGR2Gray
import cv2 as cv  
def main():   
	src = cv.imread("../imgs/opencv.png")
    cv.namedWindow("input", cv.WINDOW_AUTOSIZE)  
    cv.imshow("input", src)  
    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)  
    cv.imwrite('gray.png', gray)  
    cv.imshow("gray", gray)  
    cv.waitKey(0)  
    cv.destroyAllWindows()  
if __name__ == "__main__":  
    main()

insert image description here

def main():  
    src = cv.imread("../imgs/opencv.png")  
    cv.namedWindow("input", cv.WINDOW_AUTOSIZE)  
    cv.imshow("input", src)  
  
    hsv = cv.cvtColor(src, cv.COLOR_BGR2HSV)  
    cv.imwrite('hsv.png', hsv)  
    cv.imshow("hsv", hsv)  
    # 分离通道  
    h, s, v = cv.split(hsv)  
    cv.namedWindow("h", cv.WINDOW_NORMAL)  
    cv.imshow("h", h)  
    cv.namedWindow("s", cv.WINDOW_NORMAL)  
    cv.imshow("s", s)  
    cv.namedWindow("v", cv.WINDOW_NORMAL)  
    cv.imshow("v", v)  
  
    cv.waitKey(0)  
    cv.destroyAllWindows()  

if __name__ == "__main__":  
    main()

insert image description here

Guess you like

Origin blog.csdn.net/cvxiayixiao/article/details/130128965