% Reset
clear all; close all;

%import images
srcFiles = dir('C:\Users\GP\Documents\MATLAB\Aulas\aula 11\vid\*.png');  % the folder in which ur images exists
figure(1);
hold on;
for i = 1 : length(srcFiles)
    filename = strcat('C:\Users\GP\Documents\MATLAB\Aulas\aula 11\vid\',srcFiles(i).name);
    I = imread(filename);
   
    %================ COLOR FILTER ==================
    B=rgb2hsv(I);
    H=B(:,:,1);
    S=B(:,:,2);
    V=B(:,:,3);

    %button_mask=(H>=0 & H<=0.18 | H>=0.9 & H<=1) & S>0.5;
    button_mask = (H < 0.1 | H > 0.89) & S > 0.4 & V > 0.5;
    %=================================================
    
    % clear small areas (filter) and applies a close to round the shape
    button_mask=bwareaopen(button_mask,800);
    button_mask=imclose(button_mask, ones(3));
    
    %============== GETS IMG PROPS ===================
    %image properties
    [L,N]=bwlabel(button_mask);
    s = regionprops(L, 'Area','Perimeter','Centroid','Solidity');
    
    areas=[s.Area]; areas=areas';
    perim=[s.Perimeter];perim=perim';
    soli=[s.Solidity];soli=soli';
    
    %=== form factor
    ff=4*pi*areas./perim./perim;
    centroids=cat(1,s.Centroid);
    
    % find which shape corresponds to the button
    BTN_x=find(ff>0.7 & ff<0.95);
    BTN=ismember(L,BTN_x);
    
    
    %============= PLOT IMAGE AND POINTS ============
    figure(1);
    %imshow(BTN); hold on;
    if BTN_x>0
     plot(centroids(BTN_x,1), centroids(BTN_x,2), 'r*');
    end
    hold on;
    
    %pause();
end

print('-depsc', 'myfig.eps');

disp('END>')