python 数据图像修正_如何根据标准图像校正图像的颜色

T=imread('image.png');% Load input image (two images side by side).image1=T(:,1:end/2,:);% Left sideimage2=T(:,end/2+1:end,:);% Right sideI=image1;% Source image is named I% Use color components in range [0, 1] (colors were found by manual picking).src_sRGB=[205,232,32]/255;%Triangle sRGB color from image 1 "source image"dst_sRGB=[13,133,38]/255;%Triangle sRGB color from image 2 "destination image"%Linearize gamma-corrected RGB values (image values are in sRGB color space, we need to Linearize them).srcRGB=rgb2lin(src_sRGB)';dstRGB=rgb2lin(dst_sRGB)';linI=rgb2lin(double(I)/255);% I in linear RGB color space.% Color correction by Chromatic Adaptation:%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html% Convert from XYZ D65 color space to Linear sRGB color space.XYZD65_to_sRGB=[3.2404542-1.5371385-0.4985314-0.96926601.87601080.04155600.0556434-0.20402591.0572252];% Convert from Linear sRGB color space to XYZ D65 color space.sRGBtoXYZD65=[0.41245640.35757610.1804375;...0.21267290.71515220.0721750;...0.01933390.11919200.9503041];% Convert srcRGB and dstRGB to XYZ color spacesrcXYZ=sRGBtoXYZD65*srcRGB;dstXYZ=sRGBtoXYZD65*dstRGB;% Convert srcXYZ and dstXYZ to xyY color space (get only xy out of xyY - xy applies chromaticity).xySrc=XYZ2xy(srcXYZ);xyDst=XYZ2xy(dstXYZ);xyzSrc=xy2XYZ(xySrc,1);%normalize Y to 1 so D65 luminance comparablexyzDst=xy2XYZ(xyDst,1);%normalize Y to 1 so D65 luminance comparable% Chromatic adaptation transformcatType='bradford';%Bradford transformation is recommended by Bruce Lindbloom http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.htmlestMAT=cbCAT(xyzSrc,xyzDst,catType);% Ascale estMAT by XYZD65_to_sRGB before apply color correctionM=estMAT*XYZD65_to_sRGB;linI=cbreshape(linI);% Destination image - apply color correction be multiplying by correction matrix MlinJ=M*linI;linJ=cbunshape(linJ,size(I));% Convet J from Linear to sRGBJ=lin2rgb(linJ);% Convert from double to uint8 (multiply by 255).J=im2uint8(J);% Display resultfigure;imshow(J);title('Corrected image1');impixelinfofigure;imshow(image2);title('image2');impixelinfo% Save resultimwrite(image2,'image2.png');imwrite(J,'J.png');functionxy=XYZ2xy(xyz)%xy = XYZ2xy(xyz)% Converts CIE XYZ to xy chromaticity.X=xyz(1,:);Y=xyz(2,:);s=sum(xyz);xy=[X./s;Y./s];endfunctionXYZ=xy2XYZ(xy,Y)%XYZ = xy2XYZ(xy,Y)% Converts xyY chromaticity to CIE XYZ.x=xy(1);y=xy(2);XYZ=[Y/y*x;Y;Y/y*(1-x-y)];endfunctionoutMat=cbCAT(xyz_src,xyz_dst,type)%https://web.stanford.edu/~sujason/ColorBalancing/adaptation.html%M = cbCAT(xyz_src, xyz_dst, type)% Chromatic adaptation transform via von Kries's method.% type chooses the LMS-like space to apply scaling in, valid options:% 'vonKries', 'bradford', 'sharp', 'cmccat2000', 'cat02', 'xyz'% See http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.htmlxyz_src=makecol(xyz_src);xyz_dst=makecol(xyz_dst);% the following are mostly taken from S. Bianco. "Two New von Kries Based% Chromatic Adaptation Transforms Found by Numerical Optimization."ifstrcmpi(type,'vonKries')%Hunt-Pointer-Estevez normalized to D65Ma=[0.400240.7076-0.08081;-0.22631.165320.0457;000.91822];elseifstrcmpi(type,'bradford')Ma=[0.89510.2664-0.1614;-0.75021.71350.0367;0.0389-0.06851.0296];elseifstrcmpi(type,'sharp')Ma=[1.2694-0.0988-0.1706;-0.83641.80060.0357;0.0297-0.03151.0018];elseifstrcmpi(type,'cmccat2000')Ma=[0.79820.3389-0.1371;-0.59181.55120.0406;0.00080.2390.9753];elseifstrcmpi(type,'cat02')Ma=[0.73280.4296-0.1624;-0.70361.69750.0061;0.00300.01360.9834];elseMa=eye(3);end%Chromatic Adaptation Transforms:%1. Transform from XYZ into a cone response domain (ro, gamma, beta)%2. Scale the vector components by factors dependent upon both the source and destination reference whites.%3. Transform from (ro, gamma, beta) back to XYZ using the inverse transform of step 1.%D is diagonal matrix marked as inv(Ma)*diag(roD/roS, gammaD/gammaS, betaD/betaS)*Ma.%Matrix D applies ratios in "cone response domain".D=diag((Ma*xyz_dst)./(Ma*xyz_src));%Transform back to XYZ domain:M=Ma\D*Ma;sRGBtoXYZ=[0.41245640.35757610.1804375;...0.21267290.71515220.0721750;...0.01933390.11919200.9503041];outMat=sRGBtoXYZ\M*sRGBtoXYZ;endfunctionx=makecol(x)%x = makecol(x)% returns x as a column vectors=size(x);if(length(s)==2)&&(s(1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值