If you are curious about the theory behind the HSI and RGB color spaces, you can read this articles:
1.RGB to YUV Conversion
This function will create a YUV model from a RGB model. The algorithm behind the conversion is described by the formulas below:
RGB to YUV algorithm |
R,G,B - the components of the RGB model (red, green, blue)
Y,U,V - the components of YIQ model (luma, chrominance components)
The components of the RGB model (r,g,b) and luma should have values in the range [0,1], while the U component should have values in the range [-0.436 ,0.436] and the V component should have values in the range [-0.615, 0.615].
The prototype of the function is:
/* Description: * Creates a YuvColor structure from RGB components * Parameters: * r,g,b - the components of an RGB model expressed * as real numbers * Returns: * A pointer to the YuvColor is the parameters are * correct. Otherwise returns NULL. */ YuvColor* Yuv_CreateFromRgbF(double r, double g, double f);
The function can be implemented as:
YuvColor* Yuv_CreateFromRgbF(double r, double g, double b) { YuvColor* color = NULL; if (RgbF_IsValid(r, g, b) == true) { color = Yuv_Create(0.0, 0.0, 0.0); color->Y = 0.299 * r + 0.587 * b + 0.114 * b; color->U = 0.492 * (b - color->Y); color->V = 0.877 * (r - color->Y); } return color; }
2.YUV to RGB Conversion
This function will create a RGB model from a YUV model. The algorithm behind the conversion is described by the formulas below:
YUV to RGB algorithm |
The meaning of the variables:
R,G,B - the components of the RGB model (red, green, blue)
Y,U,V - the components of YIQ model (luma, chrominance components)
R,G,B - the components of the RGB model (red, green, blue)
Y,U,V - the components of YIQ model (luma, chrominance components)
The components of the RGB model (r,g,b) and luma should have values in the range [0,1], while the U component should have values in the range [-0.436 ,0.436] and the V component should have values in the range [-0.615, 0.615].
The prototype of the function is:
/* * Description: * Creates a RgbFColor structure from YUV components * Parameters: * y,u,v - the components of an YUV model expressed * as real numbers * Returns: * A pointer to the RgbFColor is the parameters are * correct. Otherwise returns NULL. */ RgbFColor* RgbF_CreateFromYuv(double y, double u, double v);
The function can be implemented as:
RgbFColor* RgbF_CreateFromYuv(double y, double u, double v) { RgbFColor *color = NULL; if (Yiq_IsValid(y, u, v) == true) { color = RgbF_Create(0.0, 0.0, 0.0); color->R = y + 1.140 * v; color->G = y - 0.395 * u - 0.581 * v; color->B = y + 2.032 * u; } return color; }
3.Exemplu
#include<stdio.h> #include"colorspace.h" int main(int argc, char** argv) { YuvColor* yuv = NULL; RgbFColor* rgbF = NULL; RgbIColor* rgbI = NULL; /*YUV to RGB*/ yuv = Yuv_Create(0.4, 0.1, -0.11); rgbF = RgbF_CreateFromYuv(yuv->Y, yuv->U, yuv->V); rgbI = RgbI_CreateFromRealForm(rgbF->R, rgbF->G, rgbF->B); printf("\nYIQ : %f %f %f", yuv->Y, yuv->U, yuv->V); printf("\nRGBf : %f %f %f", rgbF->R, rgbF->G, rgbF->B); printf("\nRGBi : %d %d %d", rgbI->R, rgbI->G, rgbI->B); /*Frees the resources*/ free(yuv); free(rgbF); free(rgbI); /*RGB to YUV*/ rgbI = RgbI_Create(108U, 198U, 78U); rgbF = RgbF_CreateFromIntegerForm(rgbI->R, rgbI->G, rgbI->B); yuv = Yuv_CreateFromRgbF(rgbF->R, rgbF->G, rgbF->B); printf("\nYIQ : %f %f %f", yuv->Y, yuv->U, yuv->V); printf("\nRGBf : %f %f %f", rgbF->R, rgbF->G, rgbF->B); printf("\nRGBi : %d %d %d", rgbI->R, rgbI->G, rgbI->B); return 0; } /* YIQ : 0.400000 0.100000 -0.110000 RGBf : 0.274600 0.424410 0.603200 RGBi : 70 108 154 YIQ : 0.341059 -0.017307 0.072327 RGBf : 0.423529 0.776471 0.305882 RGBi : 108 198 78 */
No comments:
Post a Comment
Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!