Opencv Python Development Guide -- simple application of color space conversion and HSV


After the introduction of the previous theoretical knowledge, we have mastered various types of color space. This blog post mainly introduces how to use code to realize the conversion of color space types in OpenCV.

RGB and GRAY convert to each other

In OpenCV, we use CV2 Cvtcolor() function realizes the conversion of color space. The function color space type is represented by enumeration type, where color_ The 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)

After running, the effect is shown in the following figure:

Next, let's take a look at how GRAY is converted to RGB. The specific code is as follows:

import cv2

img = cv2.imread("4.jpg", 0)
bgr = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
cv2.imshow("gray", img)
cv2.imshow("rgb", bgr)

After running, the display effect is shown in the following figure:

It should be noted that the color 3D matrix of RGB image is BGR, so we convert it through BGR2GRAY and GRAY2BGR. We can also see from here that its enumeration type constant is actually very easy to understand, which is just to reverse the conversion type name.

However, we find that there is no change in the conversion of GRAY image to RGB image, because GRAY image has no color value, and the operation cannot be restored by imagination. However, the conversion of GRAY image to RGB image also has certain significance. If you need color, then GRAY image is a two-dimensional matrix, which is not a three-dimensional matrix of color value, and you cannot assign value. After converting to RGB through GRAY, you can change the color value of a pixel like operating an RGB image, although it is GRAY.

RGB and HSV convert to each other

Similar to the above code, through CV2 Cvtcolor() to convert color space:

import cv2

img = cv2.imread("4.jpg", -1)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow("bgr", img)
cv2.imshow("hsv", hsv)

After running, we will get the following image:

In the previous theoretical knowledge, we can obtain a certain color according to the value of hue, that is, we can extract a specific color through the value on the H channel of HSV. This advantage of extracting and analyzing color can recognize skin color in face recognition.

Next, let's try it in blue. The specific code is as follows:

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)

After running, the effect is as follows:

Now let's extract its red area. The complete code is as follows:

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])
# Determine the blue area
mask = cv2.inRange(hsv, minBlue, maxBlue)
# Get the blue area by bitwise AND
blue_img = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow("blue", blue_img)

Here, we use a new method CV2 Inrange(), which is defined as follows:

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

src: indicates the array or avatar to check

lowerb: indicates the lower bound of the range

upperb: indicates the upper bound of the range

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

After the previous theoretical explanation, we know that H of HSV is equal to 0, which 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 limit, that is, expand the range of 30.

For channel S in HSV, the value range of Channel V is [100255]. Therefore, here, in order to obtain the value of image red, we limit the boundary between [0, 50, 50] and [30, 255, 255]. After running, we extract the red of the image:

As can be seen from this example, we passed CV2 Inrange() can mark out the values within the specified range of the image and return them to the mask. If the value of the image is in this interval, the value at the corresponding position of the mask is 255, otherwise it is 0. Then, the specified color is extracted by masking bitwise sum operation.

Here is our bitwise_and with the third parameter mask, the mask is used for "and" operation, that is, the white area of the mask image is the retention of the image pixels to be processed, and the black area is the elimination of the image pixels to be processed.

Tags: Python image processing OpenCV

Posted by anna_cm on Sat, 16 Apr 2022 17:09:40 +0930