Files
kunlun/export/inc/cnn/cnn_image_pre_process.h
2024-09-28 14:24:04 +08:00

214 lines
11 KiB
C

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
#ifndef CNN_IMAGE_PRE_PROCESS_H
#define CNN_IMAGE_PRE_PROCESS_H
/* os shim includes */
#include "os_types_api.h"
#ifdef __cplusplus
extern "C" {
#endif
/** \defgroup cnn image pre-process APIs
* @brief cnn image pre-process APIs
* These function could be used for resize or crop an image.
* Arranged for cnn engine means every column will occupy a size which is a multiple of 32 bytes.
* For example, if an image named A is of size 300 * 300, it will be saved in the memory of this form so that every column occupies 320 bytes.
* A[0][0], A[0][1], ..., A[0][299], 0, 0, 0, ..., 0, A[1][0], A[1][1], ..., A[1][299], 0, 0, 0, 0, ..., 0
*
*
*
*/
/** @addtogroup cnn_image_pre_process_APIs
* @{
*/
/* @brief image_resize_with_crop_or_pad() - resize image to given height and width, using padding or cropping, output will be arranged for cnn engine
* @param input: where to put the input image
* @param channel: the channel of the input image, usually 1 or 3
* @param input_height: the height of input image
* @param input_width: the width of input image
* @param output: where to put the output image
* @param output_height: the given height
* @param output_width: the given width
* @param in_for_cnn: non-zero - input is arranged for cnn engine
* @return 0 -- succeed
* @return 1 -- channel is 0
* @return 2 -- input_height is 0
* @return 3 -- input_width is 0
* @return 4 -- output_height is 0
* @return 5 -- output_width is 0
*/
uint8_t image_resize_with_crop_or_pad(int8_t *input, uint16_t channel,
uint16_t input_height, uint16_t input_width, int8_t *output,
uint16_t output_height, uint16_t output_width, uint8_t in_for_cnn);
/* @brief image_pad_to_bounding_box() - add offset_height rows of zeros on top, offset_width columns of zeros on the left, and then pads the image on the bottom and right with zeros until it has dimensions output_height, output_width, output will be arranged for cnn engine
* @param input: where to put the input image
* @param channel: the channel of the input image, usually 1 or 3
* @param input_height: the height of input image
* @param input_width: the width of input image
* @param offset_height: the number of rows of zeros added on the top
* @param offset_width: the number of columns of zeros added on the left
* @param output: where to put the output image
* @param output_height: the height of output image
* @param output_width: the width of output image
* @param in_for_cnn: non-zero - input is arranged for cnn engine
* @return 0 -- succeed
* @return 1 -- channel is 0
* @return 2 -- input_height is 0
* @return 3 -- input_width is 0
* @return 4 -- output_height < input_height + offset_height
* @return 5 -- output_width < input_width + offest_width
*/
uint8_t image_pad_to_bounding_box(int8_t *input, uint16_t channel,
uint16_t input_height, uint16_t input_width, uint16_t offest_height,
uint16_t offset_width, int8_t *output, uint16_t output_height,
uint16_t output_width, uint8_t in_for_cnn);
/* @brief image_pad_to_bounding_box() - add offset_height rows of zeros on top, offset_width columns of zeros on the left, and then pads the image on the bottom and right with zeros until it has dimensions output_height, output_width, output will be arranged for cnn engine
* @param input: where to put the input image
* @param channel: the channel of the input image, usually 1 or 3
* @param input_height: the height of input image
* @param input_width: the width of input image
* @param offset_height: the number of rows of zeros added on the top
* @param offset_width: the number of columns of zeros added on the left
* @param output: where to put the output image
* @param output_height: the height of output image
* @param output_width: the width of output image
* @param in_for_cnn: non-zero - input is arranged for cnn engine
* @return 0 -- succeed
* @return 1 -- channel is 0
* @return 2 -- input_height is 0
* @return 3 -- input_width is 0
* @return 4 -- output_height < input_height + offset_height
* @return 5 -- output_width < input_width + offest_width
*/
uint8_t image_crop_to_bounding_box(int8_t *input, uint16_t channel,
uint16_t input_height, uint16_t input_width, uint16_t offest_height,
uint16_t offset_width, int8_t *output, uint16_t output_height,
uint16_t output_width, uint8_t in_for_cnn);
/* @brief image_crop_and_resize_bilinear() - crop a rectangular part from the given image, and resize (using bilinear interpolation) the part to the given size, output will be arranged for cnn engine, crop_end_x / resize_ratio_x and crop_end_y / resize_ratio_y should be smaller than 2047, where ratio_x = width_of_rectangular_width / output_width, ratio_y = height_of_rectangular_part / output_height
* @param input: where to put the input image
* @param channel: the channel of the input image, usually 1 or 3
* @param input_height: the height of input image
* @param input_width: the width of input image
* @param crop_begin_x: the beginning index(along Width of the image) of the rectangular part
* @param crop_begin_y: the beginning index(along Height of the image) of the rectangular part
* @param crop_end_x: the end index(along Width of the image) of the rectangular part
* @param crop_end_y: the end index(along Height of the image) of the rectangular part
* @param output_height: the height of output image
* @param output_width: the width of output image
* @param output: where to put the output image
* @param in_for_cnn: non-zero - input is arranged for cnn engine
* @return 0 -- succeed
* @return 1 -- channel is 0
* @return 2 -- input_height is 0
* @return 3 -- input_width is 0
* @return 4 -- output_height is 0
* @return 5 -- output_width is 0
* @return 6 -- crop_begin_x >= crop_end_x
* @return 7 -- crop_begin_y >= crop_end_y
* @return 8 -- crop_end_x >= input_width
* @return 9 -- crop_end_y >= input_height
* @return 10 -- the resize ratio is too large or too small,
* please make sure that the resize ratio is in (1/8, 4)
* @return 11 -- crop_end_x or crop_end_y is too large,
* please make sure that crop_end_x / resize_ratio_x and crop_end_y / resize_ratio_y is smaller than 2047
* @return 12 -- the width of the rectangular part is too small, it should be no less than 16
* @return 13 -- the width of the input image must be multiples of 4
*/
uint8_t image_crop_and_resize_bilinear(uint8_t *input, uint8_t channel,
uint16_t input_height, uint16_t input_width, uint16_t crop_begin_x,
uint16_t crop_begin_y, uint16_t crop_end_x, uint16_t crop_end_y,
uint16_t output_height, uint16_t output_width, uint8_t *output,
uint8_t in_for_cnn);
/* @brief image_crop_and_resize_nearest_neighbor() - crop a rectangular part from the given image, and resize (using nearest neighbor interpolation) the part to the given size, output will be arranged for cnn engine
* @input: where to put the input image
* @channel: the channel of the input image, usually 1 or 3
* @input_height: the height of input image
* @input_width: the width of input image
* @crop_begin_x: the beginning index(along Width of the image) of the rectangular part
* @crop_begin_y: the beginning index(along Height of the image) of the rectangular part
* @crop_end_x: the end index(along Width of the image) of the rectangular part
* @crop_end_y: the end index(along Height of the image) of the rectangular part
* @output_height: the height of output image
* @output_width: the width of output image
* @output: where to put the output image
* @in_for_cnn: non-zero - input is arranged for cnn engine
* @return 0 -- succeed
* @return 1 -- channel is 0
* @return 2 -- input_height is 0
* @return 3 -- input_width is 0
* @return 4 -- output_height is 0
* @return 5 -- output_width is 0
* @return 6 -- crop_begin_x >= crop_end_x
* @return 7 -- crop_begin_y >= crop_end_y
* @return 8 -- crop_end_x >= input_width
* @return 9 -- crop_end_y >= input_height
*/
uint8_t image_crop_and_resize_nearest_neighbor(uint8_t *input,
uint8_t channel, uint16_t input_height, uint16_t input_width,
uint16_t crop_begin_x, uint16_t crop_begin_y, uint16_t crop_end_x,
uint16_t crop_end_y, uint16_t output_height, uint16_t output_width,
uint8_t *output, uint8_t in_for_cnn);
/* @brief image_crop_and_pad_right_down() - crop a rectangular part from the given image, and reserve the positioin for the padding part used for padding to the destin size, if the height of the part is larger than destin height, output height will be the height of the part, if the width of the part is larger than destin width, output width will be the width of the part, output will be arranged for cnn engine
* @input_addr: the address where the input image is put
* @channel: the channel of the input image, usually 1 or 3
* @in_height: the height of input image
* @in_width: the width of input image
* @crop_begin_h: the beginning index(along Height of the image) of the rectangular part
* @crop_begin_w: the beginning index(along Width of the image) of the rectangular part
* @crop_end_h: the end index(along Height of the image) of the rectangular part
* @crop_end_h: the end index(along Width of the image) of the rectangular part
* @output_addr: the address where to put the output image
* @out_height: the height of output image
* @out_width: the width of output image
* @in_for_cnn: non-zero - input is arranged for cnn engine
* @out_for_cnn: non-zero - output is arranged for cnn engine, zero - output will be arranged of 4-bytes-aligned form
* @return 0 -- succeed
* @return 1 -- channel is 0
* @return 2 -- input_height is 0
* @return 3 -- input_width is 0
* @return 6 -- crop_begin_h >= crop_end_h
* @return 7 -- crop_begin_w >= crop_end_w
* @return 8 -- crop_end_h >= input_width
* @return 9 -- crop_end_w >= input_height
* @return 10 -- the width of input image is not a multiple of 4
* @return 11 -- crop_end_h or crop_end_w is larger than 2047
* @return 12 -- the width of the rectangular part is too small, it should be no less than 16
*/
uint8_t image_crop_and_pad_right_down(uint32_t input_addr, uint16_t channel,
uint16_t in_height, uint16_t in_width, uint16_t crop_begin_h,
uint16_t crop_begin_w, uint16_t crop_end_h, uint16_t crop_end_w,
uint32_t output_addr, uint16_t out_height, uint16_t out_width,
uint8_t in_for_cnn, uint8_t out_for_cnn);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif //CNN_IMAGE_PRE_PROCESS_H