function Lab04_Segmentation_c()
    clc; clear; close all;
   
    %% Otsu's method  
    I_1 = imread('coins.png');
    I_2 = imread('eighttif.png');
    I_2 = I_2(:,:,1);
    I_3 = imread('riceG.png');
    
    % Get the threshold  
    otsuLevel_I_1 = graythresh(I_1);
    otsuLevel_I_2 = graythresh(I_2);
    otsuLevel_I_3 = graythresh(I_3);
    
    % Compute binary image using the threshold
    blackWhite_I_1 = imbinarize(I_1,otsuLevel_I_1);
    blackWhite_I_2_4 = imbinarize(I_2,otsuLevel_I_2);
    blackWhite_I_3 = imbinarize(I_3,otsuLevel_I_3);
    
    % Histogram
    [myHist_1,binLocations] = imhist(I_1);
    [myHist_2,binLocations] = imhist(I_2);
    [myHist_3,binLocations] = imhist(I_3);
    
    nr = 3;
    nc = 3; 
    FontSizeVal = 10;
    subplot(nr, nc, 1); imshow(I_1); title('Original Image 1','FontSize',FontSizeVal);
    subplot(nr, nc, 2); imshow(blackWhite_I_1); title({'Image 1',[' threshold=',num2str(otsuLevel_I_1*255)]},'FontSize',FontSizeVal); 
    subplot(nr, nc, 3); plot(0:255,myHist_1); title({'Histogram',' Image 1',[' threshold=',num2str(otsuLevel_I_1*255)]},'FontSize',FontSizeVal); xlim([-5 260]);
    hold on; plot([otsuLevel_I_1*255,otsuLevel_I_1*255],[0, max(myHist_1)]); hold off;
    
    subplot(nr, nc, 4); imshow(I_2); title('Original Image 2','FontSize',FontSizeVal);
    subplot(nr, nc, 5); imshow(blackWhite_I_2_4); title({'Image 2',[' threshold=',num2str(otsuLevel_I_2*255)]},'FontSize',FontSizeVal); 
    subplot(nr, nc, 6); plot(myHist_2); title({'Histogram',' Image 2',[' threshold=',num2str(otsuLevel_I_2*255)]},'FontSize',FontSizeVal); xlim([-5 260]);
    hold on; plot([otsuLevel_I_2*255,otsuLevel_I_2*255],[0, max(myHist_2)]); hold off;
    
    subplot(nr, nc, 7); imshow(I_3); title('Original Image 3','FontSize',FontSizeVal);
    subplot(nr, nc, 8); imshow(blackWhite_I_3); title({'Image 3',[' threshold=',num2str(otsuLevel_I_3*255)]},'FontSize',FontSizeVal); 
    subplot(nr, nc, 9); plot(myHist_3); title({'Histogram',' Image 3',[' threshold=',num2str(otsuLevel_I_3*255)]},'FontSize',FontSizeVal);  xlim([-5 260]);
    hold on; plot([otsuLevel_I_3*255,otsuLevel_I_3*255],[0, max(myHist_3)]); hold off;
    
    %% Subdivision       
    I_1 = imread('riceG.png');
        
    otsuLevel_I_1 = graythresh(I_1);   
    blackWhite_I_1 = imbinarize(I_1,otsuLevel_I_1);
    
    [m,n] = size(I_1);
    
    % Subdivision image 1x1 -> 2x2 subimages
    I_2_cells = mat2cell(I_1,[m/2,m/2],[n/2,n/2]);
    otsuLevel_I_2_cells = [];
    blackWhite_I_2_cells = {};
    % For all subimages 
    for i=1:2
       for j=1:2
           % Get the threshold 
           otsuLevel_I_2_cells(i,j) = graythresh(I_2_cells{i,j}); 
           % Compute binary image using the threshold
           blackWhite_I_2_cells{i,j} = imbinarize(I_2_cells{i,j},otsuLevel_I_2_cells(i,j));           
       end
    end    
    % Union subimage 2x2 -> 1x1 Image
    blackWhite_I_2_2 = cell2mat(blackWhite_I_2_cells);    
    
    
    % Subdivision image 1x1 -> 4x4 subimages
    I_2_cells = mat2cell(I_1,[m/4,m/4,m/4,m/4],[n/4,n/4,n/4,n/4]);
    otsuLevel_I_2_cells = [];
    blackWhite_I_2_cells = {};    
    % For all subimages 
    for i=1:4
       for j=1:4
           % Get the threshold 
           otsuLevel_I_2_cells(i,j) = graythresh(I_2_cells{i,j}); 
           % Compute binary image using the threshold
           blackWhite_I_2_cells{i,j} = imbinarize(I_2_cells{i,j},otsuLevel_I_2_cells(i,j));           
       end
    end    
    % Union subimage 4x4 -> 1x1 Image
    blackWhite_I_2_4 = cell2mat(blackWhite_I_2_cells);
    
    nr = 3;
    nc = 2;  
    subplot(nr, nc, 1); imshow(I_1); title('Original Image 1');
    subplot(nr, nc, 2); imshow(blackWhite_I_1); title(['Image 1, threshold=',num2str(otsuLevel_I_1*255)]); 
    subplot(nr, nc, 3); imshow(I_1); title('Image 1 subdivision 2x2'); addLines(m,n,2)
    subplot(nr, nc, 4); imshow(blackWhite_I_2_2); title({['Image 2, threshold'],' subdivision 2x2'}); addLines(m,n,2)
    subplot(nr, nc, 5); imshow(I_1); title('Image 1 subdivision 4x4'); addLines(m,n,4)
    subplot(nr, nc, 6); imshow(blackWhite_I_2_4); title({['Image 2, threshold'],' subdivision 4x4'}); addLines(m,n,4)
    
    
    %% Region growing
    I_1 = im2double(imread('coins.png'));
    
    % Seedpoints
    x1=50; y1=50;
    reg_maxdist = 0.2;
    J1 = regiongrowing(I_1,x1,y1,reg_maxdist);
    
    x2=100; y2=100;
    reg_maxdist = 0.25;
    J2 = regiongrowing(I_1,x2,y2,reg_maxdist);
    
    x3=180; y3=240;
    reg_maxdist = 0.3;
    J3 = regiongrowing(I_1,x3,y3,reg_maxdist);
    
    x4=10; y4=10;
    reg_maxdist = 0.2;
    J4 = regiongrowing(I_1,x4,y4,reg_maxdist);
    
    nr = 2;
    nc = 2;  
    subplot(nr, nc, 1); imshow(I_1); title('Original Image 1');
    subplot(nr, nc, 2); imshow(I_1+J1+J2+J3); title({'Image 1',' region growing = [ J1 + J2 + J3 ]'}); 
    hold on; plot([y1,y2,y3],[x1,x2,x3],'ro','MarkerFaceColor', 'r'); hold off;
    
    subplot(nr, nc, 3); imshow(I_1); title('Original Image 1');
    subplot(nr, nc, 4); imshow(I_1+J4); title({'Image 1',' region growing = [ J4 ]'}); 
    hold on; plot([y4],[x4],'ro','MarkerFaceColor', 'r'); hold off;
   
    %% Clustering K-Means 
    rng(4); % Try changing it!!    
    I_1 = rgb2gray(im2double(imread('univrLogo.jpeg')));
    
    I_1 = imgaussfilt(I_1,1); % Image Denoising
    [m,n] = size(I_1); 
    
    [idx,C] = kmeans(I_1(:),2);
    I_1_k2 =  reshape(idx, m,n);
    
    [idx,C] = kmeans(I_1(:),3);
    I_1_k3 =  reshape(idx, m,n);
    
    [idx,C] = kmeans(I_1(:),4);
    I_1_k4 =  reshape(idx, m,n);
    
    nr = 3;
    nc = 2;  
    subplot(nr, nc, 3); imshow(I_1); title('Original Image');
    subplot(nr, nc, 2); imshow(label2rgb(I_1_k2), [min(I_1_k2(:)) max(I_1_k2(:))]); title('2 Clusters'); 
    subplot(nr, nc, 4); imshow(label2rgb(I_1_k3), [min(I_1_k3(:)) max(I_1_k3(:))]); title('3 Clusters');
    subplot(nr, nc, 6); imshow(label2rgb(I_1_k4), [min(I_1_k4(:)) max(I_1_k4(:))]); title('4 Clusters');
    
    %% Clustering K-Means color
    rng(1); % Try changing it!!
    I_1 = im2double(imread('univrLogo.jpeg'));
    
    R = I_1(:, :, 1);R = R(:);
    G = I_1(:, :, 2);G = G(:);
    B = I_1(:, :, 3);B = B(:);
    
    vecRGB = [R G B];
    
    [m,n,o] = size(I_1);

    [idx,C] = kmeans(vecRGB,2);
    I_1_k2 =  reshape(idx, m,n);

    [idx,C] = kmeans(vecRGB,3);
    I_1_k3 =  reshape(idx, m,n);
    
    [idx,C] = kmeans(vecRGB,4);
    I_1_k4 =  reshape(idx, m,n);
    
    nr = 3;
    nc = 2;  
    subplot(nr, nc, 3); imshow(I_1); title('Original Image RGB');
    subplot(nr, nc, 2); imshow(label2rgb(I_1_k2), [min(I_1_k2(:)) max(I_1_k2(:))]); title('2 Clusters'); 
    subplot(nr, nc, 4); imshow(label2rgb(I_1_k3), [min(I_1_k3(:)) max(I_1_k3(:))]); title('3 Clusters');
    subplot(nr, nc, 6); imshow(label2rgb(I_1_k4), [min(I_1_k4(:)) max(I_1_k4(:))]); title('4 Clusters');
    
    %% Create nii file
% if the function niftiwrite is not installed add the next libraries 
%     addpath(genpath('iptformats'));
%     addpath(genpath('nifti'));
    I_1 = imread('coins.png');
    
    % Create 3D matrix
    B = I_1;
    for i=1:25
        B = cat(3, B, I_1);
    end 
    niftiwrite(B,'coins.nii')
    
    %%%%% ******************************************** Solutions ******************************************** %%%%%
    %% Clustering my K-Means      
    rng(2); % Try changing it!!
    I_1 = im2double(imread('coins.png'));
    I_1 = imgaussfilt(I_1,1);
    [m,n] = size(I_1);

    [idx] = myKmeans(I_1(:),2);
    I_1_k2 =  reshape(idx, m,n);

    [idx] = myKmeans(I_1(:),3);
    I_1_k3 =  reshape(idx, m,n);
    rng(6);
    [idx] = myKmeans(I_1(:),4);
    I_1_k4 =  reshape(idx, m,n);
    nr = 3;
    nc = 2;  
    subplot(nr, nc, 3); imshow(I_1); title('Original Image');
    subplot(nr, nc, 2); imshow(label2rgb(I_1_k2), [min(I_1_k2(:)) max(I_1_k2(:))]); title('2 Clusters'); 
    subplot(nr, nc, 4); imshow(label2rgb(I_1_k3), [min(I_1_k3(:)) max(I_1_k3(:))]); title('3 Clusters');
    subplot(nr, nc, 6); imshow(label2rgb(I_1_k4), [min(I_1_k4(:)) max(I_1_k4(:))]); title('4 Clusters');

    %% Clustering my K-Means color 
    rng(1); % Try changing it!!
    I_1 = im2double(imread('univrLogo.jpeg'));
    R = I_1(:, :, 1);R = R(:);
    G = I_1(:, :, 2);G = G(:);
    B = I_1(:, :, 3);B = B(:);

    vecRGB = [R G B];
    [m,n,o] = size(I_1);

    [idx] = myKmeansColor(vecRGB,2);
    I_1_k2 =  reshape(idx, m,n);

    [idx] = myKmeansColor(vecRGB,3);
    I_1_k3 =  reshape(idx, m,n);

    [idx] = myKmeansColor(vecRGB,4);
    I_1_k4 =  reshape(idx, m,n);
    nr = 3;
    nc = 2;  
    subplot(nr, nc, 3); imshow(I_1); title('Original Image');
    subplot(nr, nc, 2); imshow(label2rgb(I_1_k2)); title('2 Clusters'); 
    subplot(nr, nc, 4); imshow(label2rgb(I_1_k3)); title('3 Clusters');
    subplot(nr, nc, 6); imshow(label2rgb(I_1_k4)); title('4 Clusters');		
    
end 



