rgb与lab互换

Mark Ruzon发来的邮件:
代码如下:
===========================rgb2lab.m
  1. function [L,a,b] = RGB2Lab(R,G,B)
  2. % function [L, a, b] = RGB2Lab(R, G, B)
  3. % RGB2Lab takes matrices corresponding to Red, Green, and Blue, and 
  4. % transforms them into CIELab.  This transform is based on ITU-R 
  5. % Recommendation  BT.709 using the D65 white point reference.
  6. % The error in transforming RGB -> Lab -> RGB is approximately
  7. % 10^-5.  RGB values can be either between 0 and 1 or between 0 and 255.  
  8. % By Mark Ruzon from C code by Yossi Rubner, 23 September 1997.
  9. % Updated for MATLAB 5 28 January 1998.
  10. %
  11. % If your image is loaded into uint8 format as an M x N x 3 tensor, you 
  12. % can pass it in as one argument.  If you break it into 3 pieces, convert
  13. % them into double before calling this function.
  14. if (nargin == 1)
  15.   B = double(R(:,:,3));
  16.   G = double(R(:,:,2));
  17.   R = double(R(:,:,1));
  18. end
  19. if ((max(max(R)) > 1.0) | (max(max(G)) > 1.0) | (max(max(B)) > 1.0))
  20.   R = R/255;
  21.   G = G/255;
  22.   B = B/255;
  23. end
  24. [M, N] = size(R);
  25. s = M*N;
  26. % Set a threshold
  27. T = 0.008856;
  28. RGB = [reshape(R,1,s); reshape(G,1,s); reshape(B,1,s)];
  29. % RGB to XYZ
  30. MAT = [0.412453 0.357580 0.180423;
  31.        0.212671 0.715160 0.072169;
  32.        0.019334 0.119193 0.950227];
  33. XYZ = MAT * RGB;
  34. X = XYZ(1,:) / 0.950456;
  35. Y = XYZ(2,:);
  36. Z = XYZ(3,:) / 1.088754;
  37. XT = X > T;
  38. YT = Y > T;
  39. ZT = Z > T;
  40. fX = XT .* X.^(1/3) + (~XT) .* (7.787 .* X + 16/116);
  41. % Compute L
  42. Y3 = Y.^(1/3); 
  43. fY = YT .* Y3 + (~YT) .* (7.787 .* Y + 16/116);
  44. L  = YT .* (116 * Y3 - 16.0) + (~YT) .* (903.3 * Y);
  45. fZ = ZT .* Z.^(1/3) + (~ZT) .* (7.787 .* Z + 16/116);
  46. % Compute a and b
  47. a = 500 * (fX - fY);
  48. b = 200 * (fY - fZ);
  49. L = reshape(L, M, N);
  50. a = reshape(a, M, N);
  51. b = reshape(b, M, N);
  52. if ((nargout == 1) | (nargout == 0))
  53.   L = cat(3,L,a,b);
  54. end
==========================================lab2rgb.m
  1. function [R, G, B] = Lab2RGB(L, a, b)
  2. % function [R, G, B] = Lab2RGB(L, a, b)
  3. % Lab2RGB takes matrices corresponding to L, a, and b in CIELab space
  4. % and transforms them into RGB.  This transform is based on ITU-R 
  5. % Recommendation  BT.709 using the D65 white point reference.
  6. % and the error in transforming RGB -> Lab -> RGB is approximately
  7. % 10^-5.  By Mark Ruzon from C code by Yossi Rubner, 23 September 1997.
  8. % Updated for MATLAB 5 28 January 1998.
  9. % Fixed a bug in conversion back to uint8 9 September 1999.
  10. if (nargin == 1)
  11.   b = L(:,:,3);
  12.   a = L(:,:,2);
  13.   L = L(:,:,1);
  14. end
  15. % Thresholds
  16. T1 = 0.008856;
  17. T2 = 0.206893;
  18. [M, N] = size(L);
  19. s = M * N;
  20. L = reshape(L, 1, s);
  21. a = reshape(a, 1, s);
  22. b = reshape(b, 1, s);
  23. % Compute Y
  24. fY = ((L + 16) / 116) .^ 3;
  25. YT = fY > T1;
  26. fY = (~YT) .* (L / 903.3) + YT .* fY;
  27. Y = fY;
  28. % Alter fY slightly for further calculations
  29. fY = YT .* (fY .^ (1/3)) + (~YT) .* (7.787 .* fY + 16/116);
  30. % Compute X
  31. fX = a / 500 + fY;
  32. XT = fX > T2;
  33. X = (XT .* (fX .^ 3) + (~XT) .* ((fX - 16/116) / 7.787));
  34. % Compute Z
  35. fZ = fY - b / 200;
  36. ZT = fZ > T2;
  37. Z = (ZT .* (fZ .^ 3) + (~ZT) .* ((fZ - 16/116) / 7.787));
  38. X = X * 0.950456;
  39. Z = Z * 1.088754;
  40. MAT = [ 3.240479 -1.537150 -0.498535;
  41.        -0.969256  1.875992  0.041556;
  42.         0.055648 -0.204043  1.057311];
  43. RGB = max(min(MAT * [X; Y; Z], 1), 0);
  44. R = reshape(RGB(1,:), M, N) * 255;
  45. G = reshape(RGB(2,:), M, N) * 255;
  46. B = reshape(RGB(3,:), M, N) * 255; 
  47. if ((nargout == 1) | (nargout == 0))
  48.   R = uint8(round(cat(3,R,G,B)));
  49. end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
RGBLab是两种不同的颜色空间,它们之间的转换需要使用不同的公式。 RGBLab的公式如下: 1. 对于RGB值进行标准化,即把RGB值除以255,得到R、G、B的取值范围在0到1之间的值。 2. 对于标准化后的RGB值,进行gamma校正,即根据设备的不同进行调整,常用的是sRGB的gamma值2.4。 3. 根据下面的公式计算出X、Y、Z值: X = 0.412453*R + 0.357580*G + 0.180423*B Y = 0.212671*R + 0.715160*G + 0.072169*B Z = 0.019334*R + 0.119193*G + 0.950227*B 4. 计算出标准化的XYZ值,即将X、Y、Z分别除以参考白点的值,常用的是D50参考白点,其XYZ值为(0.9642, 1.0000, 0.8249)。 5. 对标准化的XYZ值进行非线性变换,转换为Lab值: L = 116 * f(Y/Yn) - 16 a = 500 * [f(X/Xn) - f(Y/Yn)] b = 200 * [f(Y/Yn) - f(Z/Zn)] 其中,f(t)是一个非线性函数,当t > 0.008856时,f(t) = t^(1/3),否则f(t) = 7.787*t + 16/116。 RGBLab的公式就是这样了。 LabRGB的公式如下: 1. 对于Lab值,使用下面的公式计算出对应的XYZ值: Y = (L + 16) / 116 X = a / 500 + Y Z = Y - b / 200 2. 对于标准化的XYZ值,使用参考白点的XYZ值进行反标准化。 3. 对于反标准化后的XYZ值,使用下面的公式计算出对应的RGB值: R = 3.240479*X - 1.537150*Y - 0.498535*Z G = -0.969256*X + 1.875992*Y + 0.041556*Z B = 0.055648*X - 0.204043*Y + 1.057311*Z 4. 对于计算出的RGB值,进行gamma校正,即将其进行幂次调整,常用的是sRGB的gamma值的倒数2.4。 5. 最后,将计算出的RGB值乘以255,得到最终的RGB值。 这就是LabRGB的公式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

superdont

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值