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. %RGB2LabtakesmatricescorrespondingtoRed,Green,andBlue,and
  4. %transformsthemintoCIELab.ThistransformisbasedonITU-R
  5. %RecommendationBT.709usingtheD65whitepointreference.
  6. %TheerrorintransformingRGB->Lab->RGBisapproximately
  7. %10^-5.RGBvaluescanbeeitherbetween0and1orbetween0and255.
  8. %ByMarkRuzonfromCcodebyYossiRubner,23September1997.
  9. %UpdatedforMATLAB528January1998.
  10. %
  11. %Ifyourimageisloadedintouint8formatasanMxNx3tensor,you
  12. %canpassitinasoneargument.Ifyoubreakitinto3pieces,convert
  13. %themintodoublebeforecallingthisfunction.
  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. %Setathreshold
  27. T=0.008856;
  28. RGB=[reshape(R,1,s);reshape(G,1,s);reshape(B,1,s)];
  29. %RGBtoXYZ
  30. MAT=[0.4124530.3575800.180423;
  31. 0.2126710.7151600.072169;
  32. 0.0193340.1191930.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. %ComputeL
  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. %Computeaandb
  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. %Lab2RGBtakesmatricescorrespondingtoL,a,andbinCIELabspace
  4. %andtransformsthemintoRGB.ThistransformisbasedonITU-R
  5. %RecommendationBT.709usingtheD65whitepointreference.
  6. %andtheerrorintransformingRGB->Lab->RGBisapproximately
  7. %10^-5.ByMarkRuzonfromCcodebyYossiRubner,23September1997.
  8. %UpdatedforMATLAB528January1998.
  9. %Fixedabuginconversionbacktouint89September1999.
  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. %ComputeY
  24. fY=((L+16)/116).^3;
  25. YT=fY>T1;
  26. fY=(~YT).*(L/903.3)+YT.*fY;
  27. Y=fY;
  28. %AlterfYslightlyforfurthercalculations
  29. fY=YT.*(fY.^(1/3))+(~YT).*(7.787.*fY+16/116);
  30. %ComputeX
  31. fX=a/500+fY;
  32. XT=fX>T2;
  33. X=(XT.*(fX.^3)+(~XT).*((fX-16/116)/7.787));
  34. %ComputeZ
  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.9692561.8759920.041556;
  42. 0.055648-0.2040431.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值