
clear all
pds = ['a';'b';'c';'d';'e';'f';'g';'h';'j';'l';'m';'n';'o';'p']
fgs = ['FG1';'FG2';'FG3';'FG4';'FG5';'FG6';'FG7';'FG8';'FG9']

for  ct1= 1:10
    fn = ['C:\Programas\MATLAB71\work\Ensaios Mestrado - 05.05.08\Imagens 2ºparte Trabalho\13.01.09 - Comparação_PadrãoActual_PadrãoAntigo_Recolha\P2000\130109 L AGUA RL P100 ' pds(ct1,:) '.jpg']
    fns1 = '091208 L AGUA RL P100 FG10 T1.jpg';
    fns2 = '091208 L AGUA RL P100 FG10 T2.jpg';
    th = 0.5;

    i1 = imread(fn);
    i2 = rgb2gray(i1);
    step=3;
    i2a=i2(1:step:size(i2,1), 1:step:size(i2,2));
    se = strel('square',10);

    i3a = imtophat(i2a,se);
    i3 = imadjust(i3a,[0 0.5],[]);
    i4 = im2bw(i3,th);


    % figure(1)
    % imshow(i1)
    % figure(2)
    % imshow(i2a)
    % figure(3)
    % imshow(i3)
    % figure(4)
    % imshow(i4)
    % figure(5)
    % imshow(i5)

    [labeled,numGant] = bwlabel(i4,4);

    for  sizeSl= 1:10
    
        restantes = imopen(i4, strel('disk', sizeSl));
        %intensity_area(counter + 1) = sum(remain(:));

        [labeled,numGdep] = bwlabel(restantes,4);
        numGr( ct1, sizeSl ) = numGant - numGdep;
        numGant = numGdep;
    end
    
end

tam_numGr = size (numGr);
mediaGrao = (sum(numGr(1:tam_numGr(1),:)))/tam_numGr(1);

    figure,plot(mediaGrao, 'm - *'), grid on;
    title(fn);
    xlabel('raio elemento estruturante (pixels)');
    ylabel('número Grãos');




% 
% se = strel('disk',2);
%         i5 = imopen(i5,se);
%  
%         [labeled,numGdep] = bwlabel(i5,4);
% 
% numGdep
% 
% se = strel('disk',3);
%         i5 = imopen(i5,se);
%  
%         [labeled,numGdep] = bwlabel(i5,4);
% 
% numGdep
% 
% se = strel('disk',4);
%         i5 = imopen(i5,se);
%  
%         [labeled,numGdep] = bwlabel(i5,4);
% 
% numGdep






% imwrite(i2a,fns1)
% 
% 
% 
% for counter = 0:22
%     remain = imopen(i4, strel('disk', counter));
%     intensity_area(counter + 1) = sum(remain(:));
% end
% figure,plot(intensity_area, 'm - *'), grid on;
% title('Sum of pixel values in opened image as a function of radius');
% xlabel('radius of opening (pixels)');
% ylabel('pixel value sum of opened objects (intensity)');

