c++对图像的预处理:

在用c++部署测试mobilenet的时候,需要把Mat型图像转成TYPE*进行图像预处理,现在记录一下学习的过程:

opencv的Mat型与TYPE*的相处转换:

Mat转TYPE*

// 将cv::Mat转换为byte*类型
BYTE* matToBytePointer(const cv::Mat& image) {
    BYTE* imageData = new BYTE[image.rows * image.cols * image.channels()];
    memcpy(imageData, image.data, image.rows * image.cols * image.channels());
    return imageData;
}

TYPE*转Mat

cv::Mat bytePointerToMat(BYTE* imageData, int width, int height, int channels) {
    return cv::Mat(height, width, CV_MAKETYPE(CV_8U, channels), imageData);
}

c++实现TYPE*类型的图像的letterbox填充方法:

void preprocess_letterbox_myresize(BYTE* imageData, int width, int height, int channels, int newWidth, int newHeight, float out_data[]) {

    // 计算缩放的比例
    float x_r = static_cast<float>(width) / newWidth;
    float y_r = static_cast<float>(height) / newHeight;

    // 根据较长的边调整size,等比例缩放
    int H, W;
    if (x_r < y_r) {
        H = newHeight;
        W = (width * 1.0) / y_r;
    }
    else {
        H = height / x_r;
        W = newWidth;
    }

    // 用于存放resize之后的图像
    BYTE* resizedImageData = new BYTE[W * H * channels];


    // 最近邻插值resize
    float x_ratio = static_cast<float>(width) / W;
    float y_ratio = static_cast<float>(height) / H;

    int offset = 0;

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            int x = static_cast<int>(x_ratio * j + 0.5);
            int y = static_cast<int>(y_ratio * i + 0.5);
            int index = (y * width + x) * channels;
            for (int c = 0; c < channels; c++) {
                resizedImageData[offset++] = imageData[index];
                index++;
            }
        }
    }

    // BYTE*转Mat,显示图像
    cv::Mat outImg2 = bytePointerToMat(resizedImageData, W, H, 3);
    //cv::imshow("Image", outImg2);
    //cv::waitKey(0);

    // 用于存放填充之后的图像
    BYTE* img_letterbox = new BYTE[newWidth * newHeight * channels];

    // 计算填充的范围
    int d = (newWidth - W) / 2;

    // letterbox进行填充
    int offset1 = 0;
    for (int h = 0; h < newHeight; h++) {
        for (int w = 0; w < newWidth; w++) {
            if ((w - d) < 0 || w > W + d - 1) {
                for (int c = 0; c < channels; c++) {
                    img_letterbox[offset1++] = 114;
                }
            }
            else {
                int x = w - d;
                int y = h;
                int index = (y * W + x + 1) * channels - 1;  // 同时进行BGR->RGB
                for (int c = 0; c < channels; c++) {
                    img_letterbox[offset1++] = resizedImageData[index];
                    index--;
                }
                
            }
        }
    }
    cv::Mat outImg4 = bytePointerToMat(img_letterbox, newWidth, newHeight, 3);
    //cv::imshow("Image", outImg4);
    //cv::waitKey(0);

    // 将BYTE*转float,并进行标准化
    int m = 0;
    for (int i = 0; i < newWidth * newHeight * channels - 2; i = i + 3 ) {
        out_data[m] = (static_cast<float>(img_letterbox[i]) - 128.0) / 256.0;
        out_data[m + newWidth * newHeight] = (static_cast<float>(img_letterbox[i+1]) - 128.0) / 256.0;
        out_data[m + 2 * (newWidth * newHeight)] = (static_cast<float>(img_letterbox[i+2]) - 128.0) / 256.0;
        m++;
    }

    delete[] resizedImageData; // 释放内存
    delete[] img_letterbox;
}

最后得到的float型直接送入到mobilenet模型里面进行推理。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值