通过OpenCV函数把NCHW数据转换为NHWC格式

#include "opencv2/opencv.hpp"

void MatNchwToNhwc() {
    // 数据初始化: 模拟网络输出NCHW格式(2,3,4,5), 存储顺序RGB
    const int32_t image_n = 2;
    const int32_t image_c = 3;
    const int32_t image_h = 4;
    const int32_t image_w = 5;
    cv::Mat cv_prob = (cv::Mat_<uint8_t>(1, image_n * image_c * image_h * image_w) <<   // 0x19bc0418140
                       'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
                       'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G',
                       'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
                       'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
                       'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G',
                       'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B');
    std::cerr << "==> cv_prob" << std::endl << cv_prob << std::endl;

    int32_t ndims[] = {image_n, image_c, image_h, image_w};
    cv::Mat cv_nchw = cv_prob.reshape(1, 4, ndims);  // 复用内存 0x19bc0418140
    std::cerr << "==> cv_nchw MatTotal: " << cv_nchw.total() << std::endl;    // 120
    std::cerr << "==> cv_nchw Channels: " << cv_nchw.channels() << std::endl; // 1
    std::cerr << "==> cv_nchw Mat.row: " << cv_nchw.rows << std::endl;        // -1
    std::cerr << "==> cv_nchw Mat.col: " << cv_nchw.cols << std::endl;        // -1
    std::cerr << "==> cv_nchw MatSize: " << cv_nchw.size << std::endl;        // 2 x 3 x 4 x 5

    cv::Mat cv_nhwc;                          // 0123    0231
    std::vector<int32_t> order = {0,2,3,1};   // NCHW -> NHWC
    cv::transposeND(cv_nchw, order, cv_nhwc);  // 分配内存 0x19bc0417580
    std::cerr << "==> cv_nhwc MatTotal: " << cv_nhwc.total() << std::endl;    // 120
    std::cerr << "==> cv_nhwc Channels: " << cv_nhwc.channels() << std::endl; // 1
    std::cerr << "==> cv_nhwc Mat.row: " << cv_nhwc.rows << std::endl;        // -1
    std::cerr << "==> cv_nhwc Mat.col: " << cv_nhwc.cols << std::endl;        // -1
    std::cerr << "==> cv_nhwc MatSize: " << cv_nhwc.size << std::endl;        // 2 x 4 x 5 x 3

    cv::Mat cv_out = cv_nhwc.reshape(image_n*image_c, {image_h, image_w});  // 复用内存 0x19bc0417580
    std::cerr << "==> cv_out MatTotal: " << cv_out.total() << std::endl;      // 20
    std::cerr << "==> cv_out Channels: " << cv_out.channels() << std::endl;   // 6
    std::cerr << "==> cv_out Mat.row: " << cv_out.rows << std::endl;          // 4
    std::cerr << "==> cv_out Mat.col: " << cv_out.cols << std::endl;          // 5
    std::cerr << "==> cv_out MatSize: " << cv_out.size << std::endl;          // 4 x 5

    cv::Mat im_rgb;
    cv_out.convertTo(im_rgb, CV_8UC(image_n*image_c));  // 分配内存 0x19bc0417c40
    std::cerr << "==> im_rgb" << std::endl << im_rgb << std::endl;
    // [ 82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66;
    //   82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66;
    //   82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66;
    //   82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66,  82,  71,  66]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值