#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]
}
04-08
3497
3497
08-12
1794
1794

被折叠的 条评论
为什么被折叠?



