This is the 10th day of my participation in Gwen Challenge

OpenCV is a C++ library, currently popular computer vision programming library, for real-time processing of computer vision problems, it covers a lot of modules in the field of computer vision. In Python, the OpenCV library is often used for image processing.

This article will show you how to use OpenCV for color conversion in Python3:

above

Opencv has hundreds of methods for converting between different color Spaces.

There are three commonly used color Spaces in computer vision: gray scale, BGR, and HSV

However, only two are commonly used, namely BGR< — >Gray and BGR< — >HSV.

Gray scale, BGR, HSV

Grayscale color space is converted to grayscale by removing color information. Grayscale color space is particularly effective for intermediate processing, such as face detection

BGR is blue, green and red color space. Each pixel is represented by a triplet, representing blue, green and red colors respectively. Similar to the color space BGR, they differ only in the order of color

HSV: H (Hue) refers to the Hue, which is in the range of [0,179], S (Saturation), [0,255] and V (Value) indicates the degree of darkness, which is in the range of [0,255].

Color conversion

For color conversion, we use the function cv2.cvtColor(input_image, flag), flag is the conversion type.

The following code to achieve the conversion of color space.

import cv2
import numpy asColor space conversion from BGR to gray, HSV, YUV, yCRCB def color_space_demo(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("gray", gray)

    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    cv2.imshow("hsv", hsv)

    yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)
    cv2.imshow("yuv", yuv)

    ycrcb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
    cv2.imshow("ycrcb", ycrcb)

if __name__ == '__main__':

    src = cv2.imread("E:\\demo\\lena.jpg"Cv2.namedwindow (SRC: cv2.namedWindow)"lena"Cv2.imshow (, cv2.window_autosize)"lena"Color_space_demo (SRC) cv2.waitkey (SRC)0)
    cv2.destroyAllWindows()
Copy the code

The output is:

The original image

Gray figure

HSV figure

Ycrcb figure

Yuv figure

A series of articles will follow this month,

Wonderful article, please pay attention.