如何在cvCvColor之后引用YUV组件?

时间:2014-06-22 08:01:39

标签: c++ opencv color-scheme

我尝试使用cvCvtColor方法将BGR转换为YUV然后获取对每个组件的引用。 源图像(IplImage1)具有以下参数:

  1. depth = 8
  2. nChannels = 3
  3. colorModel = RGB
  4. channelSeq = BGR
  5. width = 1620
  6. 身高= 1220
  7. 转换后转换并获取组件:

    IplImage* yuvImage = cvCreateImage(cvSize(1620, 1220), 8, 3);
    cvCvtColor(IplImage1, yuvImage, CV_BGR2YCrCb);
    yPtr = yuvImage->imageData;
    uPtr = yPtr + height*width;
    vPtr = uPtr + height*width/4;
    

    我有将YUV转换回RGB并保存到文件的方法。当我手动创建YUV组件(我创建蓝色图像)时,它可以工作,当我打开图像时,它真的很蓝。但是,当我使用上面的方法创建YUV组件时,我得到黑色图像。我想也许我错误地引用了YUV组件

    yPtr = yuvImage->imageData;
    uPtr = yPtr + height*width;
    vPtr = uPtr + height*width/4;
    

    可能是什么问题?

2 个答案:

答案 0 :(得分:1)

如果你真的必须使用IplImage(例如遗留代码或C),那么使用cvSplit

IplImage* IplImage1 = something;
IplImage* ycrcbImage = cvCreateImage(cvSize(1620, 1220), 8, 3);
cvCvtColor(IplImage1, ycrcbImage, CV_BGR2YCrCb);

IplImage* yImage  = cvCreateImage(cvSize(1620, 1220), 8, 1);
IplImage* crImage = cvCreateImage(cvSize(1620, 1220), 8, 1);
IplImage* cbImage = cvCreateImage(cvSize(1620, 1220), 8, 1);
cvSplit(ycrcbImage, yImage, crImage , cbImage, 0);

现代方法是避免遗留API并使用Mats:

cv::Mat matImage1(IplImage1);
cv::Mat ycrcb_image;
cv::cvtColor(matImage1, ycrcb_image, CV_BGR2YCrCb);

// Extract the Y, Cr and Cb channels into separate Mats
std::vector<cv::Mat> planes(3);
cv::split(ycrcb_image, planes);
// Now you have the Y image in planes[0],
// the Cr image in planes[1],
// and the Cb image in planes[2]

cv::Mat Y = planes[0]; // if you want

答案 1 :(得分:0)

RGB表示颜色为红色,绿色和蓝色; YCbCr颜色模型将颜色表示为亮度和两个色差信号。在YCbCr中,Y是亮度(亮度),Cb是蓝色减去亮度(B-Y),Cr是红色减去亮度(R-Y)。

以下是使用OpenCV 3.0.0时的相同代码:

import numpy as np
import cv2

#Obtaining and displaying the image

x = 'C:/Users/524316/Desktop/car.jpg'
img = cv2.imread(x, 1)
cv2.imshow("img",img)

#converting to YCrCb color space

YCrCb = cv2.cvtColor(a, cv2.COLOR_BGR2YCrCb)
cv2.imshow("YCrCb",YCrCb)

#splitting the channels individually

Y, Cr, Cb = cv2.split(YCrCb)

cv2.imshow('Y_channel', Y)
cv2.imshow('Cr_channel', Cr)
cv2.imshow('Cb_channel', Cb)

cv2.waitKey(0)
cv2.destroyAllWindows()

原始图片:

enter image description here

YCrCb图片:

enter image description here

Y - 频道:

与灰度图像相同

enter image description here

Cr - 频道:

enter image description here

Cb - 频道:

enter image description here