% ------------------------------------------------------------------------
%   ECE 178 - Tutorial 1
%   by Marco Zuliani - 01/09/2002
%
%   DESCRIPTION
%   In this example we will examine how to create and manipulate a figure
%   using matlab and the image processing toolbox.
% ------------------------------------------------------------------------


% let's clear the memory of the workspace
clear all
% let's clear the command window
clc
% and let's close all the figures
close all


% An RGB image of width w and height h is a h by w by 3 matrix containing
% the RGB components of each pixel. Let's create an empty image of 256x256
% pixels. Note the usage of the function uint8 in order to have that each
% element of the array represented by unsigned byte (ranging from 0 to
% 255). Note that the total number of colors is 256^3, i.e. about 16
% millions of colors
I = uint8(zeros(256, 256, 3));


% Now let's create some pattern: we want to divide the image in four squares
% with colors red, green, blu and white.
% Let's define a couple of vectors containing the indices of the image
% areas we are interested in:
ind1 = 1:128;
ind2 = 129:256;
% Let's form the red square
I(ind1, ind1, 1) = 255;
I(ind1, ind1, 2) = 0;
I(ind1, ind1, 3) = 0;
% Let's form the green square
I(ind1, ind2, 1) = 0;
I(ind1, ind2, 2) = 255;
I(ind1, ind2, 3) = 0;
% Let's form the blue square
I(ind2, ind1, 1) = 0;
I(ind2, ind1, 2) = 0;
I(ind2, ind1, 3) = 255;
% Let's form the white square
I(ind2, ind2, 1) = 255;
I(ind2, ind2, 2) = 255;
I(ind2, ind2, 3) = 255;


% Now suppose that we want to add a central square with gray levels that
% range from white to black from top to bottom.
i0 = 64;
j0 = 64;


for i = 0:127
    
    I(i0+i, j0:j0+128, 1) = 2*i;
    I(i0+i, j0:j0+128, 2) = 2*i;
    I(i0+i, j0:j0+128, 3) = 2*i;        
    
end;


% now let's conver the image to gray scale. Check that know the image is
% represented by an h by w matrix
Ibw = rgb2gray(I);


% and let's plot the results
subplot(1, 2, 1);
imshow(I);
title('Color image')
subplot(1, 2, 2);
imshow(Ibw);
title('Gray levels image')


% suppose we want to save the image we created: we will use the command
% imwrite. Then you will be able to import your image with your favourite
% program (for example Gimp, Photoshop, etc...)
imwrite(I, 'test_image.bmp', 'bmp');

