9

I'd like to create an OpenCV 3-channel Mat using data allocated elsewhere where the pixels of each channel are together, unlike the data for an OpenCV Mat where the data from different channels is interleaved.

Mat outputMat = Mat(dimY, dimX, CV_8UC3, rawData); 
// This works only if rawData interleaves channel data like an OpenCv Mat

Is there a way to create an OpenCV Mat without having to resort to the below solution of splitting channels from a temporary Mat and copying the right channel data to the corresponding location?

void createMat(unsigned char *rawData, unsigned int dimX, unsigned int dimY)
{
    Mat outputMat = Mat(dimY, dimX, CV_8UC3);

    // use outputMat to draw some stuff

    Mat channelR = Mat(dimY, dimX, CV_8UC1, rawData);
    Mat channelG = Mat(dimY, dimX, CV_8UC1, rawData + dimX * dimY);
    Mat channelB = Mat(dimY, dimX, CV_8UC1, rawData + 2 * dimX * dimY);

    std::vector<Mat> channels(3);
    split(outputMat, channels);

    channels[2].copyTo(channelR);
    channels[1].copyTo(channelG);
    channels[0].copyTo(channelB);
}

I need to do this operation frequently, so I was wondering if there is a solution that doesn't involve calling the split() and copyTo() functions each time.

Thanks!

enter image description here

locke14
  • 1,335
  • 3
  • 15
  • 36
  • 1
    How about `merge(channels,outputMat)`? – Quang Hoang Apr 03 '17 at 12:10
  • I already tried that, but it doesn't seem to work. The correct data doesn't go to the right channel. It is still interleaved. – locke14 Apr 03 '17 at 12:40
  • Your question is not clear to me, Do you want to get `RGB` channels in same manner from an OpenCV mat ? – ZdaR Apr 03 '17 at 14:24
  • Not exactly. I wish to create an OpenCV Mat with external data where the RGB channels are not interleaved, but rather each channel is stored one after the other. – locke14 Apr 03 '17 at 14:44
  • Your statement is ambiguous to me. What does "not interleaved" refer to, the channels of the Mat, the channels of the external data, or both? – beaker Apr 03 '17 at 15:27
  • I added an illustration showing how the different channels are defined for a 2x2 Mat in both cases. Hope it's clear now. – locke14 Apr 03 '17 at 16:01

1 Answers1

8

You can avoid split and copyTo by using merge directly.

Mat createMat(unsigned char *rawData, unsigned int dimX, unsigned int dimY)
{
    // No need to allocate outputMat here
    Mat outputMat;

    // Build headers on your raw data
    Mat channelR(dimY, dimX, CV_8UC1, rawData);
    Mat channelG(dimY, dimX, CV_8UC1, rawData + dimX * dimY);
    Mat channelB(dimY, dimX, CV_8UC1, rawData + 2 * dimX * dimY);

    // Invert channels, 
    // don't copy data, just the matrix headers
    std::vector<Mat> channels{ channelB, channelG, channelR };

    // Create the output matrix
    merge(channels, outputMat);

    return outputMat;
}

I tested a few other approaches, but they result to be slower. Just for the records, I thought this would be faster, but the transpose is really heavy:

Mat outputMat(3, dimY*dimX, CV_8UC1, rawData);
Mat tmp = outputMat.t();
outputMat = tmp.reshape(3, dimY);
cvtColor(outputMat, outputMat, COLOR_RGB2BGR);
Miki
  • 40,887
  • 13
  • 123
  • 202
  • `merge(channels, outputMat);` doesn't seem to use the same data as `rawData`. For example, setting `outputMat.at(0, 0) = Vec3b(128, 128, 128);` doesn't reflect the values at the locations `rawData[0]`, `rawData[dimX * dimY]` and `rawData[2 * dimX * dimY]`. I checked, and it looks like `outputMat.ptr(0) != rawData` – locke14 Apr 04 '17 at 09:03
  • 1
    1) the changes to `outputMat` are not reflected back in `rawData`. The data are deep copied while using `merge`. Since you're using a different layout than OpenCV, if you want to use your data you need to deep copy the data. 2) Obviously the data pointed by `outputMat` are different from `rawData`... you need to _interleave_ your original data to use them in a `Mat`. – Miki Apr 04 '17 at 09:11
  • That's what I feared. For my application, I'm using OpenCV to draw some stuff and display in another application which allocates `rawData`. So, seems like whenever I change the content of the `outputMat` I need to do a deep copy for the changes to reflect in `rawData`. If I have a high refresh rate, there's a lot of overhead to manually __interleave__ the data. If I understood it correctly, looks like there is no solution where I could do this just once and the subsequent changes in my OpenCV `outputMat` would reflect automatically in `rawData`. Thanks for the reply. @Miki – locke14 Apr 04 '17 at 09:21
  • 1
    Exactly. Some solutions to overcome the _refresh_ would be to: 1) change the data layout your other application, 2) don't use OpenCV and make your own `Matrix` structure and the needed functions. If you just want to draw stuff, this shouldn't be difficult. 3) Use a library with the same data layout – Miki Apr 04 '17 at 09:27
  • What about YUV420? The split method seems to return just one channel with the initial Mat. – dclaudiud Dec 31 '22 at 21:41