Image Processing Transformer
This section will explain the concepts and parameters of each transformer you use when scaling and cropping images, and provide you with reference to the use of samples to make it easier for you to perform transformer operations.
Before reading the contents of the document, please note the following:
The image data is three-dimensional data, but the default first dimension of the transformer provided by Horizon is N-dimensional, and it will process the data with the first dimension split cycle, so if you need to do processing on the image, please provide four-dimensional data.
AddTransformer
Description:
Adds values to all the pixel values in the input image. The transformer converts the data format to float32 in the output.
Parameters:
- value: Value to be added to each pixel. Note that value can be negative, e.g., -128.
Examples of use:
MeanTransformer
Description:
subtracts the mean_value from all pixel values in the input image.
Parameters:
- means: Subtracted value of each pixel. Note that the value can be a negative number, e.g., -128.
- data_format: Layout type of the input. Value range: ["CHW", "HWC"], defaults to "CHW".
Examples of use:
ScaleTransformer
Description:
Multiplies all pixel values in the input image by the data_scale factor.
Parameters:
- scale_value: Factor to be multiplied, such as 0.0078125 or 1/128.
Examples of use:
NormalizeTransformer
Description:
Normalizes the input image and converts the data format to float32 in the output.
Parameters:
- std: Value by which the first input image needs to be divided.
Examples of use:
TransposeTransformer
Description:
Operation used to perform the layout conversion.
Parameters:
- order: Order of the input image after the layout conversion (the order is related to the original layout order), e.g., suppose HWC order is (0,1,2), when converted to CHW, the order is (2,0,1).
Examples of use:
HWC2CHWTransformer
Description:
Converts NHWC to NCHW.
Parameters: None.
Examples of use:
CHW2HWCTransformer
Description:
Converts NCHW to NHWC.
Parameters: None.
Examples of use:
CenterCropTransformer
Description:
Cuts out a square image from the center of the image by directly truncating the value. This transformer will convert the data format to float32 in the output.
When the value of data_type is uint8, the output is uint8.
Parameters:
- crop_size: Size of the sides of the square cropped from the center.
- data_type: Type of the output result. Value range: ["float", "uint8"].
Examples of use:
PILCenterCropTransformer
Description:
Crops a square image from the center of the image by using the PIL method. This transformer will convert the data format to float32 in the output.
Parameters:
- size: Size of the sides of the square cropped from the center.
Examples of use:
LongSideCropTransformer
Descriptions:
Crops the longer side. This transformer will convert the data format to float32 in the output.
When width > height, crops a square based on the height, e.g., suppose width=100 and height=70, then the size after cropping is 70*70.
When height > width, crops a rectangle whose width remains the same and height is (height-weight)/2 + width, e.g., suppose width=70 and height=100, the size after cropping is 70*(100-70) /2+70 , which is a rectangle of size 70*85.
Parameters: None.
Examples of use:
PadResizeTransformer
Description:
Enlarges the image by using the padding method. The transformer converts the data format to float32 in the output.
Parameters:
- target_size: Target size. The value is a tuple, e.g., (240,240).
- pad_value: Value to be padded into the array, defaults to 127.
- pad_position: Padding position. Values range: ["boundary","bottom_right"], defaults to "boundary".
Examples of use:
ResizeTransformer
Description:
Resizes the image.
Parameters:
-
target_size: Target size. The value is a tuple, e.g., (240,240): width=240, height=240.
-
mode: Image processing mode, takes a range of values ("skimage", "opencv"), defaults to "skimage".
-
method: Interpolation method, this parameters only works in the skimage mode. Value range: [0,5], defaults to 1, of which :
- 0: Nearest-neighbor;
- 1: Bi-linear(default);
- 2: Bi-quadratic;
- 3: Bi-cubic;
- 4: Bi-quartic;
- 5: Bi-quintic.
-
data_type: Output type. Value range: (uint8, float), defaults to
float. When set touint8, the output type isuint8, otherwise it isfloat32. -
interpolation: Interpolation method. This parameter only takes effect when mode is
opencv. Value range: (opencv's interpolation method), defaults tonull. Currently, interpolation only supports empty, or two interpolation methods ofINTER_CUBICin OpenCV, when it is empty, INTER_LINEAR method is used by default.The following are the interpolation methods supported in OpenCV and their descriptions (interpolation methods not supported will be gradually added in subsequent iterations):
- INTER_NEAREST: Nearest Neighbor Interpolation.
- INTER_LINEAR: Bi-linear interpolation, used by default when the interpolation is empty.
- INTER_CUBIC: Bi-cubic interpolation within a 4x4 pixel neighborhood.
- INTER_AREA: Resampling using pixel area relation. It may be the preferred method for image decimation as it can provide moiré-free results. But when the image is scaled, it is similar to the INTER_NEAREST method.
- INTER_LANCZOS4: Lanczos interpolation of 8x8 neighborhood.
- INTER_LINEAR_EXACT: Bit-accurate bilinear interpolation.
- INTER_NEAREST_EXACT: Bit-exact nearest neighbor interpolation. This will produce the same results as the nearest neighbor method in PIL, scikit-image, or Matlab.
- INTER_MAX: Mask for interpolation code.
- WARP_FILL_OUTLIERS: Flag, padding all target image pixels. If some of them correspond to outliers in the source image, set them to zero.
- WARP_INVERSE_MAP: Flag, inverter.
Examples of use:
PILResizeTransformer
Description:
Resizes the image by using the PIL library.
Parameters:
- size: Target size. The value is a tuple, e.g., (240,240).
- interpolation: Specifies the interpolation method. Value range: (Image.NEAREST, Image.BILINEAR, Image.BICUBIC, Image.LANCZOS), default to
Image.BILINEAR.- Image.NEAREST: Nearest neighbor sampling;
- Image.BILINEAR: Linear interpolation;
- Image.BICUBIC: Cubic spline interpolation;
- Image.LANCZOS: High quality downsampling filter.
Examples of use:
ShortLongResizeTransformer
Description:
Scales the input image according to the original scale, and the size of the new image is related to the parameters set.
Perform the operation as follows:
- First, divide the size of
short_sizeby the smaller value of the width and height of the original image, and use this value as the scaling factor. - When the scaling factor is multiplied by the larger value of the width and height of the original image and the result is greater than the value of
long_size, the scaling factor will be changed tolong_sizedivided by the larger value of the width and height of the original image. - Use the
resizemethod in OpenCV to re-crop the image according to the scaling factor obtained above.
Parameters:
- short_size: Expected length of the short edge after cutting.
- long_size: Expected length of the short edge after cutting.
- include_im: Defaults to True. When set to True, it will return the original image in addition to the processed image.
Examples of use:
PadTransformer
Description:
Resizes the image by dividing the target size by the larger value of the width or height of the input image, and then multiplying this factor by the original width and height.
Then according to the size of the new image, divide it by size_divisor and round it up, then multiply it by size_divisor to generate a new image with the new width and height.
Parameters:
- size_divisor: Size divisor, defaults to 128.
- target_size: Target size, defaults to 512.
Examples of use:
ShortSideResizeTransformer
Description:
According to the desired length of the short side, this transformer use the current ratio of the long and short sides, and the crop out the operation of the new image size from the image center.
Parameters:
-
short_size: Expected length of short side.
-
data_type: Type of the output result. Value range: ("float", "uint8"), defaults to "float32", output in
float32type, and when set touint8, the output type will beuint8. -
interpolation: Interpolation method. Value range: (interpolation method used in OpenCV), defaults to empty. Currently, interpolation only supports empty, or two interpolation methods of
INTER_CUBICin OpenCV, when it is empty, INTER_LINEAR method is used by default.The following are the interpolation methods supported in OpenCV and their descriptions (interpolation methods not supported will be gradually added in subsequent iterations).
- INTER_NEAREST: Nearest Neighbor Interpolation.
- INTER_LINEAR: Bi-linear interpolation, which is used by default when the interpolation is empty.
- INTER_CUBIC: Bi-cubic interpolation within a 4x4 pixel neighborhood.
- INTER_AREA: Resampling using pixel area relation. It may be the preferred method for image decimation as it can provide moiré-free results. But when the image is scaled, it is similar to INTER_NEAREST method.
- INTER_LANCZOS4: Lanczos interpolation of 8x8 neighborhood.
- INTER_LINEAR_EXACT: Bit-accurate bilinear interpolation.
- INTER_NEAREST_EXACT: Bit-exact nearest neighbor interpolation. This will produce the same results as the nearest neighbor method in PIL, scikit-image, or Matlab.
- INTER_MAX: Mask for interpolation code.
- WARP_FILL_OUTLIERS: Flag, padding all target image pixels. If some of them correspond to outliers in the source image, set them to zero.
- WARP_INVERSE_MAP; Flag, inverter.
Examples of use:
PaddedCenterCropTransformer
Description:
Crops the center of the image with padding.
Applicable only to EfficientNet-lite related instance models.
Calculation method:
- Calculates the factor, int((float( image_size ) / ( image_size + crop_pad )).
- Calculates the size of the center, coefficient * np.minimum(height of original image, width of original image)).
- Crops the image from its center according to the calculated size.
Parameters:
- image_size: Size of the image, defaults to 224.
- crop_pad: Size of the center padding, defaults to 32.
Examples of use:
BGR2RGBTransformer
Description:
Converts the input format from BGR to RGB.
Parameters:
- data_format: Data format. Value range: (CHW,HWC), defaults to CHW.
Examples of use:
RGB2BGRTransformer
Description:
Converts the input format from RGB to BGR.
Parameters:
- data_format: Data format.Value range: (CHW,HWC), defaults to CHW.
Examples of use:
RGB2GRAYTransformer
Description:
Converts the input format from RGB to GRAY.
Parameters:
- data_format: Input layout type. Value range: ("CHW", "HWC"), defaults to "CHW".
Examples of use:
BGR2GRAYTransformer
Description:
Converts the input format from BGR to GRAY.
Parameters:
- data_format: Input layout type. Value range: ["CHW", "HWC"], defaults to "CHW".
Examples of use:
RGB2GRAY_128Transformer
Description:
Converts the input format from RGB to GRAY_128. Value range of GRAY_128: (-128,127).
Parameters:
- data_format: Input layout type. Value range: ["CHW", "HWC"], required field, defaults to "CHW".
Examples of use:
RGB2YUV444Transformer
Description:
Converts the input format from RGB to YUV444.
Parameters:
- data_format: Input layout type. Value range: ["CHW", "HWC"], required field, defaults to "CHW".
Examples of use:
BGR2YUV444Transformer
Description:
Convers the input format from BGR to YUV444.
Parameters:
- data_format: Input layout type. Value range: ["CHW", "HWC"], required field, defaults to "CHW".
Examples of use:
BGR2YUV444_128Transformer
Description:
Converts the input format from BGR to YUV444_128. Values range of YUV444_128: (-128,127).
Parameters:
- data_format: Input layout type. Value range: ["CHW", "HWC"], required field, defaults to "CHW".
Examples of use:
RGB2YUV444_128Transformer
Description:
Converts the input format from RGB to YUV444_128. Values range of YUV444_128: (-128,127).
Parameters:
- data_format: Input layout type. Value range: ["CHW", "HWC"], required field, defaults to "CHW".
Examples of use:
BGR2YUVBT601VIDEOTransformer
Description:
Converts the input format from BGR to YUV_BT601_Video_Range.
YUV_BT601_Video_Range, some camera input data are YUV BT601 (Video Range) format. Value range: 16~235, this transformer is adapted to this format of data generated.
Parameters:
- data_format: Input layout type. Value range: ["CHW","HWC"], required field, defaults to "CHW".
Examples of use:
RGB2YUVBT601VIDEOTransformer
Description:
Converts the input format from RGB to YUV_BT601_Video_Range.
YUV_BT601_Video_Range, some camera input data are YUV BT601 (Video Range) format. Value range: 16~235, this transformer is adapted to this format of data generated.
Parameters:
- data_format: Input layout type. Value range: ["CHW","HWC"], required field, defaults to "CHW".
Examples of use:
YUVTransformer
Description:
Converts the input format to YUV444.
Parameters:
- color_sequence: Color sequence, required field.
Examples of use:
ReduceChannelTransformer
Description:
Reduces the C channel to a single channel. The transformer is mainly for C channel, such as shape 1*3*224*224 to 1*1*224*224. In practice, the layout must be aligned with data_format value to avoid causing the wrong channel deletion.
Parameters:
- data_format: Input layout type. Value range: ["CHW", "HWC"], defaults to "CHW".
Examples of use:
BGR2NV12Transformer
Description:
Converts the input format from BGR to NV12.
Parameters:
- data_format: Input layout type. Value range: ["CHW","HWC"], defaults to "CHW".
- cvt_mode: cvt mode. Value range: (rgb_calc, OpenCV), defaults to rgb_calc.
- rgb_calc: Image processing using mergeUV.
- opencv: Image processing using OpenCV.
Examples of use:
RGB2NV12Transformer
Description:
Converts the input format from RGB to NV12.
Parameters:
- data_format: Input layout type. Value range: ["CHW", "HWC"], defaults to "CHW".
- cvt_mode: cvt mode. Value range: (rgb_calc,opencv), defaults to rgb_calc.
- rgb_calc: Image processing using mergeUV.
- opencv: Image processing using opencv.
Examples of use:
NV12ToYUV444Transformer
Description:
Converts the input format from NV12 to YUV444.
Parameters:
- target_size: Target size. Value is a tuple, e.g., (240,240).
- yuv444_output_layout: YUV444 output layout. Value range: (HWC,CHW), defaults to "HWC".
Examples of use:
WarpAffineTransformer
Description:
Performs image affine transformations.
Parameters:
- input_shape: Input shape value.
- scale: Factor to be multiplied.
Examples of use:
F32ToS8Transformer
Description:
Convers the input format from float32 to int8.
Parameters: None.
Examples of use:
F32ToU8Transformer
Description:
Converts the input format from float32 to uint8.
Parameters: None.
Examples of use:
