CvtColor

Function Usage

Converts the image color gamut type of a tensor to the specified type. Conversions between the following types are supported:

If keepMargin is set to true in the outputTensor, the output width is automatically aligned to a multiple of 16. The default value is false, indicating that the invalid boundary in the tensor is not reserved.

For details, see CSC.

Table 1 lists the CSC types supported by each product. The meanings of the symbols are as follows:
  • √: supported
  • x: not supported
Table 1 CSC types

CSC Type

Atlas 200I/500 A2 inference product

Atlas inference product

Atlas 800I A2 inference product

YUVSP420 (nv12) to YUV400 (GRAY)

YVUSP420 (nv21) to YUV400 (GRAY)

x

YUVSP420 (nv12) to RGB

x

YUVSP420 (nv12) to BGR

x

YVUSP420 (nv21) to RGB

x

YVUSP420 (nv21) to BGR

x

RGB to YUVSP420 (nv12)

x

RGB to YVUSP420 (nv21)

x

BGR to YUVSP420 (nv12)

x

BGR to YVUSP420 (nv21)

x

RGB to YUV400 (GRAY)

x

BGR to YUV400 (GRAY)

x

BGR to RGB

x

RGB to BGR

x

RGB to RGBA

x

RGBA to YUV400 (GRAY)

x

RGBA to RGB

x

GRAY to RGB

x

x

RGBA to mRGBA

x

x

The conversion from RGBA to mRGBA requires CANN 8.0.RC1 or later.

Prototype

1
APP_ERROR CvtColor(const Tensor &inputTensor, Tensor &outputTensor, const CvtColorMode &mode, bool keepMargin = false, AscendStream &stream = AscendStream::DefaultStream());

Parameters

Parameter

Input/Output

Description

inputTensor

Input

Tensor of type UINT8. The value cannot be empty. Memory needs to be allocated on the device or DVPP side.

  • Tensor in the shape of {height, width, number of channels}. The number of channels must match input format. RGBA-to-mRGBA conversion supports only 4 channels.
    NOTE:

    If the CSC type of inputTensor is YUVSP420 or YVUSP420, set the height 1.5 times the displayed image height when configuring the input tensor shape. The height of the tensor must be a multiple of 3, the width must be a multiple of 16, and the channel must be 1.

    When the inputTensor calls SetValidRoi to set the valid region, the value of y1 (valid height of the corresponding tensor) must be a multiple of 3, and the actual valid height of the corresponding image is y1/3 x 2.

  • The input tensor width range is [10, 4096], and the height range is [6, 4096]. If the format YUV400, YUVSP420, YVUSP420, or GRAY is contained, the width range is [18, 4096].
  • For a grayscale image, tensors in the {height x width} shape are supported.

outputTensor

Output

Tensor class, output tensor.

If this parameter is not empty, allocate memory on the device or DVPP side and set the data type to UINT8. For RGBA-to-mRGBA and GRAY-to-RGB conversions, the tensor type must match the input tensor.

mode

Input

Enumeration class, referring to the original type and target type of CSC. For details, see CvtColorMode.

When the mode color gamut type is COLOR_BGR2YUVSP420, COLOR_RGB2YUVSP420, COLOR_RGB2YVUSP420, or COLOR_BGR2YVUSP420:

  • It is recommended that the width and height of the input RGB tensor be even numbers. Otherwise, abnormal data may exist on the borders of the output image.
  • The height of the output tensor is 1.5 times the height of the input tensor. For example, if the input height is 4096, the output height is 6144, which exceeds the restrictions of some DVPP APIs. Check whether the subsequent service requirements are met.

keepMargin

Input

Specifies whether to retain the invalid boundary in the tensor in the outputTensor. The default value is false, indicating that the invalid boundary in the tensor is not reserved. If keepMargin is set to true, the output width is automatically aligned with 16 and the invalid region is reserved.

This parameter is invalid when mode is set to COLOR_GRAY2RGB or COLOR_RGBA2mRGBA.

stream

Input

AscendStream type. The default value is AscendStream::DefaultStream(). When the parameter value is the default value, the API calling is a synchronous operation. In other cases, the API calling is an asynchronous operation.

Response Parameters

Data Structure

Description

APP_ERROR

For details about the returned error codes, see APP_ERROR Description.