OpenCV-Python Development Guide (8) --- Simple application of color space conversion and HSV

Preface

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

Convert between RGB and GRAY

In OpenCV, we use the cv2.cvtColor() function to achieve color space conversion. The color space type of this function is represented by an enumeration type, and the COLOR_BGR2GRAY enumeration type is specially provided for converting 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()

After running, the effect is as shown in the figure below:
BGR to GRAY
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)
cv2.waitKey()
cv2.destroyAllWindows()

After running, the display effect is as shown in the following figure: It
GRAY to BGR
should be noted that the color 3D matrix of RGB image is BGR, so when we convert, we use BGR2GRAY and GRAY2BGR. From here, we can also see that the enumerated type constants are actually very easy to understand. It is nothing more than to reverse the name of the conversion type.

However, we found that there is no change in the conversion of grayscale images to RGB images. Because grayscale images have no color values, calculations cannot be restored by imagination. However, the conversion of grayscale images to RGB images has a certain meaning. If you If you need to color, then the grayscale image is a two-dimensional matrix, it is not a three-dimensional matrix of color values, you cannot assign values. After converting to RGB through GRAY, you can change the color value of a certain pixel like operating RGB images, even though it is gray.

Convert between RGB and HSV

Similar to the code above, use cv2.cvtColor() to convert the 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)
cv2.waitKey()
cv2.destroyAllWindows()

After running, we will get the image shown below:
BGR to HSV
In the previous theoretical knowledge, we can get 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. The advantage of this extraction and analysis of color can be used to identify 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)
cv2.waitKey()
cv2.destroyAllWindows()

After running, the effect is as follows:
blue
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])
# 确定蓝色区域
mask = cv2.inRange(hsv, minBlue, maxBlue)
# 通过按位与获取蓝色区域
blue_img = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow("blue", blue_img)
cv2.waitKey()
cv2.destroyAllWindows()

Here, we use a new method cv2.inRange(), its definition is as follows:

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

src: indicates the array or avatar to be checked

lowerb: represents the lower bound of the range

upperb: indicates the upper bound of the range

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

After the previous theoretical explanation, we know that the H of HSV is equal to 0 and it is red. For compatibility, we need to extend the value of red up and down, but the color range itself cannot be less than 0, so we can only extend the upper limit, that is, extend the range of 30.

For the S channel in HSV, the value range of the V channel is [100,255]. Therefore, in order to obtain the red value of the image, we limit the limit to [0, 50, 50] to [30, 255, 255]. After running, the red of the image is extracted:
Extract red
as can be seen from this example, We can mark the value of the specified range in the image through cv2.inRange() and return 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 taken out by the bitwise AND operation of masking.

Here, our bitwise_and has a third parameter mask, which uses the mask to perform an "AND" operation, that is, the white area of ​​the mask image is the reservation of the image pixels that need to be processed, and the black area is the removal of the image pixels that need to be processed.

Guess you like

Origin blog.csdn.net/liyuanjinglyj/article/details/113795768