BGR到YCrCb导致OpenCV中的灰度图像

时间:2015-04-12 06:57:34

标签: c++ opencv image-processing

我正在将BGR图像转换为YCrCb并将其保存在磁盘中;但是,输出图像是灰色的,如何将图像保存到带有颜色的光盘?

以下是我的代码的示例:

Mat img = imread("...");
Mat img2;
cvtColor(img, img2, CV_BGR2YCrCb);

vector<Mat> planes;
split(imgColorConverted, planes);

imwrite(".../planes1.jpg", planes[0]);
imwrite(".../planes2.jpg", planes[1]);
imwrite(".../planes3.jpg", planes[2]);

当我打开保存的图像时,它是灰色的。是否可以将图像保存为我在维基百科(Wikipedia - YCbCr)上找到的以下示例?:

谢谢!

Image extracted from Wikipedia

1 个答案:

答案 0 :(得分:3)

在这种情况下,您可以保存1个频道的图片,因此它是灰色的。事实上,你应该做的是:

Mat img = imread("...");
Mat img2;
cvtColor(img, img2, CV_BGR2YCrCb);

vector<Mat> planes;
split(imgColorConverted, planes);

Mat* planes2 = new Mat[3];
Mat emptyMat(planes[0].size(), planes[0].type(), Scalar(0));
Mat Cr, Cb;
imwrite(".../planes1.jpg", planes[0]);
// Set Cr channel on R channel
planes2[0] = emptyMat;
planes2[1] = emptyMat;
planes2[2] = planes[1];
merge(planes2, 3, Cr);
imwrite(".../planes2.jpg", Cr);
// Set Cb channel on B channel
planes2[0] = planes[2];
planes2[1] = emptyMat;
planes2[2] = emptyMat;
merge(planes2, 3, Cb);
imwrite(".../planes3.jpg", Cb);