function Lab05_MorphologicalOperators_c()
    clc; clear; close all;
   
    %% Erosion 
    clc; clear; close all;
    I_1 = rgb2gray(imread('duck.png')); 
    I_1 = I_1>50;
    
    % Structuring element
    SE_1 = ones(9);  % Morphological structuring element
    SE_2 = eye(9);             % Matrix
    SE_3 = ones(9,1);
%     SE_4 = zeros(9);SE_4(:,5)=1; SE_4(5,:)=1; 
  
    % Erosion
    BW1 = imerode(I_1,SE_1);
    BW2 = imerode(I_1,SE_2);
    BW3 = imerode(I_1,SE_3);
    
    nr = 3;
    nc = 3; 
    FontSizeVal = 10;
    subplot(nr, nc, 2); imshow(I_1); title('Binary image','FontSize',FontSizeVal);
    
    subplot(nr, nc, 4); imshow(SE_1); title('SE 1','FontSize',FontSizeVal); setGrid(SE_1);
    subplot(nr, nc, 5); imshow(SE_2); title('SE 2','FontSize',FontSizeVal); setGrid(SE_2);
    subplot(nr, nc, 6); imshow(SE_3); title('SE 3','FontSize',FontSizeVal); setGrid(SE_3); 
    
    subplot(nr, nc, 7); imshow(BW1); title('Erosion 1','FontSize',FontSizeVal); 
    subplot(nr, nc, 8); imshow(BW2); title('Erosion 2','FontSize',FontSizeVal); 
    subplot(nr, nc, 9); imshow(BW3); title('Erosion 3','FontSize',FontSizeVal); 
    
    %% Dilation 
%     clc; clear; close all;
    I_1 = rgb2gray(imread('duck.png')); 
    I_1 = I_1>50;
    
    % Structuring element
    SE_1 = ones(9);  
    SE_2 = eye(9);   
    SE_3 = ones(9,1);
%     SE_4 = zeros(9);SE_4(:,5)=1; SE_4(5,:)=1; 
    
    % Dilation
    BW1 = imdilate(I_1,SE_1);
    BW2 = imdilate(I_1,SE_2);
    BW3 = imdilate(I_1,SE_3);
        
    nr = 3;
    nc = 3; 
    FontSizeVal = 10;
    subplot(nr, nc, 2); imshow(I_1); title('Binary image','FontSize',FontSizeVal);
    
    subplot(nr, nc, 4); imshow(SE_1); title('SE 1','FontSize',FontSizeVal); setGrid(SE_1);
    subplot(nr, nc, 5); imshow(SE_2); title('SE 2','FontSize',FontSizeVal); setGrid(SE_2);
    subplot(nr, nc, 6); imshow(SE_3); title('SE 3','FontSize',FontSizeVal); setGrid(SE_3); 
    
    subplot(nr, nc, 7); imshow(BW1); title('Dilation 1','FontSize',FontSizeVal); 
    subplot(nr, nc, 8); imshow(BW2); title('Dilation 2','FontSize',FontSizeVal); 
    subplot(nr, nc, 9); imshow(BW3); title('Dilation 3','FontSize',FontSizeVal); 
    
    %% Opening (erosion of A by B followed by a dilation of the result by B)
%     clc; clear; close all;
    I_1 = rgb2gray(imread('duck.png')); 
    I_1 = I_1>50;
    
    % Structuring element
    SE_1 = ones(9);   
    SE_2 = eye(9);             
    SE_3 = ones(9,1);
%     SE_4 = zeros(9);SE_4(:,5)=1; SE_4(5,:)=1; 
    
    % Opening
    BW1 = imopen(I_1,SE_1);
    BW2 = imopen(I_1,SE_2);
    BW3 = imopen(I_1,SE_3);
    
    nr = 3;
    nc = 3; 
    FontSizeVal = 10;
    subplot(nr, nc, 2); imshow(I_1); title('Binary image','FontSize',FontSizeVal);
    
    subplot(nr, nc, 4); imshow(SE_1); title('SE 1','FontSize',FontSizeVal); setGrid(SE_1);
    subplot(nr, nc, 5); imshow(SE_2); title('SE 2','FontSize',FontSizeVal); setGrid(SE_2);
    subplot(nr, nc, 6); imshow(SE_3); title('SE 3','FontSize',FontSizeVal); setGrid(SE_3); 
    
    subplot(nr, nc, 7); imshow(BW1); title('Opening 1','FontSize',FontSizeVal); 
    subplot(nr, nc, 8); imshow(BW2); title('Opening 2','FontSize',FontSizeVal); 
    subplot(nr, nc, 9); imshow(BW3); title('Opening 3','FontSize',FontSizeVal); 
    
    %% Closing (dilation of A by B followed by an erosion of the result by B)
%     clc; clear; close all;
    I_1 = rgb2gray(imread('duck.png')); 
    I_1 = I_1>50;
    
    % Structuring element
    SE_1 = ones(9);   
    SE_2 = eye(9);             
    SE_3 = ones(9,1);
%     SE_4 = zeros(9);SE_4(:,5)=1; SE_4(5,:)=1; 
    
    % Closing
    BW1 = imclose(I_1,SE_1);
    BW2 = imclose(I_1,SE_2);
    BW3 = imclose(I_1,SE_3);
    
    nr = 3;
    nc = 3; 
    FontSizeVal = 10;
    subplot(nr, nc, 2); imshow(I_1); title('Binary image','FontSize',FontSizeVal);
    
    subplot(nr, nc, 4); imshow(SE_1); title('SE 1','FontSize',FontSizeVal); setGrid(SE_1);
    subplot(nr, nc, 5); imshow(SE_2); title('SE 2','FontSize',FontSizeVal); setGrid(SE_2);
    subplot(nr, nc, 6); imshow(SE_3); title('SE 3','FontSize',FontSizeVal); setGrid(SE_3); 
    
    subplot(nr, nc, 7); imshow(BW1); title('Closing 1','FontSize',FontSizeVal); 
    subplot(nr, nc, 8); imshow(BW2); title('Closing 2','FontSize',FontSizeVal); 
    subplot(nr, nc, 9); imshow(BW3); title('Closing 3','FontSize',FontSizeVal); 
    
    %% Boundary extraction
%     clc; clear; close all;
    I_1 = rgb2gray( imread('perfil2.png'));
    I_1 = I_1>150;
    
    % Structuring element
    SE_1 = ones(3);             
    
    % Dilation
    BW1 = imdilate(I_1,SE_1);
    
    boundary_01 = I_1 ~= BW1;
    
    nr = 1;
    nc = 3; 
    FontSizeVal = 10;
    subplot(nr, nc, 1); imshow(I_1); title('Binary image','FontSize',FontSizeVal);
    subplot(nr, nc, 2); imshow(SE_1); title('SE','FontSize',FontSizeVal); setGrid(SE_1);
    subplot(nr, nc, 3); imshow(boundary_01); title('Border','FontSize',FontSizeVal); 
    
    %% Skeleton 01
%     clc; clear; close all;
    I_1 = imread('circles.png');
    I_1 = I_1(:,:,1);
    
    % Skeleton
    Img_skel = bwmorph(I_1,'skel',Inf);
    % Structuring element    
    SE_1 = ones(3);      
    % Dilation
    BW1 = imdilate(Img_skel,SE_1);
    % Binary image + Skeleton
    Img_skel_fc = imfuse(I_1, Img_skel);
    % Binary image + Skeleton + Dilation
    BW1_fc = imfuse(I_1, BW1);
    
    nr = 1;
    nc = 4; 
    FontSizeVal = 10;
    subplot(nr, nc, 1); imshow(I_1); title('Binary image','FontSize',FontSizeVal);
    subplot(nr, nc, 2); imshow(Img_skel); title('Skeleton','FontSize',FontSizeVal); 
    subplot(nr, nc, 3); imshow(Img_skel_fc); title('Binary image + Skeleton','FontSize',FontSizeVal);     
    subplot(nr, nc, 4); imshow(BW1_fc); title('Binary image + Skeleton + Dilation','FontSize',FontSizeVal); 
    
    %%%%% ******************************************** Solutions ******************************************** %%%%%
     %% myOpening (erosion of A by B followed by a dilation of the result by B)
    clc; clear; close all;
    I_1 = rgb2gray(imread('duck.png')); 
    I_1 = I_1>50;
    
    % Structuring element
    SE_1 = ones(9);   
    SE_2 = eye(9);             
    SE_3 = ones(9,1);
%     SE_4 = zeros(9);SE_4(:,5)=1; SE_4(5,:)=1; 
    
    % Opening
    BW1 = myOpening(I_1,SE_1);
    BW2 = myOpening(I_1,SE_2);
    BW3 = myOpening(I_1,SE_3);
    
    nr = 3;
    nc = 3; 
    FontSizeVal = 10;
    subplot(nr, nc, 2); imshow(I_1); title('Binary image','FontSize',FontSizeVal);
    
    subplot(nr, nc, 4); imshow(SE_1); title('SE 1','FontSize',FontSizeVal); setGrid(SE_1);
    subplot(nr, nc, 5); imshow(SE_2); title('SE 2','FontSize',FontSizeVal); setGrid(SE_2);
    subplot(nr, nc, 6); imshow(SE_3); title('SE 3','FontSize',FontSizeVal); setGrid(SE_3); 
    
    subplot(nr, nc, 7); imshow(BW1); title('myOpening 1','FontSize',FontSizeVal); 
    subplot(nr, nc, 8); imshow(BW2); title('myOpening 2','FontSize',FontSizeVal); 
    subplot(nr, nc, 9); imshow(BW3); title('myOpening 3','FontSize',FontSizeVal); 
    
    %% myClosing (dilation of A by B followed by an erosion of the result by B)
    clc; clear; close all;
    I_1 = rgb2gray(imread('duck.png')); 
    I_1 = I_1>50;
    
    % Structuring element
    SE_1 = ones(9);  
    SE_2 = eye(9);             
    SE_3 = ones(9,1);
%     SE_4 = zeros(9);SE_4(:,5)=1; SE_4(5,:)=1; 
    
    % Closing
    BW1 = myClosing(I_1,SE_1);
    BW2 = myClosing(I_1,SE_2);
    BW3 = myClosing(I_1,SE_3);
    
    nr = 3;
    nc = 3; 
    FontSizeVal = 10;
    subplot(nr, nc, 2); imshow(I_1); title('Binary image','FontSize',FontSizeVal);
    
    subplot(nr, nc, 4); imshow(SE_1); title('SE 1','FontSize',FontSizeVal); setGrid(SE_1);
    subplot(nr, nc, 5); imshow(SE_2); title('SE 2','FontSize',FontSizeVal); setGrid(SE_2);
    subplot(nr, nc, 6); imshow(SE_3); title('SE 3','FontSize',FontSizeVal); setGrid(SE_3); 
    
    subplot(nr, nc, 7); imshow(BW1); title('myClosing 1','FontSize',FontSizeVal); 
    subplot(nr, nc, 8); imshow(BW2); title('myClosing 2','FontSize',FontSizeVal); 
    subplot(nr, nc, 9); imshow(BW3); title('myClosing 3','FontSize',FontSizeVal); 
    
    
end



