我想使用其他地方分配的数据创建一个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()
函数。
谢谢!
答案 0 :(得分:3)
您可以直接使用split
来避免copyTo
和merge
。
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);