function Lab01_ImgHistContrast_c()
    clc; clear; close all;
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%   Images - read and plot histogram
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    % Read and show a RGB image    
    clc; clear; close all;
    figure('Name','Show image','NumberTitle','off');
    Img1 = imread('appleColor.jpg');
    whos Img1
    imshow(Img1)
    
    % Read a RGB image, show a gryscale image   
    clc; clear; close all;
    figure('Name','Show image','NumberTitle','off');
    Img1 = imread('appleColor.jpg');
    Img2 = rgb2gray(Img1);
    whos Img2
    imshow(Img2)
    title(['imread - range of values: [', num2str(min(Img2(:))), ', ', num2str(max(Img2(:))), ']']) 
    Img3 = im2double(Img2(:,:,1));
    whos Img3
    imshow(Img3)
    title(['im2double - range of values [', num2str(min(Img3(:))), ', ', num2str(max(Img3(:))), ']']) 
       

    % Read and show a gryscale image    
    clc; clear; close all;
    figure('Name','Show image','NumberTitle','off');
    Img1 = imread('img02.png');
    Img1 = Img1(:,:,1);
    whos Img1   
    ['min = ' num2str(min(Img1(:))) ', max = ' num2str(max(Img1(:)))]
    
    Img1 = double(Img1(:,:,1));
    whos Img1
    ['min = ' num2str(min(Img1(:))) '; max = ' num2str(max(Img1(:)))]
    [m,n] = size(Img1);
    
    subplot(1,2,1); imshow(Img1,[0,255]); 
    title(['Matrix ', num2str(m), ' x ', num2str(n)])    
    subplot(1,2,2); imshow(Img1',[0,255]); 
    title(['Matrix ', num2str(n), ' x ', num2str(m)])
        
    
    % Histogram 
    clc; clear; close all;    
    figure('Name','Histogram','NumberTitle','off');
    Img1 = imread('img02.png');    
    Img1 = (Img1(:,:,1));   
    [m,n] = size(Img1);
    myHist = zeros(256,1);
    
    for i = 1:m
        for j = 1:n
            myHist( Img1(i,j)+1 ) = myHist( Img1(i,j)+1 ) + 1; 
        end
    end    
    
    subplot(1,2,1); imshow(Img1,[0,255]); title('Original image [0,255]')    
    subplot(1,2,2); bar(myHist); title('my histogram'); ylim([0 max(myHist)*1.01]);
    
    
    
    % Histogram 
    clc; clear; close all;    
    figure('Name','Histogram','NumberTitle','off');
    Img1 = imread('img02.png');    
    Img1 = (Img1(:,:,1));   
    [m,n] = size(Img1);
    myHist = zeros(256,1);
    
    for i = 1:m
        for j = 1:n
            myHist( Img1(i,j)+1 ) = myHist( Img1(i,j)+1 ) + 1; 
        end
    end    
    
    [counts,binLocations] = imhist(Img1);
    
    subplot(1,3,1); imshow(Img1,[0,255]); title('Original image [0,255]' )    
    subplot(1,3,2); bar(myHist); title('my histogram'); ylim([0 max(myHist)*1.01]);
    subplot(1,3,3); bar(counts); title('Histogram'); ylim([0 max(myHist)*1.01]);
    
    sum(counts-myHist)
    
    
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %   Point operations
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    %% Brightness, Brightness substraction ********************************************
    clc; clear; close all;
    br = -0.50;
    figure('Name','Show image','NumberTitle','off');
    Img1 = imread('img02.png');    
    Img1 = im2double(Img1(:,:,1));   
    [m,n] = size(Img1);
    mat = zeros(m,n);  
%     mat = Img1+br;     
    for i = 1:m
        for j = 1:n
            mat(i,j) = Img1(i,j)+br; 
        end
    end   
    
    subplot(1,2,1); imshow(Img1,[0,1]); title('Original image [0,1]')    
    subplot(1,2,2); imshow(mat,[0,1]); title(['New image, Brightness=',num2str(br)])
    
    
    %% Threshold ********************************************
    clc; clear; close all;
    th = 0.25*255;
    figure('Name','Show image','NumberTitle','off');
    Img1 = imread('img02.png');    
    Img1 = double(Img1(:,:,1));  
    [m,n] = size(Img1);
    mat = zeros(m,n);  
%     mat = Img1 < th;    
    for i = 1:m
        for j = 1:n
            if Img1(i,j) < th
                mat(i,j) = 0;
            else
                mat(i,j) = 255;
            end
        end
    end
    
    subplot(1,2,1); imshow(Img1,[0,255]); title('Original image [0,255]')    
    subplot(1,2,2); imshow(mat,[0,255]); title(['New image, Threshold=',num2str(th)])
    
    
    %% Contrast ********************************************
    clc; clear; close all;
    cr = 1.2;
    figure('Name','Show image','NumberTitle','off');
    Img1 = imread('img02.png');    
    Img1 = double(Img1(:,:,1));
    [m,n] = size(Img1);
    mat = zeros(m,n);  
%     mat = (Img1 - 0.5) * cr + 0.5;    
    for i = 1:m
        for j = 1:n
            mat(i,j) = (Img1(i,j) - 0.5*255) * cr + 0.5*255; 
        end
    end
    
    subplot(1,2,1); imshow(Img1,[0,255]); title('Original image [0,255]')    
    subplot(1,2,2); imshow(mat,[0,255]); title(['New image, Contrast=',num2str(cr)])
    
   
    
    
    %% Clamping ********************************************
    clc; clear; close all;
    a = floor(0.25*255);
    b = floor(0.75*255);
    figure('Name','Show image','NumberTitle','off');
    Img1 = imread('img02.png');    
    Img1 = double(Img1(:,:,1));  
    [m,n] = size(Img1);
    mat = zeros(m,n);  
    
    for i = 1:m
        for j = 1:n
            if Img1(i,j) < a
                mat(i,j) = a;
            elseif b < Img1(i,j)
                mat(i,j) = b;
            else
                mat(i,j) = Img1(i,j);
            end
        end
    end
    
    subplot(1,2,1); imshow(Img1,[0,255]); title('Original image [0,255]')    
    subplot(1,2,2); imshow(mat,[0,255]); title({'New image', ['Clamping=[',num2str(a),',',num2str(b),']']})
    
    
    
    %% Intensity Windowing ********************************************
    clc; clear; close all;
    a = floor(0.25*255);
    b = floor(0.75*255);
    M = 255;
    figure('Name','Show image','NumberTitle','off');
    Img1 = imread('img02.png');    
    Img1 = double(Img1(:,:,1));  
    [m,n] = size(Img1);
    mat = zeros(m,n);  
    
    for i = 1:m
        for j = 1:n
            if Img1(i,j) < a
                mat(i,j) = 0;
            elseif b < Img1(i,j)
                mat(i,j) = M;
            else
                mat(i,j) = M * (Img1(i,j) - a) / (b - a);
            end
        end
    end
    
    subplot(1,2,1); imshow(Img1,[0,255]); title('Original image [0,255]')    
    subplot(1,2,2); imshow(mat,[0,255]); title({'Windowed Image',[ 'Range=[',num2str(a),',',num2str(b),'] M=',num2str(M)]})
    
    
    
    
    %% Histogram equalization Matlab ********************************************
    clc; clear; close all;    
    figure('Name','Show image','NumberTitle','off');
    Img1 = imread('img02.png');    
    Img1 = (Img1(:,:,1));   
    [m,n] = size(Img1);        
    
    % Compute the histogram  
    [myHist,binLocations] = imhist(Img1);
    
    % Normalize histogram
    myHistNorm = myHist/(m*n);
    
    % Cumulative histogram 
    myHistCum = cumsum(myHistNorm);
    
    subplot(2,3,1); imshow(Img1,[0,255]); title('Original image')    
    subplot(2,3,2); bar(myHist); title('Histogram'); ylim([0 max(myHist)*1.2]);
    subplot(2,3,3); plot(myHistCum); title('Cumulative histogram '); 
        
    Img3 = histeq(Img1); % Histogram equalization
    % Compute the histogram 
    [counts,binLocations] = imhist(Img3);
    
    subplot(2,3,4); imshow(Img3,[0,255]); title('Image with EH matlab')    
    subplot(2,3,5); bar(counts); title('EH matlab'); ylim([0 max(myHist)*1.2]);
    
    % Normalize histogram
    myHistNorm3 = counts/(m*n);
    
    % Cumulative histogram 
    myHistCum3 = cumsum(myHistNorm3);
    
    subplot(2,3,6); plot(myHistCum3); title('Cumulative EH matlab'); 
    

    %%%%% ******************************************** Solutions ******************************************** %%%%%
    %% Real-valued functions ********************************************
    clc; clear; %close all;    
    figure('Name','Show image','NumberTitle','off');
    Img1 = imread('img02.png');    
    Img1 = im2double(Img1(:,:,1));
    [m,n] = size(Img1);

    matExp = exp(Img1); % enhancing contrast of brighter regions 
    matLog = log(Img1*255); % reducing contrast of brighter regions 
    matInv = 1./(Img1);
    matPower2 = Img1.^2;
    matInverted = max(Img1(:)) - Img1;

    Img2 = mat2gray(matExp);
    Img3 = mat2gray(matLog);
    Img4 = mat2gray(matInv);
    Img5 = mat2gray(matPower2);
    Img6 = mat2gray(matInverted);

    subplot(2,3,1); imshow(Img1); title('Original image [0,1]')    
    subplot(2,3,2); imshow(Img2); title('exp(x)')
    subplot(2,3,3); imshow(Img3); title('ln(x*255)')    
    subplot(2,3,4); imshow(Img4); title('1/x')
    subplot(2,3,5); imshow(Img5); title('x^2')    
    subplot(2,3,6); imshow(Img6); title('1-x')
     
    %% My Histogram equalization  ********************************************
    figure('Name','Show image','NumberTitle','off');
    Img1 = imread('img02.png');    
    Img1 = (Img1(:,:,1));   
    [m,n] = size(Img1);    
    myHist = zeros(1,256);
    myHistCum = zeros(1,256);
    % Compute the histogram
    for i = 1:m
     for j = 1:n
      myHist( Img1(i,j)+1 ) = myHist( Img1(i,j)+1 ) + 1; 
     end
    end   
    % Normalize histogram
    myHistNorm = myHist/(m*n);
    % Cumulative histogram 
    tmp = 0;
    for i = 1:256
     tmp = tmp + myHistNorm(i);
     myHistCum(i) = tmp;
    end
    % Compute image with histogram equalized
    Img2 = zeros(m,n,'uint8');
    for i = 1:m
     for j = 1:n
      Img2(i,j) = uint8(255*myHistCum( Img1(i,j)+1 )); 
     end
    end    
    subplot(2,3,1); imshow(Img1,[0,255]); title('Original image')    
    subplot(2,3,2); bar(myHist); title('my histogram'); ylim([0 max(myHist)*1.2]);
    subplot(2,3,3); plot(myHistCum); title('Cumulative histogram '); 
    myHist2 = zeros(1,256);
    myHistCum2 = zeros(1,256);
    % Compute the histogram
    for i = 1:m
     for j = 1:n
      myHist2( Img2(i,j)+1 ) = myHist2( Img2(i,j)+1 ) + 1; 
     end
    end  
    % Normalize histogram
    myHistNorm2 = myHist2/(m*n);
    % Cumulative histogram 
    tmp = 0;
    for i = 1:256
     tmp = tmp + myHistNorm2(i);
     myHistCum2(i) = tmp;
    end
    subplot(2,3,4); imshow(Img2,[0,255]); title('Image with EH')    
    subplot(2,3,5); bar(myHist2); title('my EH'); ylim([0 max(myHist2)*1.2]);
    subplot(2,3,6); plot(myHistCum2); title('My Cumulative EH '); 



end 
