Color Spaces Module

The color_spaces module provides functions for converting between different color spaces commonly used in computer vision and image processing.

Color space conversion functions.

This module provides functions for converting between different color spaces commonly used in computer vision and image processing.

Functions:

cvt_color, cvtColor: Generic color space conversion function.

rgb2bgr, bgr2rgb: Convert between RGB and BGR color spaces.

rgba2bgra, bgra2rgba: Convert between RGBA and BGRA color spaces.

rgb2gray, bgr2gray: Convert color images to grayscale.

gray2rgb, gray2bgr: Convert grayscale images to color.

gray2rgba, gray2bgra: Convert grayscale images to color with alpha channel.

bgr2hsv, rgb2hsv: Convert color images to HSV color space.

hsv2bgr, hsv2rgb: Convert HSV images to color.

rgb, bgr: Aliases for rgb2bgr and bgr2rgb.

rgba, bgra: Aliases for rgba2bgra and bgra2rgba.

cv3.color_spaces.cvt_color(img, code)[source]

Convert image between different color spaces.

Parameters:
  • img (numpy.ndarray) – Input image.

  • code (int) – Color space conversion code (e.g., cv2.COLOR_RGB2BGR).

Returns:

Image in the target color space.

Return type:

numpy.ndarray

Raises:

ValueError – If trying to convert a non-grayscale image to RGB/RGBA.

cv3.color_spaces.cvtColor(img, code)

Alias for cvt_color().

cv3.color_spaces.rgb2bgr(img)[source]

Convert image from RGB to BGR color space.

Parameters:

img (numpy.ndarray) – Input image in RGB format.

Returns:

Image in BGR format.

Return type:

numpy.ndarray

cv3.color_spaces.bgr2rgb(img)[source]

Convert image from BGR to RGB color space.

Parameters:

img (numpy.ndarray) – Input image in BGR format.

Returns:

Image in RGB format.

Return type:

numpy.ndarray

cv3.color_spaces.rgba2bgra(img)[source]

Convert image from RGBA to BGRA color space.

Parameters:

img (numpy.ndarray) – Input image in RGBA format.

Returns:

Image in BGRA format.

Return type:

numpy.ndarray

cv3.color_spaces.bgra2rgba(img)[source]

Convert image from BGRA to RGBA color space.

Parameters:

img (numpy.ndarray) – Input image in BGRA format.

Returns:

Image in RGBA format.

Return type:

numpy.ndarray

cv3.color_spaces.rgb2gray(img)[source]

Convert image from RGB to grayscale.

Parameters:

img (numpy.ndarray) – Input image in RGB format.

Returns:

Grayscale image.

Return type:

numpy.ndarray

cv3.color_spaces.bgr2gray(img)[source]

Convert image from BGR to grayscale.

Parameters:

img (numpy.ndarray) – Input image in BGR format.

Returns:

Grayscale image.

Return type:

numpy.ndarray

cv3.color_spaces.gray2rgb(img)[source]

Convert grayscale image to RGB.

Parameters:

img (numpy.ndarray) – Input grayscale image.

Returns:

Image in RGB format.

Return type:

numpy.ndarray

Raises:

ValueError – If input image is not grayscale.

cv3.color_spaces.gray2bgr(img)[source]

Convert grayscale image to BGR.

Parameters:

img (numpy.ndarray) – Input grayscale image.

Returns:

Image in BGR format.

Return type:

numpy.ndarray

Raises:

ValueError – If input image is not grayscale.

cv3.color_spaces.gray2rgba(img)[source]

Convert grayscale image to RGBA.

Parameters:

img (numpy.ndarray) – Input grayscale image.

Returns:

Image in RGBA format.

Return type:

numpy.ndarray

Raises:

ValueError – If input image is not grayscale.

cv3.color_spaces.gray2bgra(img)[source]

Convert grayscale image to BGRA.

Parameters:

img (numpy.ndarray) – Input grayscale image.

Returns:

Image in BGRA format.

Return type:

numpy.ndarray

Raises:

ValueError – If input image is not grayscale.

cv3.color_spaces.bgr2hsv(img)[source]

Convert image from BGR to HSV color space.

Parameters:

img (numpy.ndarray) – Input image in BGR format.

Returns:

Image in HSV format.

Return type:

numpy.ndarray

cv3.color_spaces.rgb2hsv(img)[source]

Convert image from RGB to HSV color space.

Parameters:

img (numpy.ndarray) – Input image in RGB format.

Returns:

Image in HSV format.

Return type:

numpy.ndarray

cv3.color_spaces.hsv2bgr(img)[source]

Convert image from HSV to BGR color space.

Parameters:

img (numpy.ndarray) – Input image in HSV format.

Returns:

Image in BGR format.

Return type:

numpy.ndarray

cv3.color_spaces.hsv2rgb(img)[source]

Convert image from HSV to RGB color space.

Parameters:

img (numpy.ndarray) – Input image in HSV format.

Returns:

Image in RGB format.

Return type:

numpy.ndarray

cv3.color_spaces.rgb(img)

Alias for rgb2bgr().

cv3.color_spaces.bgr(img)

Alias for bgr2rgb().

cv3.color_spaces.rgba(img)

Alias for rgba2bgra().

cv3.color_spaces.bgra(img)

Alias for bgra2rgba().

cvt_color(img, code)

Convert image between different color spaces.

cvtColor(img, code)

Alias for cvt_color().

rgb2bgr(img)

Convert image from RGB to BGR color space.

bgr2rgb(img)

Convert image from BGR to RGB color space.

rgba2bgra(img)

Convert image from RGBA to BGRA color space.

bgra2rgba(img)

Convert image from BGRA to RGBA color space.

rgb2gray(img)

Convert image from RGB to grayscale.

bgr2gray(img)

Convert image from BGR to grayscale.

gray2rgb(img)

Convert grayscale image to RGB.

gray2bgr(img)

Convert grayscale image to BGR.

gray2rgba(img)

Convert grayscale image to RGBA.

gray2bgra(img)

Convert grayscale image to BGRA.

bgr2hsv(img)

Convert image from BGR to HSV color space.

rgb2hsv(img)

Convert image from RGB to HSV color space.

hsv2bgr(img)

Convert image from HSV to BGR color space.

hsv2rgb(img)

Convert image from HSV to RGB color space.

rgb(img)

Alias for rgb2bgr().

bgr(img)

Alias for bgr2rgb().

rgba(img)

Alias for rgba2bgra().

bgra(img)

Alias for bgra2rgba().