values = imread('frame2.bmp', 'bmp'); % reads image from file
figure(1); imshow(values);

disp(values(53, 122, 1)); % shows the red component of pixel 53, 122, 1)
disp(values(53, 122, 2)); % shows the green component of pixel 53, 122, 1)
disp(values(53, 122, 3)); % shows the blue component of pixel 53, 122, 1)

% converts image to double type (each color component is a double), 
% and converts range from [0 255] to [0 1]
converted = double(values) / 255; 
                            
% to correctly display a double-type image, its values must be between 0 and 1
% (whereas for uchar type, they must be between 0 and 255).
figure(2); imshow(converted); 
                              
% convert color image to grayscale                              
gray = (converted(:,:,1) + converted(:,:,2) + converted(:,:,3)) / 3; 
figure(1); imshow(gray)                              

% apply an x-derivative filter. The filter is matrix
% -1 0 1
% -1 0 1
% -1 0 1
% and it is normalized so that the maximum absolute value of the result is 1
% note that we take absolute values of the result of filtering
gray_x = abs(conv2(gray, [-1 0 1; -1 0 1; -1 0 1]/3));
imshow(gray_x);

% display the value for pixel (32,53)
disp(gray_x(32,53));

% convert gray_x to an image in the [0 255] range
max_value = max(max(gray_x));
min_value = min(min(gray_x));
range = max_value - min_value;
gray_x = round((gray_x - min_value) / range * 255);

% save gray_x as a bitmap image
% notice that we convert gray_x to a matrix of uint8 type, before saving.
imwrite(uint8(gray_x), 'output.ppm', 'ppm');


% remember to use the help function to see how to use other matlab
% commands, such as if, for, while, zero, rand, ones, etc. For example:
help for
