8

I need to convert RGB to YCbCr for my final project and I trying to do this way (I'm programming in C):

/* Autor: Vinicius Garcia
 * Data : 09.ago.2011
 * 
 * Função que converte um pixel RGB em YCbCr
 * 
 * param : int R valor do pixel no canal red
 * param : int G valor do pixel no canal green
 * param : int B valor do pixel no canal blue
 * return: int* vetor de inteiros com os valores H, S e V calculados - nesta ordem
 */
int* converter_RGB_para_YCbCr(int R, int G, int B){
    int* YCbCr = (int*) malloc(3 * sizeof(int));

    double delta = 128.0; //Constante necessaria para o calculo da conversão de cor
    double Y  = (0.299 * R + 0.587 * G + 0.114 * B);
    double Cb = ((B - Y) * 0.564 + delta);
    double Cr = ((R - Y) * 0.713 + delta);

    YCbCr[0] = (int) Y;
    YCbCr[1] = (int) Cb;
    YCbCr[2] = (int) Cr;
    return YCbCr;
}

But it doesn't work for me!

I was comparing with cvCvtColor (from OpenCv library) and the results doesn't match:

            R = 88,  G  = 76,   B = 78
cvCvtColor: Y = 80,  Cb = 127, Cr = 134
myfunction: Y = 382, Cb = 132, Cr = 132 (cr and cr are always equal!)

I really need help with this, I trying do this for a long time and I couldn't find any answer for my doubt.


Do you guys think I'm getting the RGB values wrong? I'm doing this way:

uchar B = CV_IMAGE_ELEM(img_RGB, uchar, linha, coluna * 3);
uchar G = CV_IMAGE_ELEM(img_RGB, uchar, linha, coluna * 3 + 1);
uchar R = CV_IMAGE_ELEM(img_RGB, uchar, linha, coluna * 3 + 2);

Then I'm calling my conversion function this way:

converter_RGB_para_YCbCr(R, G, B);

My function expects int R, int G, int B but I'm passing uchar B, uchar G, uchar R. its that ok?

10
  • Hmm, that is Odd; even doing it by hand and using your method, Y = 80 (or rather rounded to 80 when converted to int) Can you inspect your method at the point where you assign Y,Cb and Cr to your YCbCr return value, and post what the values are here ? Commented Aug 17, 2011 at 0:56
  • Also what is this delta for ? I've not seen that used myself. Ill post an answer with the conversion as I'm aware of it... Commented Aug 17, 2011 at 0:57
  • No idea where you get Y = 382, it can never be > 255, assuming that R, G and B can be maximum 255. I get Y = 79.816, using a "pocket" calculator. Commented Aug 17, 2011 at 0:59
  • Same here Rudy, I wonder if it's type conversion ? Commented Aug 17, 2011 at 1:05
  • 3
    I think the bug is in your printing code... (not in the posted code). Commented Aug 17, 2011 at 2:37

2 Answers 2

7
int y  = (int)( 0.299   * R + 0.587   * G + 0.114   * B);
int cb = (int)(-0.16874 * R - 0.33126 * G + 0.50000 * B);
int cr = (int)( 0.50000 * R - 0.41869 * G - 0.08131 * B);
Sign up to request clarification or add additional context in comments.

4 Comments

Hey guys! thanks for your help! the 'delta' I took from cvCvtColor manual opencv.itseez.com/modules/imgproc/doc/… It is used for 8-bit images.
thanks Russ. but I had try this code too but I got the same problem. =(
Cr gives me a negative value (-127.5), shouldn't we be adding 128 to that value?
Hi Dominicbri7; I have seen an implementation that does indeed add 128 to the sum for cb and cr actually.
3

you should use this formula matlab uses it

Y = 0.257R´ + 0.504G´ + 0.098B´ + 16

Cb = -0.148R´ - 0.291G´ + 0.439B´ + 128

Cr = 0.439R´ - 0.368G´ - 0.071B´ + 128

and to rgb

R´ = 1.164(Y - 16) + 1.596(Cr - 128)

G´ = 1.164(Y - 16) - 0.813(Cr - 128) - 0.392(Cb - 128)

B´ = 1.164(Y - 16) + 2.017(Cb - 128)

I have tested them in a software im making they work ok

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.