# How to convert from YCbCr to RGB?

• I have an assignment, it is about converting an Rgb Image to YCbCr, modifying the Y Value and then convert it back to Rgb.
I used the following methods to convert the Image to YCbCr:

``````
void ImageViewer::convert(){
int height = image->height();
int width = image->width();

QRgb color;
struct YCBCR yCbCr;
for (int f1=0; f1<width; f1++) {
for (int f2=0; f2<height; f2++) {
color = image->pixel(f1,f2);
int red = qRed(color);
int blue = qBlue(color);
int green = qGreen(color);
yCbCr= convertToYCBCR(red,green,blue);

image->setPixel(f1, f2, qRgb(yCbCr.Y, yCbCr.CB, yCbCr.CR));

}
}

updateImageDisplay();
renewLogging();

}

struct YCBCR ImageViewer::convertToYCBCR(int red, int green, int blue) {
struct YCBCR yCbCr;

yCbCr.Y = (int)(0.229*red + 0.587*green + 0.114*blue);
yCbCr.CB = (int)(-0.169*red -0.331*green + 0.5*blue )+128;
yCbCr.CR = (int)(int)(0.5* red - 0.419*green - 0.08*blue) +128;

return yCbCr;
}
``````

Now i am trying to implement a method to get the Y, CB, and CR values from the converted Image in order to modify the Y and then convert it back to RGB, but i have no Idea how to get the y, CB, CR values from the CONVERTED IMAGE. Is there an algorithm? Any Ideas?

• but i have no Idea how to get the y, CB, CR values from the CONVERTED IMAGE. Is there an algorithm?

You can get the rgb pixel from an image with QImage::pixel(). This rgb value can be converted to your YCbCr value and back.

• Yeah but not after i have converted the image.

• Why not?

• Hi,

From your code you seem to convert every pixel value, use the reverse formula to change your YCbCr back to RGB. You should have it in the same place as the one for RGB to YCbCr.