% Face Image 
clear;
I1=imread('Face.bmp');
figure(1)
imagesc(I1);

I1r=im2double(I1(:,:,1));
I1g=im2double(I1(:,:,2));
I1b=im2double(I1(:,:,3));
I1Y=(0.297*I1r+ 0.587*I1g+0.114*I1b)*255;
I1Cb=(-0.169*I1r-0.331*I1g+0.5*I1b)*255+128;
I1Cr=(0.5*I1r-0.419*I1g-0.081*I1b)*255+128;
imwrite(I1Y/255,'Face_Y.bmp');
imwrite(I1Cb/255,'Face_Cb.bmp');
imwrite(I1Cr/255,'Face_Cr.bmp');

figure(1)
imagesc(I1Y);colormap(gray)

% Luminance Distribution
figure(2)
Y=0:255;
hist(I1Y(:), Y); grid on;
xlabel('Luminance Y');
ylabel('Frequency Distribution D(Y)')
axis([0, 255, 0, 8000])

% Face Area decision(113<=Y<=160)
for m=1:337
    for n=1:404
        if((I1Y(n,m)<113 |I1Y(n,m)>160))
            I1Y(n,m)=16;
        else
            I1Y(n,m)=255;
        end
    end
end

figure(3)
imagesc(I1Y);colormap(gray)
imwrite(I1Y/255,'Face_area.bmp');

% Luminance Average along x and y
        A=sum(I1Y);
        B=sum(I1Y,2);
figure(4)
imagesc(I1Y);colormap(gray)
imwrite(I1Y/255,'barbara256_block.bmp');
x=1:337;
y=1:404;
F=[1 1 1 1 1]/5;
AA=conv(A,F);
D_AA=(diff(AA));
for m=3:339; D_A(m-2)=fix(D_AA(m)); end;
for m=3:335
    if((D_A(m)>-1 | D_A(m)<1) & D_A(m-2)<-100 & D_A(m+2)>100 & A(m)>15000)
        xp(m)=1;
    else
        xp(m)=0;
    end
end;
xp(1)=0; xp(2)=0; xp(336)=0; xp(337)=0;
BB=conv(B,F);
D_BB=(diff(BB));
for n=3:406; D_B(n-2)=fix(D_BB(n)); end;
for n=3:402
    if((D_B(n)>-1 | D_B(n)<1) & D_B(n-2)<-100 & D_B(n+2)>100 & B(n)>15000)
        yp(n)=1;
    else
        yp(n)=0;
    end
end;
yp(1)=0; yp(2)=0; yp(403)=0; yp(404)=0;
for n=3:406; D_B(n-2)=D_BB(n); end;
figure(5)
plot(x,A);
grid on;
xlabel('X position');
ylabel('Accumulated Luminance sigma(X)')
axis([1, 337, 0, 50000])

figure(6)
plot(y,B);
grid on;
xlabel('Y position');
ylabel('Accumulated Luminance sigma(Y)')
axis([1, 404, 0, 40000])

figure(7)
subplot(3,2,1);plot(x,A);
subplot(3,2,2);plot(y,B);
subplot(3,2,3);plot(x,D_A);
subplot(3,2,4);plot(y,D_B);
subplot(3,2,5);plot(x,xp);
subplot(3,2,6);plot(y,yp);
%
for m=1:337
    if(xp(m)==1)
        for n=1:404; I1Y(n,m)=128; end;
    end;
end;
for n=1:404
    if(yp(n)==1)
        for m=1:337; I1Y(n,m)=128; end;
    end;
end;
figure(8)
imagesc(I1Y);colormap(gray)
imwrite(I1Y/255,'Face_parts.bmp');
Cross=