% ImageDemoBW
% Producing a Black and White Image from a Color image

close all
figure
set(gcf,'position',[100 100 800 600])
A = imread('LawSchool.jpg');
[m,n,p] = size(A);
image(A)
pause

% using rgb2gray...

figure
set(gcf,'position',[100 100 800 600])
bwA = rgb2gray(A);
imshow(bwA)
imwrite(bwA,'MatBW.jpg')
pause

B = uint8(zeros(m,n));
C = uint8(zeros(m,n));
for i=1:m
for j=1:n
B(i,j) = round((A(i,j,1) + A(i,j,2) + A(i,j,3))/3);
C(i,j) = max([A(i,j,1) A(i,j,2) A(i,j,3)]);
end
end
figure
set(gcf,'position',[100 100 800 600])
imshow(B)
title('By Averaging','fontsize',14)
imwrite(B,'AveBw.jpg')

figure
set(gcf,'position',[100 100 800 600])
imshow(C)
title('By max','fontsize',14)
imwrite(C,'MaxBW.jpg')