rgb与lab互换

来源:互联网 发布:数据挖掘分类常用方法 编辑:程序博客网 时间:2024/04/29 14:01
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

原创粉丝点击