The main purpose of image preprocessing is to eliminate irrelevant information in the image, restore useful real information, enhance the detectability of related information and simplify data to the greatest extent, thereby improving the reliability of feature extraction, image segmentation, matching and recognition. Here, by creating a LiteMat object, the image data is processed before inference to meet the data format requirements for model inference.
The process is as follows:
#include "lite_cv/lite_mat.h"
#include "lite_cv/image_process.h"
Here, the InitFromPixel function in the image_process.h
file is used to initialize the image.
bool InitFromPixel(const unsigned char *data, LPixelType pixel_type, LDataType data_type, int w, int h, LiteMat &m)
// Create the data object of the LiteMat object.
LiteMat lite_mat_bgr;
// Initialize the lite_mat_bgr object.
// The image data pointer passed in by the user (The data in the Bitmap corresponding to the Android platform).
InitFromPixel(pixel_ptr, LPixelType::RGBA2GRAY, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
The image processing operations here can be used in any combination according to the actual situation.
Here we use the ResizeBilinear function in image_process.h
to resize the image through a bilinear algorithm. Currently, the supported data type is unit8, and the supported channels are 3 and 1.
bool ResizeBilinear(const LiteMat &src, LiteMat &dst, int dst_w, int dst_h)
// Initialize the image data.
LiteMat lite_mat_bgr;
InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
// Create a resize image data object.
LiteMat lite_mat_resize;
// Resize the image.
ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256);
Here we use the ConvertTo function in image_process.h
to convert the image data type. Currently, the conversion from uint8 to float is supported.
bool ConvertTo(const LiteMat &src, LiteMat &dst, double scale = 1.0)
// Initialize the image data.
LiteMat lite_mat_bgr;
InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
// Create the converted data type object.
LiteMat lite_mat_convert_float;
// Perform conversion type operations on the object. Currently, the supported conversion is to convert uint8 to float.
ConvertTo(lite_mat_bgr, lite_mat_convert_float);
Here we use the Crop function in image_process.h
to crop the image. Currently, channels 3 and 1 are supported.
bool Crop(const LiteMat &src, LiteMat &dst, int x, int y, int w, int h)
// Initialize the image data.
LiteMat lite_mat_bgr;
InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
// Create the cropped object.
LiteMat lite_mat_cut;
// The image is cropped by the values of x, y, w, h.
Crop(lite_mat_bgr, lite_mat_cut, 16, 16, 224, 224);
In order to eliminate the dimensional influence among the data indicators and solve the comparability problem among the data indicators through standardization processing is adopted, here is the use of the SubStractMeanNormalize function in image_process.h
to normalize the image data.
bool SubStractMeanNormalize(const LiteMat &src, LiteMat &dst, const std::vector<float> &mean, const std::vector<float> &std)
// Initialize the image data.
LiteMat lite_mat_bgr;
InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr);
// The mean value of the image data.
// The variance of the image data.
std::vector<float> means = {0.485, 0.456, 0.406};
std::vector<float> stds = {0.229, 0.224, 0.225};
// Create a normalized image object.
LiteMat lite_mat_bgr_norm;
// The image data is normalized by the mean value and variance of the image data.
SubStractMeanNormalize(lite_mat_bgr, lite_mat_bgr_norm, means, stds);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。