1. 程式人生 > >用c++實現顏色空間rgb,grey,luv和lab的互轉

用c++實現顏色空間rgb,grey,luv和lab的互轉

1 rgb轉grey,rgb轉luv,rgb轉lab

1. 1 rgb轉grey

 

 
  1. void RgbToGrey(unsigned char *rgb, double *grey)

  2. {

  3. double R = ((double) rgb[0]) / (double)255.0;

  4. double G = ((double) rgb[1]) / (double)255.0;

  5. double B = ((double) rgb[2]) / (double)255.0;

  6. double y = (double)0.212671 * R + (double)0.715160 * G + (double)0.072169 * B;

  7.  
  8.  
  9. if (y > (double)0.008856) grey[0] = (double)116.0 * pow(y, (double)1.0 / (double)3.0) - (double)16.0;

  10. else grey[0] = (double)903.3 * y;

  11. }

  12.  

 

1. 2  rgb轉luv

 

 

 
  1. void RgbToLuv(unsigned char *rgb, double *luv)

  2. {

  3. double L, u, v;

  4.  
  5.  
  6. double R = ((double) rgb[0]) / (double)255.0;

  7. double G = ((double) rgb[1]) / (double)255.0;

  8. double B = ((double) rgb[2]) / (double)255.0;

  9.  
  10.  
  11. double x = (double)0.412453 * R + (double)0.357580 * G + (double)0.180423 * B;

  12. double y = (double)0.212671 * R + (double)0.715160 * G + (double)0.072169 * B;

  13. double z = (double)0.019334 * R + (double)0.119193 * G + (double)0.950227 * B;

  14.  
  15.  
  16. if (y > 0.008856) L = (double)116.0 * pow(y, (double)1.0 / (double)3.0) - (double)16.0;

  17. else L = (double)903.3 * y;

  18.  
  19.  
  20. double sum = x + 15 * y + 3 * z;

  21.  
  22.  
  23. if(sum != 0) u = 4 * x / sum, v = 9 * y / sum;

  24. else u = 4.0, v = (double)9.0 / (double)15.0;

  25.  
  26.  
  27. luv[0] = L;

  28. luv[1] = 13 * L * (u - (double)0.19784977571475);

  29. luv[2] = 13 * L * (v - (double)0.46834507665248);

  30. }

  31.  

 

1. 3 rgb轉grey

 

 

 
  1. void RgbToLab(unsigned char *rgb, double *lab)

  2. {

  3. double R = ((double) rgb[0]) / (double)255.0;

  4. double G = ((double) rgb[1]) / (double)255.0;

  5. double B = ((double) rgb[2]) / (double)255.0;

  6.  
  7.  
  8. double X = 0.412453 * R + 0.357580 * G + 0.180423 * B;

  9. double Y = 0.212671 * R + 0.715160 * G + 0.072169 * B;

  10. double Z = 0.019334 * R + 0.119193 * G + 0.950227 * B;

  11.  
  12.  
  13. double xr = X / 0.950456, yr = Y / 1.000, zr = Z / 1.088854;

  14. if(yr > 0.008856) lab[0] = 116.0 * pow(yr, 1.0 / 3.0) - 16.0;

  15. else lab[0] = 903.3 * yr;

  16.  
  17.  
  18. double fxr, fyr, fzr;

  19.  
  20.  
  21. if(xr > 0.008856) fxr = pow(xr, 1.0 / 3.0);

  22. else fxr = 7.787 * xr + 16.0 / 116.0;

  23.  
  24.  
  25. if(yr > 0.008856) fyr = pow(yr, 1.0 / 3.0);

  26. else fyr = 7.787 * yr + 16.0 / 116.0;

  27.  
  28.  
  29. if(zr > 0.008856) fzr = pow(zr, 1.0 / 3.0);

  30. else fzr = 7.787 * zr + 16.0 / 116.0;

  31.  
  32.  
  33. lab[1] = 500.0 * (fxr - fyr);

  34. lab[2] = 200.0 * (fyr - fzr);

  35. }

  36.  

2 grey轉rgb,grey轉luv,grey轉lab

 

2.1 grey轉rgb

 

 
  1. void GreyToRgb(double *grey, unsigned char *rgb)

  2. {

  3. if(grey[0] < (double)0.1) rgb[0] = rgb[1] = rgb[2] = 0;

  4. else

  5. {

  6. double y;

  7. if(grey[0] <= (double)7.9996) y = 255 * grey[0] / (double)903.3;

  8. else y = (grey[0] + (double)16.0) / (double)116.0, y = 255 * y * y * y;

  9.  
  10.  
  11. rgb[0] = (unsigned char)((y < 0) ? 0 : ((y > 255) ? 255 : y));

  12. rgb[2] = rgb[1] = rgb[0];

  13. }

  14. }

  15.  


 

2.2 grey轉luv

 

 
  1. void GreyToLuv(double *grey, double *luv)

  2. {

  3. luv[0] = grey[0], luv[1] = luv[2] = 0;

  4. }

  5.  

 

2.3 grey轉lab

 

 

 
  1. void GreyToLab(double *grey, double *lab)

  2. {

  3. lab[0] = grey[0], lab[1] = lab[2] = 0;

  4. }

  5.  

 

3 luv轉rgb,luv轉grey    

 

3.1  luv轉rgb

 

 
  1. void LuvToRgb(double *luv, unsigned char *rgb)

  2. {

  3. if(luv[0] < 0.1) rgb[0] = rgb[1] = rgb[2] = 0;

  4. else

  5. {

  6. double x, y, z, u, v;

  7.  
  8.  
  9. if(luv[0] <= (double)7.9996) y = luv[0] / (double)903.3;

  10. else y = (luv[0] + (double)16.0) / (double)116.0, y = y * y * y;

  11.  
  12.  
  13. u = luv[1] / (13 * luv[0]) + (double)0.19784977571475;

  14. v = luv[2] / (13 * luv[0]) + (double)0.46834507665248;

  15. x = 9 * u * y / (4 * v);

  16. z = (12 - 3 * u - 20 * v) * y / (4 * v);

  17.  
  18.  
  19. double R = (double)3.240479 * x - (double)1.537150 * y - (double)0.498535 * z;

  20. double G = (double)-0.969256 * x + (double)1.875992 * y + (double)0.041556 * z;

  21. double B = (double)0.055648 * x - (double)0.204043 * y + (double)1.057311 * z;

  22. R *= 255, G *= 255, B *= 255;

  23.  
  24.  
  25. rgb[0] = (unsigned char)((R < 0) ? 0 : ((R > 255) ? 255 : R));

  26. rgb[1] = (unsigned char)((G < 0) ? 0 : ((G > 255) ? 255 : G));

  27. rgb[2] = (unsigned char)((B < 0) ? 0 : ((B > 255) ? 255 : B));

  28. }

  29. }

 

3.2 luv轉grey   

 

 

 
  1. void LuvToGrey(double *luv, double *grey)

  2. {

  3. grey[0] = luv[0];

  4. }

  5.  

 

 4  lab轉rgb,lab轉grey

 

4.1 lab轉rgb

 

 
  1. void LabToRgb(double *lab, unsigned char *rgb)

  2. {

  3. double X, Y, Z;

  4. double P = (lab[0] + 16.0) / 116.0;

  5.  
  6.  
  7. if(lab[0] > 7.9996) Y = 1.000 * P * P * P;

  8. else Y = 1.000 * lab[0] / 903.3;

  9.  
  10.  
  11. double yr = Y / 1.000, fy;

  12. if(yr > 0.008856) fy = pow(yr, 1.0 / 3.0);

  13. else fy = 7.787 * yr + 16.0 / 116.0;

  14.  
  15.  
  16. double fx = lab[1] / 500.0 + fy, fz = fy - lab[2] / 200.0;

  17.  
  18.  
  19. if(fx > 0.2069) X = 0.950456 * fx * fx * fx;

  20. else X = 0.950456 / 7.787 * (fx - 16.0 / 116.0);

  21.  
  22.  
  23. if(fz > 0.2069) Z = 1.088854 * fz * fz * fz;

  24. else Z = 1.088854 / 7.787 * (fz - 16.0 / 116.0);

  25.  
  26.  
  27. double R = 3.240479 * X - 1.537150 * Y - 0.498535 * Z;

  28. double G = -0.969256 * X + 1.875992 * Y + 0.041556 * Z;

  29. double B = 0.055648 * X - 0.204043 * Y + 1.057311 * Z;

  30. R *= 255, G *= 255, B *= 255;

  31.  
  32.  
  33. rgb[0] = (unsigned char)((R < 0) ? 0 : ((R > 255) ? 255 : R));

  34. rgb[1] = (unsigned char)((G < 0) ? 0 : ((G > 255) ? 255 : G));

  35. rgb[2] = (unsigned char)((B < 0) ? 0 : ((B > 255) ? 255 : B));

  36. }

  37.  

 

4.2 lab轉grey

 

 

 
  1. void LabToGrey(double *lab, double *grey)

  2. {

  3. grey[0] = lab[0];

  4. }

 

5 參考文獻

https://blog.csdn.net/google0802/article/details/17633111

[1]http://blog.sina.com.cn/s/blog_5309cefc0101cdeg.html  作者:RescuEe  題目:用c++實現顏色空間rgb,grey,luv和lab的互轉

[2]http://blog.sina.com.cn/s/blog_46e3af5b0100u7wt.html  作者:孫志海    題目:RGB與Lab顏色空間轉換的程式實現及效果(VC++6.0)