[Original] OpenCV-Python series of changing the color space (13)

From this tutorial, we formally enter the basics of learning. The most important part of OpenCV image processing is the color space of the image. We have seen examples of image grayscale before, but this is only one of them. One kind.

Color space

Color/color space (English: Color space) is the organization of colors. With the help of color space and physical device testing, fixed analog and digital representations of colors can be obtained. The color space can be defined by just picking some colors arbitrarily. For example, the Pantone system just takes a set of specific colors as samples, and then defines a name and code for each color; it can also be based on rigorous mathematical definitions, such as Adobe RGB , SRGB.

Many people know that three primary colors of red, yellow, and blue can be used to generate different colors when painting. These colors define a color space. We define the amount of magenta as the X coordinate axis, the amount of cyan as the Y coordinate axis, and the amount of yellow as the Z coordinate axis, so that a three-dimensional space is obtained, and each possible color is unique in this three-dimensional space Of a location.

However, this is not the only color space. For example, when displaying colors on a computer monitor, it is usually defined in the RGB (red, green, blue) color space. This is another way to generate the same color. Red, green, and blue are treated as X, Y and Z coordinate axes. Another way to generate the same color is to use hue (X axis), saturation (chromaticity) (Y axis) and lightness (Z axis). This method is called HSB color space. In addition, there are many other color spaces, many of which can be represented in three dimensions (X, Y, Z), more or less dimensions in this way, but some cannot be represented in this way at all.

Most of the color pictures we see in our lives are of RGB type, but when performing image processing, we need to use grayscale, binary, HSV, HSI and other color systems. OpenCV provides the cvtColor() function to achieve these Features. First look at the cvtColor function definition:

cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 );

. InputArray src: The input image is the original image to be transformed into the color space, which can be a Mat class

. OutputArray dst: The output image is to store the image after color space transformation, or Mat class

. int code: The code or logo of the conversion, that is, here to determine what format picture will be converted into what format picture, which will be described in detail later

. int dstCn = 0: The number of target image channels, if the value is 0, it is determined by src and code

The function of the function is to convert an image from one color space to another color space, but when converting from RGB to other types, the color channel of the image must be clearly indicated. As we mentioned earlier, in OpenCV, its default color The system arrangement is BGR instead of RGB. So for a 24-bit color image, the first 8-bit is blue, the middle 8-bit is green, and the last 8-bit is red. The value range of common R, G, and B channels is:

. 0-255: CV_8U type picture

. 0-65535: CV_16U type picture

. 0-1: CV_32F type picture

In this tutorial, we only study several important color space conversion methods, including grayscale and HSV space conversion. Let's first look at the code:

	view plaincopy to clipboardprint?
import cv2  
  
img = cv2.imread("cat.jpg")  
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)  
hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)  
LAB = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)  
LUV = cv2.cvtColor(img, cv2.COLOR_BGR2LUV)  
  
cv2.imshow("gray", gray)  
cv2.imshow("hsv", hsv)  
cv2.imshow("hls", hls)  
cv2.imshow("LAB", LAB)  
cv2.imshow("LUV", LUV)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

Let's look at the effect:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
we saw six cats of different colors. Of course, there are more than 150 color space conversions inside OpenCV. It is impossible to demonstrate one by one here. We only talk about the most important two: grayscale and HSV, grayscale, don’t need to say more, mainly look at HSV, HSV is usually used with color tracking, and it can be regarded as a primary target tracking method. It is easier to track objects of a certain color than BGR, and is often used to segment designated colors. Objects.

The way HSV expresses color images consists of three parts:

· Hue (hue, hue)

· Saturation (saturation, color purity)

· Value (Lightness)

Use the following cylinder to represent the HSV color space. The cross-section of the cylinder can be regarded as a polar coordinate system. H is represented by the polar angle in polar coordinates, S is represented by the length of the polar axis in polar coordinates, and V is represented by the central axis of the cylinder. Height representation.
Insert picture description here
Hue is measured by angle, with a value range of 0-360°, which represents color information, that is, the position of the spectral color. Expressed as follows:
Insert picture description here

All the colors on the color circle are colors on the spectrum, starting from red and rotating counterclockwise, Hue=0 means red, Hue=120 means green, Hue=240 means blue and so on.

In GRB, the color is determined by three values. For example, yellow is (255,255,0); in HSV, yellow is determined by only one value, Hue=60.

Half cross section of HSV cylinder (Hue=60):
Insert picture description here
The horizontal direction represents saturation, and saturation represents how close the color is to the spectral color. The higher the saturation, the darker the color, the closer to the spectrum, the lower the saturation, the lighter the color, the closer to white. A saturation of 0 means pure white. The value range is 0-100%. The larger the value, the more saturated the color.

The vertical direction indicates the brightness, which determines the brightness of the color in the color space. The higher the brightness, the brighter the color, and the range is 0-100%. A lightness of 0 means pure black (the color is the darkest at this time).

It can be understood as:

In the case of a certain Hue, the saturation is reduced, that is, white is added to the spectral color, and the proportion of the spectral color is also reduced. The saturation is reduced to 0, which means that the proportion of the spectral color is zero, resulting in the entire color Appears white.

The lightness decreases, that is, black is added to the spectral color, and the proportion of the spectral color is also reduced. The lightness decreases to 0, which means that the proportion of the spectral color is zero, which causes the entire color to appear black.

HSV is a relatively intuitive color model for us. We can easily get a single color, that is, specify the color angle H, and let V=S=1, and then add black and white to it to get the color we need. Increasing black can reduce V without changing S, and increasing white can reduce S without changing V. For example, to get dark blue, V=0.4 S=1 H=240 degrees. To get light blue, V=1 S=0.4 H=240 degrees.

The stretching contrast enhancement of HSV is to normalize the two components of S and V, and H remains unchanged.

Note: The range of the three components of HSV in OpenCV is:

· H = [0,179]

· S = [0,255]

· V = [0.255]

Target color tracking

As mentioned earlier, color tracking is implemented according to the HSV color space. First, we need to perform color space conversion on a BGR value to obtain the HSV value.

In order to identify objects of a specific color, it is important to obtain the HSV value corresponding to the color. Here we give a conversion table:
Insert picture description here

In order to implement these data ranges with code, we need to know a function of OpenCV:

cv2. inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst), the analysis of each parameter:
1. src is the input array

2.lowerb is the minimum value array or component containing lowerb, the minimum value used in the above table in this tutorial

3. upperb is the maximum value array or component containing upperb, the maximum value used in the above table in this tutorial

4. The output array, the size and the number of channels are the same as src, and it is of type CV_8V

Let's look at the code, which is used to open the camera and recognize the movement of the green object:

view plaincopy to clipboardprint?
import cv2  
import numpy as np  
  
cap = cv2.VideoCapture(0)  
  
while True:  
    ret, frame = cap.read()  
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)  
  
    lower_black = np.array([35, 43, 46])  
    upper_black= np.array([77, 255, 255])  
  
    mask = cv2.inRange(hsv, lower_black, upper_black)  
    res = cv2.bitwise_and(frame, frame, mask=mask)  
  
    cv2.imshow('frame', frame)  
    cv2.imshow('mask', mask)  
    cv2.imshow('res', res)  
    k = cv2.waitKey(5) & 0xFF  
    if k == 27:  
        break  
  
cv2.destroyAllWindows()  

Let's look at the effect:
Insert picture description here

Perfect, so you can use HSV to track the object. The most important thing is to calibrate the value range. It is perfectly fine to perform the value according to the above table.

Check the article summary page https://blog.csdn.net/weixin_44237705/article/details/107864965
More openvino technical information can be exchanged in the group~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44237705/article/details/108121584