mindspore.dataset.vision.ConvertColor

View Source On Gitee
class mindspore.dataset.vision.ConvertColor(convert_mode)[source]

Change the color space of the image.

Supports Ascend hardware acceleration and can be enabled through the .device("Ascend") method.

Parameters

convert_mode (ConvertMode) –

The mode of image channel conversion.

  • ConvertMode.COLOR_BGR2BGRA, Convert BGR image to BGRA image.

  • ConvertMode.COLOR_RGB2RGBA, Convert RGB image to RGBA image.

  • ConvertMode.COLOR_BGRA2BGR, Convert BGRA image to BGR image.

  • ConvertMode.COLOR_RGBA2RGB, Convert RGBA image to RGB image.

  • ConvertMode.COLOR_BGR2RGBA, Convert BGR image to RGBA image.

  • ConvertMode.COLOR_RGB2BGRA, Convert RGB image to BGRA image.

  • ConvertMode.COLOR_RGBA2BGR, Convert RGBA image to BGR image.

  • ConvertMode.COLOR_BGRA2RGB, Convert BGRA image to RGB image.

  • ConvertMode.COLOR_BGR2RGB, Convert BGR image to RGB image.

  • ConvertMode.COLOR_RGB2BGR, Convert RGB image to BGR image.

  • ConvertMode.COLOR_BGRA2RGBA, Convert BGRA image to RGBA image.

  • ConvertMode.COLOR_RGBA2BGRA, Convert RGBA image to BGRA image.

  • ConvertMode.COLOR_BGR2GRAY, Convert BGR image to GRAY image.

  • ConvertMode.COLOR_RGB2GRAY, Convert RGB image to GRAY image.

  • ConvertMode.COLOR_GRAY2BGR, Convert GRAY image to BGR image.

  • ConvertMode.COLOR_GRAY2RGB, Convert GRAY image to RGB image.

  • ConvertMode.COLOR_GRAY2BGRA, Convert GRAY image to BGRA image.

  • ConvertMode.COLOR_GRAY2RGBA, Convert GRAY image to RGBA image.

  • ConvertMode.COLOR_BGRA2GRAY, Convert BGRA image to GRAY image.

  • ConvertMode.COLOR_RGBA2GRAY, Convert RGBA image to GRAY image.

Raises
Supported Platforms:

CPU Ascend

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>>
>>> # Use the transform in dataset pipeline mode
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>>
>>> # Convert RGB images to GRAY images
>>> convert_op = vision.ConvertColor(vision.ConvertMode.COLOR_RGB2GRAY)
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=convert_op, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(100, 100) uint8
>>> # Convert RGB images to BGR images
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> convert_op = vision.ConvertColor(vision.ConvertMode.COLOR_RGB2BGR)
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=convert_op, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(100, 100, 3) uint8
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8)
>>> output = vision.ConvertColor(vision.ConvertMode.COLOR_RGB2GRAY)(data)
>>> print(output.shape, output.dtype)
(100, 100) uint8
Tutorial Examples:
device(device_target='CPU')[source]

Set the device for the current operator execution.

  • When the device is Ascend, input type only supports uint8 , input channel supports 1 and 3. The input data has a height limit of [4, 8192] and a width limit of [6, 4096].

Parameters

device_target (str, optional) – The operator will be executed on this device. Currently supports CPU and Ascend . Default: CPU .

Raises
  • TypeError – If device_target is not of type str.

  • ValueError – If device_target is not within the valid set of ['CPU', 'Ascend'].

Supported Platforms:

CPU Ascend

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>>
>>> # Use the transform in dataset pipeline mode
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> transforms_list = [vision.ConvertColor(vision.ConvertMode.COLOR_RGB2BGR).device("Ascend")]
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(100, 100, 3) uint8
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8)
>>> output = vision.ConvertColor(vision.ConvertMode.COLOR_RGB2BGR).device("Ascend")(data)
>>> print(output.shape, output.dtype)
(100, 100, 3) uint8
Tutorial Examples: