MNN icon indicating copy to clipboard operation
MNN copied to clipboard

Resize, padding image to specific size using MNN ImageProcessig

Open AnhPC03 opened this issue 1 year ago • 3 comments

平台(如果交叉编译请再附上交叉编译目标平台):

Platform(Include target platform as well if cross-compiling):

Ubuntu/Linux

Github版本:

Github Version: 2.8.1

编译方式:

Compiling Method

Downloaded libMNN.so file from release

Details

Hello @jxt1234, I've searched many places for solving this problem but haven't been success. Could you help me? Thank you very much.

I have an image having resolution 1000x748. I calculated some values to resize the origin image to 320x238 (my aim is to keep original ratio as much as possible) and then added some padding values (128, 128, 128) to archive image with size 320x320 because my model accepts 320x320 input resolution. I don't want to resize directly from 1000x748 to 320x320 because of distorting.

If using opencv lib, I did like this, it worked

int resizedWidth = 320;
int resizedHeight = 238;
int Pad_W = 0;
int Pad_H = (320 - 238) / 2;
cv::resize(croppedImg, resizedImg, cv::Size(resizedWidth, resizedHeight), 0.0, 0.0, 2);
cv::copyMakeBorder(resizedImg, resizedImg, Pad_H, Pad_H, Pad_W, Pad_W,
                       cv::BORDER_CONSTANT, cv::Scalar(128, 128, 128));

In MNN, I wrote as following but didn't work (output wrong results)

int model_w = 320;
int model_h = 320;
int img_w = 1000;
int img_h = 748;
int img_c = 3;
int resized_w = 320;
int resized_h = 238;

    config.wrap = MNN::CV::Wrap::ZERO;
    MNN::Tensor *tensorPtr = netPtr->getSessionInput(sessionPtr, nullptr);
    MNN::CV::Matrix transform;
    // auto resize for full conv network.
    std::vector<int> dims = {1, img_c, model_h, model_w};
    netPtr->resizeTensor(tensorPtr, dims);
    netPtr->resizeSession(sessionPtr);
    transform.preScale(1.0f / (float)img_w, 1.0f / (float)img_h);
    transform.preScale((float)resized_w, (float)resized_h);
    transform.postScale(1.0f / (float)model_w, 1.0f / (float)model_h);
    transform.postScale((float)img_w, (float)img_h);
    MNN::CV::ImageProcess *process = MNN::CV::ImageProcess::create(config);
    process->setPadding(128);
    process->setMatrix(transform);
    process->convert(data, img_w, img_h, img_w * img_c, tensorPtr);
    netPtr->runSession(sessionPtr);

BTW, could you guide me how to check input image before runSession. Thank you very much.

AnhPC03 avatar Jan 10 '24 10:01 AnhPC03

getSessionInput

v0jiuqi avatar Jan 16 '24 07:01 v0jiuqi

@v0jiuqi Thank you for your response. getSessionInput is to get input image, right?

Could you help me resize and padding problem using MNN? Thank you

AnhPC03 avatar Jan 16 '24 08:01 AnhPC03

@wangzhaode Could you help me about resize and padding problem? Thank you.

AnhPC03 avatar Jan 30 '24 07:01 AnhPC03

  1. See demo/exec/pictureRotate.cpp. Dump your output to png to check if the matrix is right.
  2. You can try to use mnn-opencv (-DMNN_BUILD_OPENCV=true). It's method is like opencv.

jxt1234 avatar Feb 20 '24 02:02 jxt1234

@jxt1234 Thank you very much.

AnhPC03 avatar Mar 04 '24 03:03 AnhPC03