我需要将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,coluna * 3 + 1); uchar R = CV_IMAGE_ELEM(img_RGB,coluna * 3 + 2);
converter_RGB_para_YCbCr(R,G,B);
我的函数需要int R,int B但是我传递了uchar B,uchar G,uchar R.它还好吗?
解决方法
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);