rgb与lab互换
来源:互联网 发布:数据挖掘分类常用方法 编辑:程序博客网 时间:2024/04/29 14:01
Mark Ruzon发来的邮件:
代码如下:
===========================rgb2lab.m
==========================================lab2rgb.m
代码如下:
===========================rgb2lab.m
- function [L,a,b] = RGB2Lab(R,G,B)
- % function [L, a, b] = RGB2Lab(R, G, B)
- % RGB2Lab takes matrices corresponding to Red, Green, and Blue, and
- % transforms them into CIELab. This transform is based on ITU-R
- % Recommendation BT.709 using the D65 white point reference.
- % The error in transforming RGB -> Lab -> RGB is approximately
- % 10^-5. RGB values can be either between 0 and 1 or between 0 and 255.
- % By Mark Ruzon from C code by Yossi Rubner, 23 September 1997.
- % Updated for MATLAB 5 28 January 1998.
- %
- % If your image is loaded into uint8 format as an M x N x 3 tensor, you
- % can pass it in as one argument. If you break it into 3 pieces, convert
- % them into double before calling this function.
- if (nargin == 1)
- B = double(R(:,:,3));
- G = double(R(:,:,2));
- R = double(R(:,:,1));
- end
- if ((max(max(R)) > 1.0) | (max(max(G)) > 1.0) | (max(max(B)) > 1.0))
- R = R/255;
- G = G/255;
- B = B/255;
- end
- [M, N] = size(R);
- s = M*N;
- % Set a threshold
- T = 0.008856;
- RGB = [reshape(R,1,s); reshape(G,1,s); reshape(B,1,s)];
- % RGB to XYZ
- MAT = [0.412453 0.357580 0.180423;
- 0.212671 0.715160 0.072169;
- 0.019334 0.119193 0.950227];
- XYZ = MAT * RGB;
- X = XYZ(1,:) / 0.950456;
- Y = XYZ(2,:);
- Z = XYZ(3,:) / 1.088754;
- XT = X > T;
- YT = Y > T;
- ZT = Z > T;
- fX = XT .* X.^(1/3) + (~XT) .* (7.787 .* X + 16/116);
- % Compute L
- Y3 = Y.^(1/3);
- fY = YT .* Y3 + (~YT) .* (7.787 .* Y + 16/116);
- L = YT .* (116 * Y3 - 16.0) + (~YT) .* (903.3 * Y);
- fZ = ZT .* Z.^(1/3) + (~ZT) .* (7.787 .* Z + 16/116);
- % Compute a and b
- a = 500 * (fX - fY);
- b = 200 * (fY - fZ);
- L = reshape(L, M, N);
- a = reshape(a, M, N);
- b = reshape(b, M, N);
- if ((nargout == 1) | (nargout == 0))
- L = cat(3,L,a,b);
- end
- function [R, G, B] = Lab2RGB(L, a, b)
- % function [R, G, B] = Lab2RGB(L, a, b)
- % Lab2RGB takes matrices corresponding to L, a, and b in CIELab space
- % and transforms them into RGB. This transform is based on ITU-R
- % Recommendation BT.709 using the D65 white point reference.
- % and the error in transforming RGB -> Lab -> RGB is approximately
- % 10^-5. By Mark Ruzon from C code by Yossi Rubner, 23 September 1997.
- % Updated for MATLAB 5 28 January 1998.
- % Fixed a bug in conversion back to uint8 9 September 1999.
- if (nargin == 1)
- b = L(:,:,3);
- a = L(:,:,2);
- L = L(:,:,1);
- end
- % Thresholds
- T1 = 0.008856;
- T2 = 0.206893;
- [M, N] = size(L);
- s = M * N;
- L = reshape(L, 1, s);
- a = reshape(a, 1, s);
- b = reshape(b, 1, s);
- % Compute Y
- fY = ((L + 16) / 116) .^ 3;
- YT = fY > T1;
- fY = (~YT) .* (L / 903.3) + YT .* fY;
- Y = fY;
- % Alter fY slightly for further calculations
- fY = YT .* (fY .^ (1/3)) + (~YT) .* (7.787 .* fY + 16/116);
- % Compute X
- fX = a / 500 + fY;
- XT = fX > T2;
- X = (XT .* (fX .^ 3) + (~XT) .* ((fX - 16/116) / 7.787));
- % Compute Z
- fZ = fY - b / 200;
- ZT = fZ > T2;
- Z = (ZT .* (fZ .^ 3) + (~ZT) .* ((fZ - 16/116) / 7.787));
- X = X * 0.950456;
- Z = Z * 1.088754;
- MAT = [ 3.240479 -1.537150 -0.498535;
- -0.969256 1.875992 0.041556;
- 0.055648 -0.204043 1.057311];
- RGB = max(min(MAT * [X; Y; Z], 1), 0);
- R = reshape(RGB(1,:), M, N) * 255;
- G = reshape(RGB(2,:), M, N) * 255;
- B = reshape(RGB(3,:), M, N) * 255;
- if ((nargout == 1) | (nargout == 0))
- R = uint8(round(cat(3,R,G,B)));
- end
- rgb与lab互换
- RGB与LAB优化互转
- RGB与Lab颜色空间互相转换
- 【Computer vision】OPENCV RGB与BGR互换
- photoshop中RGB与LAB空间的相互转换
- 颜色表示:HSB,Lab,RGB,CMYK与十六进制表示法
- RGB与Lab色彩空间相互转换c++代码
- RGB转为Lab空间
- RGB to LAB converter
- rgb, cmyk and lab
- 【MATLAB】RGB,HSV,LAB
- RGB转LAB代码
- RGB转为Lab空间
- RGB CMYK HSV LAB
- RGB转LAB
- MATLAB自定义函数:RGB与HSI颜色空间互换
- RGB与HSI颜色空间互换函数(matlab)
- Python实现RGB与HSI颜色空间的互换
- 指向数组的指针
- 我的163博客
- 嵌入式系统开发的模式与流程
- 继承与包含在程序设计中的使用
- Lucene Analyser包分析及自己写Analyser方法
- rgb与lab互换
- 程序如何连接本地Oracle 数据库?
- Linux下select调用的过程转
- c#.net常用函数列表
- 中国移动费笑话
- 一次性照相机
- Delphi与Word之间的融合技术(下)zt
- windows 2003中iis 无法运行asp页面的解决方案
- 教你怎样在MySQL中提高全文搜索效率