介绍
通过使用MNN自带接口,预处理图片数据,包括缩放和填充操作。
1.图片文件加载
// MNN 读取图片
// input_image_path为图片路径
int width, height, channel;
auto inputImage = stbi_load(input_image_path, &width, &height, &channel, 0);
// opencv拷出原图保存
// cv::Mat img(height,width,CV_8UC3,inputImage);
// cv::imwrite("image.jpg",img);
2.padding
// 计算缩放后宽高
scale = min(size_w * 1.f / width, size_h * 1.f / height);
int scaleWidth = (scale * width);
int scaleHeight = (scale * height);
// 使图片居顶
padTop = 0;
padLeft = 0;
// 使图片居中
// int padTop = (height - scaleHeight) / 2;
// int padLeft = (size_w - scaleWidth) / 2;
// // 构造Matrix应用缩放和平移
trans.postScale(scale, scale); // 缩放
trans.postTranslate((float)padLeft, (float)padTop); // 移动
trans.invert(&trans);
3.执行操作
ImageProcess::Config config;
config.filterType = BILINEAR;
float mean[3] = {0.0f, 0.0f, 0.0f};
float normals[3] = {1.0/255.0f, 1.0/255.0f, 1.0/255.0f};
memcpy(config.mean, mean, sizeof(mean));
memcpy(config.normal, normals, sizeof(normals));
config.sourceFormat = RGB;
config.destFormat = BGR;
// padding 填充值
config.wrap = MNN::CV::Wrap::ZERO;
halide_type_t ht(halide_type_code_t::halide_type_uint, 8, 1);
std::shared_ptr<ImageProcess> pretreat(ImageProcess::create(config), ImageProcess::destroy);
pretreat->setMatrix(trans);
pretreat->convert((uint8_t*)inputImage, width, height, 0, inputUser->host<uint8_t>() + inputUser->stride(0) * batch * inputUser->getType().bytes(), size_w, size_h, bpp, 0, ht);
// opencv拷出转换后的图像并保存
// auto img_data = inputUser->host<uint8_t>();
// cv::Mat img(size_h,size_w,CV_8UC3,img_data);
// cv::imwrite("prefer.jpg",img);