clc
clear all
close all


%i = 1

visualizacao = true
nimg = 10;
grao = 'P1500' 
pds = [' a';' b';' c';' d';' e';' f';' g';' h';' i';' j';' l';' m';' n';' o';' p'];
spc = [' aa';' bb';' cc';' dd';' ee';' ff';' gg';' hh';' ii';' jj';' ll';' mm';' nn';' oo';' pp'];


%spc = ['ar';'br';'cr';'dr';'er';'fr';'gr';'hr';'jr';'lr';'mr';'nr';'or';'pr'];~
%fgs = ['FG1';'FG2';'FG3';'FG4';'FG5';'FG6';'FG7';'FG8';'FG9'];


if visualizacao == true
    figure(1)

    set(1,'Position',[24 51 1232 650])
    set(1,'NumberTitle','off')
    set(1,'Name','Análise Spectral')

    figure(2)

    set(2,'Position',[24 51 1232 650])
    set(2,'NumberTitle','off')
    set(2,'Name','1-7 Padrão Antigo, 8-14 Padrão Novo, 15-23 Def. Falta Grao')

    figure(3)

    set(3,'Position',[24 51 1232 650])
    set(3,'NumberTitle','off')
    set(3,'Name','Granulometria')
end

for  ct1= 1:5
    
%     fn = ['E:\Programas\MATLAB71\work\Ensaios Mestrado - 05.05.08\Imagens 2ºparte Trabalho\10.03.09 - Darkfield\' grao];
%     fn = [fn '\100309 L AGUA RL ' grao ];

    fn = ['C:\Programas\MATLAB71\work\Ensaios Mestrado - 05.05.08\Imagens 2ºparte Trabalho\22.01.09 - Testes\P150\Micro P150 0_graus 00']
%    fn = ['C:\Programas\MATLAB71\work\Ensaios Mestrado - 05.05.08\Imagens 2ºparte Trabalho\23.01.09 - Testes\DVD']
  
   if ct1 <= nimg
         fn = [fn pds(ct1,:) '.jpg'];
    else
        fn = [fn spc(ct1-nimg,:) '.jpg'];
    end
        
    irgb = imread(fn);
    icinz = rgb2gray(irgb);
    th = graythresh(icinz);

    step=1;
    ireduzida=icinz(1:step:size(icinz,1), 1:step:size(icinz,2));
    se = strel('square',10);

    ivalmax = imtophat(ireduzida,se);
    iajust = imadjust(ivalmax,[0 0.5],[]);

    h = imhist(iajust);
    ibin = im2bw(iajust,th);

     
    [v,unv] = statmoments(h, 2);
    
    padrao(ct1).media = v(1);
    padrao(ct1).desvio = v(2);
    padrao(ct1).mediaun = unv(1);
    padrao(ct1).desvioun = unv(2);
    mediaplot(ct1) = v(1);
    desvplot(ct1)= v(2);
    
    rst(ct1,:)= statxture(iajust);
    
    [labeled,numGant] = bwlabel(ibin,4);
   figure(5)
   imshow(ibin)
    
    
    for  sizeSl= 1:8
    
        restantes = imopen(ibin, strel('disk', sizeSl));
%   intensity_area(counter + 1) = sum(remain(:));
   figure(5)
   imshow(restantes)
         
        [labeled,numGdep] = bwlabel(restantes,4);
        numGr( ct1, sizeSl ) = numGant - numGdep;
        numGant = numGdep;
    end
    

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

    if visualizacao == true
        figure(1),
    
        subplot(2,3,1)
        imshow(icinz),
        title('rgb2gray')
    
        figure(1),
        subplot(2,3,2)
        imshow(iajust),
        title('imadjust')
    
        figure(1),
        subplot(2,3,3)
        imshow(ibin),
        title('im2bw')
    
        [srad, sang, S]=specxture(iajust);
        figure(1),
        subplot(2,3,4)
        plot(srad), 
        title('srad')
    
        figure(1),
        subplot(2,3,5)
        plot(sang), 
        title('sang')
    
        figure(1),
        subplot(2,3,6)
        imshow(S),
        title('Spectrum Image')
    
        soma = sum(numGr(ct1,:))
     
        if (50 < soma &  soma < 350)
            sprintf('%s', 'Contagem: P100')
            title('P100')
            resultado(ct1,:) = 'P100'
        end
    
        if (610 < soma &  soma < 910)
            sprintf('%s', 'Contagem: P320')
            title('P320')
            resultado(ct1,:) = 'P320'
        end

        if (2830 < soma &  soma < 3130)
            sprintf('%s', 'Contagem: P600')
            title('P600')
            resultado(ct1,:) = 'P600'
        end
    
        if (3310 < soma &  soma < 3610)
            sprintf('%s', 'Contagem: P800')
            title('P800')
            resultado(ct1,:) = 'P800'
        end
    
        if (7530 < soma &  soma < 7830)
            sprintf('%s', 'Contagem: P1500')
            title('P1500')
            resultado(ct1,:) = 'P1500'
        end
    end
    pause(1.2)
end

if visualizacao == true
    figure(2),
    subplot(4,2,1)
    bar(rst(:,1)), grid on;
    title('Intesidade Media')
    figure(2),
    subplot(4,2,2)
    bar(rst(:,2)), grid on;
    title('Contraste Medio')
    figure(2),
    subplot(4,2,3)
    bar(rst(:,3)), grid on;
    title('R')
    figure(2),
    subplot(4,2,4)
    bar(rst(:,4)), grid on;
    title('3º Momento')
    figure(2),
    subplot(4,2,5)
    bar(rst(:,5)), grid on;
    title('Uniformidade')
    figure(2),
    subplot(4,2,6)
    bar(rst(:,6)), grid on;
    title('Entropia')
end

somamedias = sum(mediaGrao);

mediaponderada = 1*(mediaGrao(1)/somamedias)+2*(mediaGrao(2)/somamedias)+3*(mediaGrao(3)/somamedias)+4*(mediaGrao(4)/somamedias)+5*(mediaGrao(5)/somamedias);

numtam = 0;
for i = 1 :8
    if mediaGrao(i)~= 0
        numtam = numtam + 1;
    end
end


%if visualizacao == true
    figure(3),plot(mediaGrao, 'm - *'), grid on;
    title(mean(rst(:,3)));
    xlabel('raio elemento estruturante (pixels)');
    ylabel('número Grãos');
%end
% figure(1),
% subplot(2,3,1)
% bar(mediaplot), grid on;
% title('Media: 1-7 Padrão Antigo, 8-14 Padrão Novo, 15-23 Def. Falta Grao')
% figure(1),
% subplot(2,3,2)
% bar(desvplot), grid on;
% title('Desvio: 1-7 Padrão Antigo, 8-14 Padrão Novo, 15-23 Def. Falta Grao')

%fdsurfft(icinz)

grao
