²é¿´: 1452  |  »Ø¸´: 2
µ±Ç°Ö»ÏÔʾÂú×ãÖ¸¶¨Ìõ¼þµÄ»ØÌû£¬µã»÷ÕâÀï²é¿´±¾»°ÌâµÄËùÓлØÌû

shn521

гæ (³õÈëÎÄ̳)

[½»Á÷] ¡¾ÇóÖú¡¿°ïæ¿´ÏÂÕâËĸömatlabËã·¨£¬ÎÒÊDzËÄñÒÑÓÐ2È˲ÎÓë

°ïæ¿´¿´ÕâËĸöËã·¨µÄ×÷Óã¬×îºÃÄܽ²½âºÍ¶Ô±ÈÏ£¬·Ç³£¸Ðл

µÚÒ»¸ögrayWorld.m
function grayWorld(filename,outFile,catType,maxIter,plots)
%grayWorld(filename,outfile,catType,maxIter,plot)
% Performs color balancing via the gray world assumption and a chromatic
% adpatation transform.  Can be run interatively to possibly improve the
% result.  Set plot = 0 or 1 to turn diagnostic plots on or off.

tutorialinit

%% import image
im_orig = cbimread(filename);
% figure
% imshow(im_orig)
% title('Original Image')

%% various constants
xyz_D65 = [95.04; 100; 108.88]; %http://en.wikipedia.org/wiki/D65, normalized Y = 100

sRGBtoXYZ =   [0.4124564  0.3575761  0.1804375; ...
               0.2126729  0.7151522  0.0721750; ...
               0.0193339  0.1191920  0.9503041];

%% grayworld
% catType = 'vonKries';
% maxIter = 1;
b = 0.001; %convergence threshold

imRGB_orig = cbreshape(im_orig)*255;
imRGB = imRGB_orig;

grayDiff =[];
for iter = 1:maxIter
    rgbEst = mean(imRGB,2); %grayworld, average everything
    grayDiff = [grayDiff norm([rgbEst(1)-rgbEst(2),rgbEst(1)-rgbEst(3),rgbEst(2)-rgbEst(3)])];
   
    if grayDiff(end) < b
        disp(['Converged. RGB difference vector < ' num2str(b) ' in magnitude.'])
        break
    elseif iter >= 2 && abs(grayDiff(end-1)-grayDiff(end)) < 10^-6
        disp(['RGB difference vector no longer improving.'])
        break
    end
   
    xyEst = XYZ2xy(sRGBtoXYZ*rgbEst); %calculate xy chromaticity
    xyzEst = xy2XYZ(xyEst,100); %normalize Y to 100 so D65 luminance comparable
    imRGB = cbCAT(xyzEst,xyz_D65,catType)*imRGB;
end

imwrite(cbunshape(imRGB,size(im_orig))/255,outFile,'png');

if plots
    length(grayDiff) %number of iterations done
%     figure
%     imshow(cbunshape(imRGB,size(im_orig))/255)
%     title('Gray World Corrected')
   
    figure
    plot(grayDiff)
    title('GW: Norm of RGB Difference Vector vs Iterations')
    print(gcf,'-dpng',[outFile '-fig1'])
end

µÚ¶þ¸ösimplestColorBalance.m
function simplestColorBalance(filename,outFile,satLevel,plots)
%simplestColorBalance(filename,outFile,satLevel,plot)
% Performs color balancing via histogram normalization.
% satLevel controls the percentage of pixels to clip to white and black.
% Set plot = 0 or 1 to turn diagnostic plots on or off.

tutorialinit

%% import image
im_orig = cbimread(filename);
% figure
% imshow(im_orig)
% title('Original Image')


%% full width histogram method
% satLevel = .01; %percentage of the image to saturate to black or white, tweakable param
q = [satLevel/2 1-satLevel/2];

imRGB_orig = cbreshape(im_orig)*255;
imRGB = zeros(size(imRGB_orig));
N = size(imRGB_orig,2);
color = {'r','g','b'};
for ch = 1:3
    if plots
        figure
        subplot(211)
        hist(imRGB_orig(ch,:),256)
        set(findobj(gca,'Type','patch'),'FaceColor',color{ch},'EdgeColor',color{ch})
        xlim([0 255])
        title('Original Histogram')
    end
    tiles = quantile(imRGB_orig(ch,:),q);
%     [sum(imRGB_orig(ch,:)tiles(2))/N] %check percentages are correct
    imRGB(ch,:) = cbsaturate(imRGB_orig(ch,:),tiles); %saturate at the appropriate pts. in distribution
    bottom = min(imRGB(ch,:)); top = max(imRGB(ch,:));
    imRGB(ch,:) = (imRGB(ch,:)-bottom)*255/(top-bottom);
   
    if plots
        subplot(212)
        hist(imRGB(ch,:),256)
        set(findobj(gca,'Type','patch'),'FaceColor',color{ch},'EdgeColor',color{ch})
        xlim([0 255])
        title('Corrected Histogram')
    end
    print(gcf,'-dpng',[outFile '-fig' num2str(ch)])
end

imwrite(cbunshape(imRGB,size(im_orig))/255,outFile,'png');
% figure
% imshow(cbunshape(imRGB,size(im_orig))/255)
% title('Simplest Color Balance Corrected')

µÚÈý¸örobustAWB.m
function robustAWB(filename,outFile,option,catType,T,maxIter,plots)
%robustAWB(filename,outFile,option,catType,T,maxIter,plot)
% Performs robust auto white-balancing by estimating gray pixels based on
% their deviation in YUV space then applying an iterative correction via
% CAT or directly adjusting the R and B channels.
% Set option = 'RB gain' or 'cat' for the correction method.
% Set T higher for a larger threshold of deviation to consider off-gray.
% Set plot = 0 or 1 to turn diagnostic plots on or off.

tutorialinit

%% import image
im_orig = cbimread(filename);
% figure
% imshow(im_orig)
% title('Original Image')

%% various constants
xyz_D65 = [95.04; 100; 108.88]; %http://en.wikipedia.org/wiki/D65, normalized Y = 100

sRGBtoXYZ =   [0.4124564  0.3575761  0.1804375; ...
               0.2126729  0.7151522  0.0721750; ...
               0.0193339  0.1191920  0.9503041];

%% robust auto white balance - YUV grays
% option = 'RB gain'; % adjustments are done as a gain to only R or B channelas descripted in Huo
% option = 'cat';
% catType = 'CAT02';

% maxIter = 1000;

% T = 0.3; %this should be tweakable slider
% T = 0.1;
% gain adjustment parameters, can probably be optimized via more control system analysis
u = .01; %gain step size
a = .8; %double step threshold
b = .001; %convergence threshold

% rgb to yuv
xfm =   [0.299 0.587 0.144; ...
        -0.299 -0.587 0.886; ...
        0.701 -0.587 -0.114];
   
inv_xfm = inv(xfm);
   
imRGB_orig = cbreshape(im_orig)*255;
imRGB = imRGB_orig;
gain = [1 1 1];

U_avg = [];
V_avg = [];
totGray = [];
for iter = 1:maxIter
    im = xfm*imRGB; %convert to YUV
    % find gray chromaticity
    % (|U|+|V|)/Y
    F = ( abs(im(2,:)) + abs(im(3,:)) )./im(1,:);
   
%     figure
%     imshow(cbunshape(imRGB/255.*repmat(F %     % imshow(cbunshape(inv_xfm)*im+[F>T;zeros(2,length(F))],size(im_orig))) %rubylith overlays
%     title('Gray Pixels Found by Robust AWB'),xlabel('Pixels in their original color are considered gray')
   
    totGray = [totGray sum(F     if totGray(end) == 0
        disp('No valid gray pixels found.')
        break
    end
    grays = im(:,F     U_bar = mean(grays(2,:));
    V_bar = mean(grays(3,:));
   
    U_avg = [U_avg U_bar];
    V_avg = [V_avg V_bar];
   
    if strcmpi(option,'cat')
        if max(abs([U_bar V_bar])) < b
            disp(['Converged. U_bar and V_bar < ' num2str(b) ' in magnitude.'])
            break
        elseif iter >= 2 && norm([U_avg(end)-U_avg(end-1) V_avg(end)-V_avg(end-1)]) < 10^-6
            disp(['U_bar and V_bar are no longer improving.'])
            break
        end
        rgbEst = inv_xfm*[100;U_bar;V_bar]; %convert the average gray from YUV to RGB
        xyEst = XYZ2xy(sRGBtoXYZ*rgbEst); %calculate xy chromaticity
        xyzEst = xy2XYZ(xyEst,100); %normalize Y to 100 so D65 luminance comparable
        imRGB = cbCAT(xyzEst,xyz_D65,catType)*imRGB;
    else
        if abs(U_bar) > abs(V_bar) % U > V; blue needs adjustment
            err = U_bar;
            ch = 3; %blue channel
        else
            err = V_bar;
            ch = 1; %red channel
        end
        if abs(err) >= a
            delta = 2*sign(err)*u; %accelerate gain adjustment if far off
        elseif abs(err) < b %converged
            delta = 0;
            disp(['Converged. U_bar and V_bar < ' num2str(b) ' in magnitude.'])
            break
        else
            delta = err*u;
        end
%         [err,delta]
        gain(ch) = gain(ch)-delta; %negative fdbk loop
        imRGB = diag(gain)*imRGB_orig;
    end
end

imwrite(cbunshape(imRGB,size(im_orig))/255,outFile,'png');
% figure
% imshow(cbunshape(imRGB,size(im_orig))/255)
% title('Robust AWB Corrected')
if plots
    length(U_avg)
    figure
    plot(totGray)
    title('rAWB: Total Gray Pixels vs Iterations')
    print(gcf,'-dpng',[outFile '-fig1'])
   
    figure
    hold on
    plot(U_avg,'b')
    plot(V_avg,'r')
    hold off
    title('rAWB: Mean Chromaticity vs Iterations')
    legend('U','V')
    print(gcf,'-dpng',[outFile '-fig2'])
        
    figure
    imshow(cbunshape(imRGB/255.*repmat(F     % imshow(cbunshape(inv_xfm)*im+[F>T;zeros(2,length(F))],size(im_orig))) %rubylith overlays
    title('Gray Pixels Found by Robust AWB'),xlabel('Pixels in their original color are considered gray')
    print(gcf,'-dpng',[outFile '-fig3'])
end

µÚËĸösensorCorrelation.m
function sensorCorrelation(filename,outFile,catType,plots)
%sensorCorrelation(filename,outFile,catType,plot)
% Performs the sensor correlation method for illuminant estimation and then
% a chromatic adaptation transform for correction.
% Set plot = 0 or 1 to turn diagnostic plots on or off.

% clear
tutorialinit
load refGamut.mat %a sample set of reference gamuts simulated by ISET for mired = 118:23.5:400

xyz_D65 = [95.04; 100; 108.88]; %http://en.wikipedia.org/wiki/D65, normalized Y = 100
%% import image
im_orig = cbimread(filename);
% figure
% imshow(im_orig)
% title('Original Image')


%% sensor correlation
clipVal = 225;

imRGB_orig = cbreshape(im_orig)*255;
imRGB = imRGB_orig;

%% pre-process
imRGB = imRGB(:,max(imRGB,[],1)= 225+1 since we will saturate later

%populate color space
imRGBSpace = zeros(clipVal+1,clipVal+1,clipVal+1,'uint8');
for i=1:size(imRGB,2)
    c = imRGB(:,i);
    imRGBSpace(c(1)+1,c(2)+1,c(3)+1) = imRGBSpace(c(1)+1,c(2)+1,c(3)+1) || 1;
end

%compute connectivity lookup table
% imRGBSpaceConn = imfilter(imRGBSpace,conndef(size(imRGB,1),'maximal')).*imRGBSpace>2; %only keep colors that are connected to more than 2 neighbors
%the above is the truly correct way but since we'll never access the color
%entries not in the image, we don't need to zero them out, this is good enough and faster
imRGBSpaceConn = imfilter(imRGBSpace,conndef(size(imRGB,1),'maximal'))>2; %must have more than 1 neighbor

imRGBConn = false(1,size(imRGB,2));
for i=1:length(imRGBConn)
    c = imRGB(:,i);
    imRGBConn(i) = imRGBSpaceConn(c(1)+1,c(2)+1,c(3)+1);
end
imRGB = imRGB(:,imRGBConn); %keep only pixels in the image with connected color values
clear imRGBSpaceConn imRGBSpace imRGBConn

imRGB = imRGB(:,max(imRGB,[],1)= 225, saturated

% normalize
I = zeros(1,size(imRGB_orig,2));
for i = 1:length(I)
    I(i) = norm(imRGB_orig(:,i)); %sqrt(R^2+G^2+B^2)
end
imRGB = 255*imRGB/max(I);

%%
[gamut_im,gamutArea_im] = convhull(imRGB(1,:),imRGB(3,:)); %image gamut is the convex hull of (R,B) points
[x,y] = poly2cw(imRGB(1,gamut_im), imRGB(3,gamut_im));
gamut_im = [x; y]; %the image gamut vertices

% gamut_illum =[]; %gamuts of typical illuminants %2xnx# illum
% gamutArea_illum = []; % # illum x 1

kScale = .1:.1:1;
corr = zeros(length(kScale),length(gamutArea_illum)); %rows are scaling, cols are different illuminants
for j=1:length(kScale)
    k = kScale(j);
    for i = 1:length(gamut_temps)
        gi = gamut_illum{i};
        [x,y] = polybool('&',k*gamut_im(1,:),k*gamut_im(2,:),gi(1,:),gi(2,:));
        corr(j,i) = polyarea(x,y)/sqrt(gamutArea_im*gamutArea_illum(i));
    end
end
% corr
[corr,k] = max(corr,[],1);
[maxCorrVal,targetIllum] = max(corr);
k = k(targetIllum);
T = gamut_temps(targetIllum)

%% correct
xyzEst = xy2XYZ(XYZ2xy(gamut_XYZ(:,targetIllum)),100); %xyz color cast estimate normalized Y to 100

% catType = 'vonKries';

imRGB = imRGB_orig;
imRGB = cbCAT(xyzEst,xyz_D65,catType)*imRGB;

% figure
% imshow(cbunshape(imRGB,size(im_orig))/255)
% title(['Sensor Correlation Corrected, T=' num2str(T)])

imwrite(cbunshape(imRGB,size(im_orig))/255,outFile,'png');
%% plot
if plots
    figure,clf
    title('Gamut Plots')
    for i=1:length(gamut_temps)
        gi = gamut_illum{i};
        x = gi(1,:);
        y = gi(2,:);
        hold on
        if i == targetIllum
            patch(x, y, 1, 'FaceColor', 'b')
        else
            patch(x, y, 1, 'FaceColor', 'g')
        end
        hold off
    end
    hold on
    % plot(imRGB(1,:),imRGB(3,:),'.')
    patch(gamut_im(1,:), gamut_im(2,:), 1, 'FaceColor', 'r')
    hold off
    print(gcf,'-dpng',[outFile '-fig1'])
end
% [k2,a2] = convhull(x2,y2);
%
% [xp1,yp1] = poly2cw(x1(k1),y1(k1));
% [xp2,yp2] = poly2cw(x2(k2),y2(k2));
% % xp1 = x1(k1); yp1 = y1(k1);
% % xp2 = x2(k2); yp2 = y2(k2);
%
%
% [x,y] = polybool('&',xp1,yp1,xp2,yp2);
%
% % figure(10),clf
% % hold on
% % patch(x, y, 1, 'FaceColor', 'g')
% % plot(x1,y1,'.',x2,y2,'r.')
% % plot(x1(k1),y1(k1),'-',x2(k2),y2(k2),'r-')
% % hold off
%
% corr = polyarea(x,y)/sqrt(a1*a2)
end
»Ø¸´´ËÂ¥

» ÊÕ¼±¾ÌûµÄÌÔÌùר¼­ÍƼö

source

» ²ÂÄãϲ»¶

» ±¾Ö÷ÌâÏà¹Ø¼ÛÖµÌùÍƼö£¬¶ÔÄúͬÑùÓаïÖú:

ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû

shn521

гæ (³õÈëÎÄ̳)

°ï°ïæ
2Â¥2011-02-27 09:50:16
ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
Ïà¹Ø°æ¿éÌøת ÎÒÒª¶©ÔÄÂ¥Ö÷ shn521 µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍƼö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼²©] ¶ÔÏóûÓУ¬»¹·Ç³£Ïë¶Á²©£¬ÄÑÒÔ¾ñÔñ +10 pvrw0224 2024-06-23 30/1500 2024-06-24 00:47 by pvrw0224
[½Ìʦ֮¼Ò] ¸ßУ´ÇÖ°£¬ÒªÇóÅâ³¥£¬Õâµ½µ×ºÏ²»ºÏÀí +14 ´«¶¯_º£Éñ 2024-06-23 19/950 2024-06-23 23:23 by dogdog2021
[»ù½ðÉêÇë] ¹ú×ÔÈ»ÇàÄê»ù½ð£¬1A4BÄÜÉÏ»áÂð£¿ÇàÄêºÍÃæÉϵÄÉÏ»á±ê×¼ÊÇÒ»ÑùµÄÂ𣿠+19 ½ñÍíÍƼö22 2024-06-20 32/1600 2024-06-23 23:17 by andywei1028
[¿¼²©] ÉêÇë25²©Ê¿£¬¿ÉÒÔÌáÇ°½ø×é×ö¿ÆÑÐÖúÀí +5 ÖðÃÎ;ÖÐw 2024-06-22 6/300 2024-06-23 22:38 by jsÍõÑý
[˶²©¼ÒÔ°] Êý¾Ý²»ºÃ +3 Hetai 2024-06-23 5/250 2024-06-23 20:36 by 82ÄêÀ­·Æ
[»ù½ðÉêÇë] ¹ú×ÔÈ»×ÊÖú±ÈÂÊÊDz»ÊÇҪϽµÁË£¿£¿ +8 ½ñÍíÍƼö22 2024-06-21 11/550 2024-06-23 19:18 by ½ñÍíÍƼö22
[ÂÛÎÄͶ¸å] ÂÛÎÄÌá½»¶þÉó»¹ÓÐÈýÌì¾ÍÈý¸öÔÂÁË£¬Á¬ÐøÎÊÁ˱༭²¿¼¸´Î 10+3 ´óÍõ½ÐÎÒÀ´Ñ°É½Ä 2024-06-22 7/350 2024-06-23 16:18 by Ͷ±ØµÃ¿ÆÑйËÎÊ
[»ù½ðÉêÇë] ¸Õ¸ÕÊÕµ½¿ÆÑÐÖ®ÓÑÓʼþ +27 olivermiaoer 2024-06-19 40/2000 2024-06-23 11:45 by qq632458
[¹«Åɳö¹ú] CSC²©Ê¿ÁªÅà¶Ô½«À´¾ÍÒµÓÐÓÃÂð +3 Ò²¾ÍÕâÑù 2024-06-22 3/150 2024-06-22 22:04 by 326lhpqk
[Óлú½»Á÷] Èý¾±Æ¿¼ÓÈÈ 5+3 wzjwx888 2024-06-20 3/150 2024-06-22 16:38 by zyp0009928
[¿¼²©] Óлú»¯Ñ§ÃÔãѧÉú +6 ·ðϵÃþÓã5 2024-06-18 11/550 2024-06-22 15:47 by yuanjijoy
[Óлú½»Á÷] ¼ä°±»ù±½¼×Ëá¼×õ¥ºÏ³É 5+3 pengdatao 2024-06-19 6/300 2024-06-22 10:58 by zyp0009928
[²©ºóÖ®¼Ò] ÔÚ¹úÄÚij¸ßУ×öÈ«Ö°²©Ê¿ºó2Ä꣬ÏÖÔÚÕÒµ½Ðµĵ¥Î»£¬³öÕ¾»òÍËÕ¾¶Ôй¤×÷ÓÐʲôӰÏ죿 +10 nxplfcc 2024-06-20 10/500 2024-06-22 07:52 by Ð쳤°²
[Óлú½»Á÷] Óлú·´Ó¦ 50+3 ÓêÖеĺìõ¹å 2024-06-19 6/300 2024-06-22 00:10 by ϲ»¶ºÍÒ»Ñõ»¯¶þÌ
[Óлú½»Á÷] ÅÜ°åÄÜÅÜ¿ª£¬¹ýÖù¹ý²»´¿Ôõô°ì +8 СºúÔÚŬÁ¦ 2024-06-18 10/500 2024-06-21 08:22 by hptianyan
[¾«Ï¸»¯¹¤] ÊÔ¼Á¼Û¸ñ +7 Ðñ±ØÉÏ°¶ 2024-06-17 10/500 2024-06-20 23:35 by ÏàÓë´¦ÓÚ½µÄÓã
[ÂÛÎÄͶ¸å] µÚһƪÂÛÎÄͶ¸å½ø³Ì¼Ç¼ +4 É÷¶ÀµÄС»¨¾í 2024-06-20 9/450 2024-06-20 20:37 by É÷¶ÀµÄС»¨¾í
[ÂÛÎÄͶ¸å] ACS AMI ·µ»ØÉó¸åÒâ¼û£¬Ò»¸ö´óÐÞ£¬Á½¸ö¾Ý¸å£¬±à¼­¸øµÄÐÞ¸ÄÖØͶ +7 ÖÇÉÌÒѸüР2024-06-19 7/350 2024-06-20 19:54 by kinlin13
[ÂÛÎÄͶ¸å] Ͷ¸åÇóÖú 6+3 С¶ÌÍÈ°²ç÷À­ 2024-06-19 4/200 2024-06-20 17:40 by ²»Ò»ÑùÑÌ»ð12345
[ÂÛÎÄͶ¸å] Ͷ¸åÇóÖú +4 ƽ·²µÄÈÕ×Ó 2024-06-19 5/250 2024-06-20 16:24 by yueyueyue@
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û