clear all;
clc;

%% images I/O on file
% Load the image from file into the X matrix
X = imread('satellite_gray.jpg');

% show the image
figure;
imshow(X);

% Images are matrices, but not in double
whos
% uint8: 8 bit -> 0..255
% elements greater than 255 are shown as white
imshow(X*2)
% elements smaller than 0 are shown as black
imshow(X-150)

% Images can be black-and-white (binary) images (0 black, 1 white)
imshow(X<80)

% Store an image in a file
imwrite(X<80, 'sat_threshold.png');
% Image representation format can be specified.
% When no explicit options are provided, the most cheap format is chosen
T = imread('sat_threshold.png');
whos

%% Conversion
X = imread('teethwhitening_Woman_Smile_small.jpg');
imshow(X);
title('RGB');
whos
% color images are represented as combination of three primary colors 
% (Red, Green, Blue).
% In Matlab this leads to represent images as three-dimensional matrices
% (#rows, #columns, #channels).

%
figure;
imshow(X(:,:,1))
title('R');
figure;
imshow(X(:,:,2))
title('G')
figure;
imshow(X(:,:,3))
title('B');

% color images can be converted into gray level images
T = rgb2gray(X);
figure;
imshow(T);
title('Gray levels');
whos

%% Casting
% The integer representation can cause some numerical problems
T(25767),
T(25767)/2,
T(25767)*100,
% casting allow to process the integer values as double precision ones
double(T(25767))/2,
double(T(25767))*100,
% it can be useful also for representation purposes
mesh(double(T))

%% Topology
% Return a structure with the list of the connected components wrt the specified neighborhood relationship.
X = T > 220;
cc = bwconncomp(X,4);
