OpenCv从连续数据阵列创建3通道Mat

时间:2017-04-03 11:52:05

标签: c++ opencv image-processing mat opencv-mat

我想使用其他地方分配的数据创建一个OpenCV 3通道Mat,其中每个通道的像素在一起,这与OpenCV Mat的数据不同,其中来自不同通道的数据是交错的。

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

有没有办法创建OpenCV Mat而不必采用以下解决方案从临时Mat分割通道并将正确的通道数据复制到相应的位置?

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

我需要经常进行此操作,所以我想知道是否有一个解决方案并不涉及每次调用split()copyTo()函数。

谢谢!

enter image description here

1 个答案:

答案 0 :(得分:3)

您可以直接使用split来避免copyTomerge

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

我测试了其他一些方法,但结果却变慢了。仅仅为了记录,我认为这会更快,但转置真的很重:

Mat outputMat(3, dimY*dimX, CV_8UC1, rawData);
Mat tmp = outputMat.t();
outputMat = tmp.reshape(3, dimY);
cvtColor(outputMat, outputMat, COLOR_RGB2BGR);