Mark Ruzon发来的邮件:

代码如下:


===========================rgb2lab.m1. 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
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
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
13. % them into double before calling this
14.
15. if
16. double(R(:,:,3));
17. double(R(:,:,2));
18. double(R(:,:,1));
19. end
20.
21. if
22. R = R/255;
23. G = G/255;
24. B = B/255;
25. end
26.
27. [M, N] = size(R);
28. s = M*N;
29.
30. % Set a threshold
31. T = 0.008856;
32.
33. RGB = [reshape(R,1,s); reshape(G,1,s); reshape(B,1,s)];
34.
35. % RGB to XYZ
36. MAT = [0.412453 0.357580 0.180423;
37. 0.212671 0.715160 0.072169;
38. 0.019334 0.119193 0.950227];
39. XYZ = MAT * RGB;
40.
41. X = XYZ(1,:) / 0.950456;
42. Y = XYZ(2,:);
43. Z = XYZ(3,:) / 1.088754;
44.
45. XT = X > T;
46. YT = Y > T;
47. ZT = Z > T;
48.
49. fX = XT .* X.^(1/3) + (~XT) .* (7.787 .* X + 16/116);
50.
51. % Compute L
52. Y3 = Y.^(1/3);
53. fY = YT .* Y3 + (~YT) .* (7.787 .* Y + 16/116);
54. L = YT .* (116 * Y3 - 16.0) + (~YT) .* (903.3 * Y);
55.
56. fZ = ZT .* Z.^(1/3) + (~ZT) .* (7.787 .* Z + 16/116);
57.
58. % Compute a and b
59. a = 500 * (fX - fY);
60. b = 200 * (fY - fZ);
61.
62. L = reshape(L, M, N);
63. a = reshape(a, M, N);
64. b = reshape(b, M, N);
65.
66. if
67. L = cat(3,L,a,b);
68. end
69.

==========================================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
4. % and transforms them into RGB. This transform is
5. % Recommendation BT.709 using
6. % and the error in transforming RGB -> Lab -> RGB is
7. % 10^-5. By Mark Ruzon from C code by Yossi Rubner, 23 September 1997.
8. % Updated for
9. % Fixed a bug in
10.
11. if
12. b = L(:,:,3);
13. a = L(:,:,2);
14. L = L(:,:,1);
15. end
16.
17. % Thresholds
18. T1 = 0.008856;
19. T2 = 0.206893;
20.
21. [M, N] = size(L);
22. s = M * N;
23. L = reshape(L, 1, s);
24. a = reshape(a, 1, s);
25. b = reshape(b, 1, s);
26.
27. % Compute Y
28. fY = ((L + 16) / 116) .^ 3;
29. YT = fY > T1;
30. fY = (~YT) .* (L / 903.3) + YT .* fY;
31. Y = fY;
32.
33. % Alter fY slightly for
34. fY = YT .* (fY .^ (1/3)) + (~YT) .* (7.787 .* fY + 16/116);
35.
36. % Compute X
37. fX = a / 500 + fY;
38. XT = fX > T2;
39. X = (XT .* (fX .^ 3) + (~XT) .* ((fX - 16/116) / 7.787));
40.
41. % Compute Z
42. fZ = fY - b / 200;
43. ZT = fZ > T2;
44. Z = (ZT .* (fZ .^ 3) + (~ZT) .* ((fZ - 16/116) / 7.787));
45.
46. X = X * 0.950456;
47. Z = Z * 1.088754;
48.
49. MAT = [ 3.240479 -1.537150 -0.498535;
50. -0.969256 1.875992 0.041556;
51. 0.055648 -0.204043 1.057311];
52.
53. RGB = max(min(MAT * [X; Y; Z], 1), 0);
54.
55. R = reshape(RGB(1,:), M, N) * 255;
56. G = reshape(RGB(2,:), M, N) * 255;
57. B = reshape(RGB(3,:), M, N) * 255;
58.
59. if
60. R = uint8(round(cat(3,R,G,B)));
61. end
62.
63.