Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm trying to convert BGR to YUV with
cvCvtColor
method AND then get reference to each component.
The source image (
IplImage1
) has following parameters:
depth = 8
nChannels = 3
colorModel = RGB
channelSeq = BGR
width = 1620
height = 1220
Convert and get the components after conversion:
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;
I have method that converts the YUV back to RGB and saves to file. When I create the YUV components manually (I create blue image) it works and when I open the image it's really blue. But, when I create YUV components using the method above I get black image. I think that maybe I get reference to YUV components wrongly
yPtr = yuvImage->imageData;
uPtr = yPtr + height*width;
vPtr = uPtr + height*width/4;
What could be the problem?
–
–
–
If you really must use IplImage (e.g. in legacy code, or C) then use 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);
The modern approach would be to avoid the legacy API and use 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
–
–
–
–
While RGB represents color as red, green and blue; the YCbCr color model represents color as brightness and two color difference signals. In YCbCr, the Y is the brightness (luma), Cb is blue minus luma (B-Y) and Cr is red minus luma (R-Y).
Here is the code for the same in case you are using 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()
Original image:
YCrCb image :
Y - Channel :
It is the same as grayscale image
Cr - channel :
Cb - channel :
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.