OpenCV以NCHW格式读取图像(样本数x通道x高度x宽度),我需要将其转换为NHWC格式(将数组的第2维移到最后)。在C++中,是否有一种从NCHW到NHWC转换的有效方法?我可以用一个3 for循环来完成这个任务,但是很明显这根本没有效率。
发布于 2022-02-09 21:44:51
这个简单的解决方案适用于OpenCV C++:
static void hwc_to_chw(cv::InputArray src, cv::OutputArray dst) {
std::vector<cv::Mat> channels;
cv::split(src, channels);
// Stretch one-channel images to vector
for (auto &img : channels) {
img = img.reshape(1, 1);
}
// Concatenate three vectors to one
cv::hconcat( channels, dst );
}
发布于 2022-09-26 07:57:37
使用OpenCV >= 4.6,您可以使用transposeND
(来自opencv2/core.hpp
)进行此类转换:
std::vector<int> order = {0, 2, 3, 1};
Mat inp, out; // inp: NCHW, out: NHWC
transposeND(inp, order, out);
https://stackoverflow.com/questions/69063147
复制相似问题