preface

After the introduction of the previous theoretical knowledge, we have mastered a variety of color space types. This blog post describes how to convert color space types in code in OpenCV.

RGB and GRAY convert each other

In OpenCV, we use the cv2.cvtColor() function to transform the color space. The function color space type is represented by the enumeration type, where the COLOR_BGR2GRAY enumeration type is specifically provided for RGB to GRAY.

The specific code is as follows:

import cv2

img = cv2.imread("4.jpg", -1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("rgb", img)
cv2.imshow("gray", gray)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the effect is as shown in the figure below:Next, let’s look at how GRAY is converted to RGB. The code looks like this:

import cv2

img = cv2.imread("4.jpg".0)
bgr = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.imshow("gray", img)
cv2.imshow("rgb", bgr)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the display effect is as shown in the figure below:It is important to note that the color matrix of an RGB image is BGR, so our conversion is done by BGR2GRAY and GRAY2BGR. As we can see from this, the constants of the enum type are very easy to understand, simply by reversing the name of the conversion type.

However, we found that the gray image is converted to RGB image is not change, because there is no color, gray image arithmetic could not have dreamed up by way of reduction, but turn gray image RGB image and has a certain meaning, if you need to color, then the gray image is a two-dimensional matrix, it is not the color value of three-dimensional matrix, are just out of your assignment. Converting from GRAY to RGB allows you to change the color value of a pixel just as you would an RGB image, even though it is GRAY.

RGB and HSV conversion

Similar to the code above, convert the color space with cv2.cvtcolor () :

import cv2

img = cv2.imread("4.jpg", -1)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow("bgr", img)
cv2.imshow("hsv", hsv)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, we get an image like this:In the previous theoretical knowledge, we can obtain a certain color according to the value of the hue, that is, we can extract the specific color by the value on the H channel of HSV. The advantages of this extraction and analysis of color can be used in face recognition, such as skin color recognition.

Let’s try it in blue. The code looks like this:

import cv2

img = cv2.imread("4.jpg", -1)
img[:, :, 0] = 255
Blue = img
blueHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow("imgBlue", Blue)
cv2.imshow("blueHSV", blueHSV)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

After running, the effect looks like this:Now, let’s extract its red area. The complete code looks like this:

import cv2
import numpy as np

img = cv2.imread("4.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow("hsv", hsv)
minBlue = np.array([0.50.50])
maxBlue = np.array([30.255.255])
# Identify the blue area
mask = cv2.inRange(hsv, minBlue, maxBlue)
# Get the blue area by bitwise
blue_img = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow("blue", blue_img)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code

Here, we use a new method cv2.inrange (), which is defined as follows:

def inRange(src, lowerb, upperb, dst=None) :
Copy the code

SRC: Indicates the array or head to check

Lowerb: indicates the lower bound of the range

Upperb: indicates the upper bound of the range

By this method, we can judge whether the pixel value of the pixel point in the image is in a certain interval.

So we know from the theory that HSV is zero is red. For compatibility, we need to expand the red value up and down, but the color range itself cannot be less than 0, so we can only expand the upper bound, which is 30 range.

In HSV, the value range of S channel and V channel is [100,255]. Therefore, in order to obtain the value of the image red, we limited the boundary between [0, 50, 50] and [30, 255, 255]. After running, the image red was extracted:As you can see from this example, we can use cv2.inrange () to annotate the specified range of values in the image and return them to the mask. If the value of the image is located in this area, the value of the corresponding position of mask is 255, otherwise, it is 0. The specified color is then extracted by bitwise and operation of the mask.

Here, our bitwise_and has the third parameter mask, and the “and” operation is carried out by using the mask, that is, the white area of the mask image is the retention of the image pixels to be processed, while the black area is the deletion of the image pixels to be processed.