% Reset all variables
clear all
a = zeros(3,4)
size(a)
a(1,1) = 1
% Setting a range
a(3,3:4) = 1
% Linear indexing
a(3:5) = 1
% Returns array of indices
find(a == 1)
% use to set
a(find(a == 1)) = 2

% Practical example
img = imread('../../Images/pienta.jpg');
imtool(img)
find(img(:,:,3) > 100); % show
% how many?
length(find(img(:,:,3) > 100))
% Show creating a matrix of 0's or 1's
mask = zeros(size(img,1), size(img,2));
whos
mask = ones(size(img,1), size(img,2));
% Mask of the face only
% Note the use of logical operators.
% & (and), | (or), ~ (not)
mask(find(img(:,:,1) < 100 ... % ... = continuation
    & img(:,:,2) < 150 ...
    & img(:,:,3) > 170)) = 0;
%Alternative:
%mask = img(:,:,1) < 100 & img(:,:,2) < 150 & img(:,:,3) > 170;
%mask = 1 - mask
imshow(mask)
imwrite(mask, 'raw_mask.png')
% Black out the background
%face = mask .* img; % doesn't work
%face = mask .* double(img)
mask3D = zeros(size(img));
for i = 1:3
    mask3D(:,:,i) = mask;
end
face = mask3D .* double(img);
imtool(face)
whos
imtool(uint8(face))
imwrite(uint8(face), 'applied_mask.png')
