
% Lab 10 Solutions
% CS 1109, Summer 2011
% July 28


function avgGray(im)
%
% avgGray: This function converts a color image to grayscale via averaging
%
%   This function displays a grayscale conversion of a given color image.
%   The grayscale value of each pixel is computed as the average of the
%   original RGB values. We assume that the input argument im is a color
%   image. The function does not return anything.
%

% We convert the input uint8 array to double, compute the grayscale values
% of each pixel, and then reconvert to uint8 before display. This avoids
% rounding errors caused by uint8 arithmetic.
im = double(im);
gray = uint8((im(:,:,1) + im(:,:,2) + im(:,:,3)) / 3);
imshow(gray);

end

