0

I'm trying to reduce the runtime of a routine that converts an RGB image to a YCbCr image. My code looks like this:

cv::Mat input(BGR->m_height, BGR->m_width, CV_8UC3, BGR->m_imageData);
cv::Mat output(BGR->m_height, BGR->m_width, CV_8UC3);

cv::cvtColor(input, output, CV_BGR2YCrCb);

cv::Mat outputArr[3];
outputArr[0] = cv::Mat(BGR->m_height, BGR->m_width, CV_8UC1, Y->m_imageData);
outputArr[1] = cv::Mat(BGR->m_height, BGR->m_width, CV_8UC1, Cr->m_imageData);
outputArr[2] = cv::Mat(BGR->m_height, BGR->m_width, CV_8UC1, Cb->m_imageData);  

split(output,outputArr);

But, this code is slow because there is a redundant split operation which copies the interleaved RGB image into the separate channel images. Is there a way to make the cvtColor function create an output that is already split into channel images? I tried to use constructors of the _OutputArray class that accepts a vector or array of matrices as an input, but it didn't work.

4

3 回答 3

0

There is no calling option for cv::cvtColor, that gives it result as three seperate cv::Mats (one per channel).

dst – output image of the same size and depth as src.

source: http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor

You have to copy the pixels from the result (as you are already doing) or write such a conversion function yourself.

于 2012-12-10T22:04:51.733 回答
0

使用拆分。这会将图像分成 3 个不同的通道或数组。

现在将它们转换回UIImage是我遇到麻烦的地方。我得到三个灰度图像,每个数组中一个。我确信它们是 cvMat 格式的正确通道,但是当我将它们转换为灰度时,UIImage它们是灰度的,但每个图像中的灰度值不同。如果您可以使用imreadimshow那么它应该在拆分后为您显示图像。我的问题是尝试使用 ios.h 方法,我相信它会重新组装数组,而不是传输单个数组。这是我的代码,它使用分段控件来选择要显示的图层或数组。就像我说的,我得到了 3 张灰度图像,但它们的值完全不同。我需要保留一层并放弃其余的。仍在研究它的那部分。

UIImageToMat(_img, cvImage);
cv::cvtColor(cvImage, RYB, CV_RGB2BGRA);
split(RYB, layers);
if (_segmentedRGBControl.selectedSegmentIndex == 0) {
   // cv::cvtColor(layers[0], RYB, CV_8UC1);
    RYB = layers[0];
    _imageProcessView.image = MatToUIImage(RYB);
}
if (_segmentedRGBControl.selectedSegmentIndex == 1) {
    RYB = (layers[1]);
    _imageProcessView.image = MatToUIImage(RYB);
}
if (_segmentedRGBControl.selectedSegmentIndex == 2) {
    RYB = (layers[2]);
    _imageProcessView.image = MatToUIImage(RYB);
}
}
于 2013-12-28T16:37:58.880 回答
0

您确定复制图像数据是限制步骤吗?
你是如何生产 Y 的?Cr / Cb cv::mats?
你可以重写这个函数来将结果写入三个单独的图像吗?

于 2012-12-10T17:02:28.777 回答