将 RGB 转换为 YCbCr - C 代码

发布于 2024-11-30 02:32:08 字数 1516 浏览 1 评论 0原文

我需要将 RGB 转换为 YCbCr 来完成我的最终项目,我尝试这样做(我用 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;
}

但它对我不起作用!

我正在与 cvCvtColor (来自 OpenCv 库)进行比较,结果不匹配:

            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!)

我真的需要这方面的帮助,我尝试这样做很长时间,但找不到任何我的疑问的答案。


你们认为我的 RGB 值有误吗?我这样做:

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);

然后我这样调用我的转换函数:

converter_RGB_para_YCbCr(R, G, B);

我的函数需要 int R、int G、int B,但我传递了 uchar B、uchar G、uchar R。这样可以吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一直在等你来 2024-12-07 02:32:08
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);
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);
清旖 2024-12-07 02:32:08

你应该使用这个公式 matlab使用它

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

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

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

并转换为 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)

我已经在软件中测试了它们,让它们工作正常

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文