OpenCv create 3 channel Mat from continuous data array

老子叫甜甜 提交于 2020-07-06 11:27:10

问题


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!


回答1:


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);


来源:https://stackoverflow.com/questions/43183931/opencv-create-3-channel-mat-from-continuous-data-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!