function Lab06_FeatureExtraction()
    
    clc; clear; close all;
    
    
    %% Sobel
    clc; clear; close all;
    Img = imread('circuit.tif');
    Img = imread('arch-window_bw.jpeg');
    [m,n] = size(Img);
    % Img, 'Sobel', threshold, 'horizontal' or 'vertical'
    [BW1, threshOut] = edge(Img,'Sobel'); % Automatically    
    BW2 = edge(Img,'Sobel',0.3);
    BW3 = edge(Img,'Sobel','horizontal');
    BW4 = edge(Img,'Sobel','vertical');

    
    figure;
    nr = 2;
    nc = 3; 
    FontSizeVal = 10;
    subplot(nr, nc, 1); imshow(Img); title('Image','FontSize',FontSizeVal);
    subplot(nr, nc, 2); imshow(BW1); title('Sobel (automatic)','FontSize',FontSizeVal);
    subplot(nr, nc, 3); imshow(BW2); title('Sobel (th=0.3)','FontSize',FontSizeVal);    
    subplot(nr, nc, 5); imshow(BW3); title('Sobel (horizontal)','FontSize',FontSizeVal);    
    subplot(nr, nc, 6); imshow(BW4); title('Sobel (vertical)','FontSize',FontSizeVal);

    %% Prewitt
    clc; clear; close all;
    Img = imread('circuit.tif');
    Img = imread('arch-window_bw.jpeg');
    [m,n] = size(Img);
    % Img, 'Prewitt', threshold, 'horizontal' or 'vertical'
    [BW1, threshOut] = edge(Img,'Sobel'); % Automatically    
    BW2 = edge(Img,'Prewitt',0.3);
    BW3 = edge(Img,'Prewitt','horizontal');
    BW4 = edge(Img,'Prewitt','vertical');

    
    figure;
    nr = 2;
    nc = 3; 
    FontSizeVal = 10;
    subplot(nr, nc, 1); imshow(Img); title('Image','FontSize',FontSizeVal);
    subplot(nr, nc, 2); imshow(BW1); title('Prewitt (automatic)','FontSize',FontSizeVal);
    subplot(nr, nc, 3); imshow(BW2); title('Prewitt (th=0.3)','FontSize',FontSizeVal);    
    subplot(nr, nc, 5); imshow(BW3); title('Prewitt (horizontal)','FontSize',FontSizeVal);    
    subplot(nr, nc, 6); imshow(BW4); title('Prewitt (vertical)','FontSize',FontSizeVal);

    
    %% Canny
    clc; clear; close all;
    Img = imread('circuit.tif');
    Img = imread('arch-window_bw.jpeg');
    [m,n] = size(Img);
    % Img, 'Canny', threshold of derivative [min max], sigma of Gaussian filter 
    [BW1, threshOut] = edge(Img,'Canny'); % Automatically    
    BW2 = edge(Img,'Canny',[0.4,0.5]);
    BW3 = edge(Img,'Canny',[0.4,0.5],4);
    
    
    figure;
    nr = 2;
    nc = 2; 
    FontSizeVal = 10;
    subplot(nr, nc, 1); imshow(Img); title('Image','FontSize',FontSizeVal);
    subplot(nr, nc, 2); imshow(BW1); title('Canny (automatic)','FontSize',FontSizeVal);
    subplot(nr, nc, 3); imshow(BW2); title('Canny (th=[0.4,0.6])','FontSize',FontSizeVal);    
    subplot(nr, nc, 4); imshow(BW3); title('Canny (\sigma = 4)','FontSize',FontSizeVal);


    BWn = zeros(m,n,3,3);
    BWn_fc = zeros(m,n,3,3,3,'uint8');
    
    figure;
    nr = 3;
    nc = 3; 
    FontSizeVal = 10;
    cont = 0;
    
    for i=1:3
        for j=1:3
            cont = cont + 1;
            lmin = (i-1)*0.3+0.025;
            lmax = j*0.3+0.05;
            if lmin < lmax
                
                BWn(:,:,i,j)= edge(Img,'Canny',[lmin,lmax]);
                BWn_fc(:,:,:,i,j) = imfuse(Img, BWn(:,:,i,j));
                

                subplot(nr, nc, cont); imshow(BWn_fc(:,:,1,i,j),[0,255]); title(['Canny (th=[' num2str(lmin) ' , ' num2str(lmax) '])'],'FontSize',FontSizeVal);
            end 
        end
    end
    

 %% Hough Transform (lines) % https://www.mathworks.com/help/images/hough-transform.html
%     clc; clear; close all;
    Img = imread('circuit.tif');
    Img = imread('blobs.png');
    Img = imread('arch-window_bw.jpeg');
    [m,n] = size(Img);    
    
    % Find the edges in the image using the edge function.
    BW =  edge(Img,'Canny');
    % Compute the Hough transform of the binary image returned by edge.
    [H,theta,rho] = hough(BW);    
    % Find the peaks in the Hough transform matrix, H, using the houghpeaks function.
    P = houghpeaks(H,6,'threshold',ceil(0.3*max(H(:))));
    
    
    % Find lines in the image using the houghlines function. 
    lines = houghlines(BW,theta,rho,P,'FillGap',20,'MinLength',10);
    % Plot original image with the lines superimposed on it.
    figure;
    nr = 2;
    nc = 2; 
    subplot(nr, nc, 1); imshow(Img); title('Image');
    subplot(nr, nc, 2); imshow(BW); title('Canny');
    
    subplot(nr, nc, 3); 
    imshow(Img), hold on
    max_len = 0;
    for k = 1:length(lines)
       xy = [lines(k).point1; lines(k).point2];
       plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

       % Plot beginnings and ends of lines
       plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
       plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

       % Determine the endpoints of the longest line segment
       len = norm(lines(k).point1 - lines(k).point2);
       if ( len > max_len)
          max_len = len;
          xy_long = xy;
       end
    end
    % highlight the longest line segment
    plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');
    title('Hough Transform lines (FillGap=20)');
    hold off
    
    
    % Find lines in the image using the houghlines function. 
    lines = houghlines(BW,theta,rho,P,'FillGap',30,'MinLength',10);
        
    subplot(nr, nc, 4); 
    imshow(Img), hold on
    max_len = 0;
    for k = 1:length(lines)
       xy = [lines(k).point1; lines(k).point2];
       plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

       % Plot beginnings and ends of lines
       plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
       plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

       % Determine the endpoints of the longest line segment
       len = norm(lines(k).point1 - lines(k).point2);
       if ( len > max_len)
          max_len = len;
          xy_long = xy;
       end
    end
    % highlight the longest line segment
    plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');
    title('Hough Transform lines (FillGap=30)');
    hold off
   
    %%%%% ******************************************** Solutions ******************************************** %%%%%
    %% Template matching - Example 1
    clc; clear; close all;
    Img = im2double( imread('arch-window_bw.jpeg') );
    template = Img(96:108, 46:60);

    avgImg = mean(Img(:));
    

    mat = Correlation_kernel(Img-avgImg, template-avgImg);
    minMat = min(mat(:));
    mat2 = (mat-minMat) / max(mat(:)-minMat);

    nr = 1; nc = 3; 
    subplot(nr, nc, 1); imshow(Img); title('Img');
    subplot(nr, nc, 2); imshow(template); title('Template');
    subplot(nr, nc, 3); imshow(mat2); title('Correlation');
    
    %% Template matching - Example 2
    clc; clear; close all;
    Img = im2double( rgb2gray( imread('birds.png') ) );
    [m,n] =  size(Img);
    template = Img(7:67, 173:257);

    avgImg = mean(Img(:));

    mat = Correlation_kernel(Img-avgImg, template-avgImg);
    minMat = min(mat(:));
    mat2 = (mat-minMat) / max(mat(:)-minMat);

    matMax = max(mat2(:));
    [y,x]=find(matMax==mat2);
    tmp = matMax-mat2;

    nr = 1; nc = 4; 
    subplot(nr, nc, 1); imshow(Img); title('Img');
    subplot(nr, nc, 2); imshow(template); title('Template');
    subplot(nr, nc, 3); imshow(mat2); title('Correlation');
    subplot(nr, nc, 4); imshow(Img); title('Correlation [maxCorrelation]');
    hold on
    rectangle('Position',[x-42,y-30,84,60],'LineWidth',2,'LineStyle','--','EdgeColor','r')
    hold off
    
    %% myCanny
    clc; clear; close all;
    Img = imread('circuit.tif');
    Img = imread('arch-window_bw.jpeg');
    [m,n] = size(Img);
    
    BW1 = myCanny(Img,0.4,0.5,sqrt(2));  
    BW2 = myCanny(Img,0.4,0.5,2);
    BW3 = myCanny(Img,0.4,0.5,4);
    
    
    figure;
    nr = 2;
    nc = 2; 
    FontSizeVal = 10;
    subplot(nr, nc, 1); imshow(Img); title('Image','FontSize',FontSizeVal);
    subplot(nr, nc, 2); imshow(BW1); title('myCanny (automatic)','FontSize',FontSizeVal);
    subplot(nr, nc, 3); imshow(BW2); title('myCanny (th=[0.4,0.6])','FontSize',FontSizeVal);    
    subplot(nr, nc, 4); imshow(BW3); title('myCanny (\sigma = 4)','FontSize',FontSizeVal);


    
end


