4

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.

Gepard
  • 111
  • 1
  • 9
  • using cv::split and copying the single channels to a single memory space should work? But probably there is a more simple way – Micka May 21 '17 at 08:46

3 Answers3

4

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.

Anton Ganichev
  • 2,184
  • 1
  • 18
  • 17
3

Use cv::dnn::blobFromImage:

cv::Mat bgr_image = cv::imread(imageFileName);

cv::Mat chw_image = cv::dnn::blobFromImage
(
    bgr_image,
    1.0, // scale factor
    cv::Size(), // spatial size for output image
    cv::Scalar(), // mean
    true, // swapRB: BGR to RGB
    false, // crop
    CV_32F // Depth of output blob. Choose CV_32F or CV_8U.
);

const float* data = reinterpret_cast<const float*>(chw_image.data);

int data_length = 1 * 3 * bgr_image.rows * bgr_image.cols;
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
2

You can either iterate over the image manually and copy the values into the right place, or you can use something like cv::extractChannel to copy the channels one by one like so:

#include <opencv2/opencv.hpp>

int main()
{
    //create dummy 3 channel float image
    cv::Mat sourceRGB(cv::Size(100,100),CV_32FC3);
    auto size = sourceRGB.size();
    for (int y = 0; y < size.height; ++y)
    {
        for (int x = 0; x < size.width; ++x)
        {
            float* pxl = sourceRGB.ptr<float>(x, y);
            *pxl = x / 100.0f;
            *(pxl+1) = y / 100.0f;
            *(pxl + 2) = (y / 100.0f) * (x / 100.0f);
        }
    }

    cv::imshow("test", sourceRGB);
    cv::waitKey(0);

    //create single image with all 3 channels one after the other
    cv::Size newsize(size.width,size.height*3);
    cv::Mat destination(newsize,CV_32FC1);

    //copy the channels from the source image to the destination
    for (int i = 0; i < sourceRGB.channels(); ++i)
    {
        cv::extractChannel(
            sourceRGB,
            cv::Mat(
                size.height,
                size.width,
                CV_32FC1,
                &(destination.at<float>(size.height*size.width*i))),
            i);
    }

    cv::imshow("test", destination);
    cv::waitKey(0);
    return 0;
}
PeterT
  • 7,981
  • 1
  • 26
  • 34