Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Image Mat to 1D CHW(RR...R, GG..G, BB..B) vector

Tags:

c++

opencv

cudnn

Nvidia's cuDNN for deep learning has a rather interesting format for images called CHW. I have a cv::Mat img; that I want to convert to a one-dimensional vector of floats. The problem that I'm having is that the format of the 1D vector for CHW is (RR...R, GG..G,BB..B).

So I'm curious as to how I can extract the channel values for each pixel and order them for this format.

like image 941
Gepard Avatar asked Sep 12 '25 13:09

Gepard


1 Answers

I faced with same problem and and solve it in that way:

#include <opencv2/opencv.hpp>

cv::Mat hwc2chw(const cv::Mat &image){
    std::vector<cv::Mat> rgb_images;
    cv::split(image, rgb_images);

    // Stretch one-channel images to vector
    cv::Mat m_flat_r = rgb_images[0].reshape(1,1);
    cv::Mat m_flat_g = rgb_images[1].reshape(1,1);
    cv::Mat m_flat_b = rgb_images[2].reshape(1,1);

    // Now we can rearrange channels if need
    cv::Mat matArray[] = { m_flat_r, m_flat_g, m_flat_b};
    
    cv::Mat flat_image;
    // Concatenate three vectors to one
    cv::hconcat( matArray, 3, flat_image );
    return flat_image;
}

P.S. If input image isn't in RGB format, you can change channel order in matArray creation line.

like image 108
Anton Ganichev Avatar answered Sep 15 '25 03:09

Anton Ganichev