Tuesday 21 November 2023

Color Space Conversion with OpenCV's cvtColor

 

Using ‘cvtColor’ method, we can convert the color space of an image.

 

Signature

dstImage = cv2.cvtColor(srcImage, code[, dst[, dstCn]])

 

Example

grayscale_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

 

Following table summarizes the parameters of cvtColor method.

 

Parameter

Description

src

Source image that you want to convert to.

code

Specifies the destination image color code.

 

Ex: cv2.COLOR_BGR2GRAY

dst

It is an optional parameter, where the converted image will be stored.

dstCn

Specifies the number of channels in the destination image

 

What is color space?

Color space is a way of representing the colors in a digital space. For example, RGB (Red, Green, Blue) is the most common used color space. Each color space has its own set of rules and coordinates to represent the colors.

 

Following are some mostly used color spaces.

 

1.   RGB: This color space represent the colors by mixing various intensities of red, green, and blue channels.

2.   Grayscale: It represents images in shades of gray (from black to white.) rather than color. Each pixel in a grayscale image is represented by a single value, which represents the brightness of the pixel. Gray scale images are used where color information is not needed.

3.   HSV (Hue, Saturation, Value): Hue represents the type of color (e.g., red, blue, green), Saturation represents the intensity or purity of the color, and Value represents the brightness or lightness of the color.

 

 

Simple RGB image

 


 

Grayscale version of the same image



Previous                                                    Next                                                    Home

No comments:

Post a Comment