aa=dir;

for qi=1:size(aa,1)
    
    if aa(qi).isdir
        if ~(aa(qi).name=='.')
            if ~(strcmp(aa(qi).name,'..'))
                disp (aa(qi).name)
                ab=dir([aa(qi).folder '/' aa(qi).name '/*.tif']);
                
                for qii=1:size(ab,1)
                    if ~ab(qii).isdir
                        if ~(~ab(qii).name=='.')
                            if ~(strcmp(ab(qii).name,'..'))
%                                 disp (ab(qii).name)
                                IM=imread([ab(qii).folder '/' ab(qii).name]);
                                figure (1)
                                imagesc (IM)
                                title ('Vyber rohy pajeci plochy')
                                [xi,yi]=ginput(2);
                                IM=IM(floor(yi(1)):floor(yi(2)),floor(xi(1)):floor(xi(2)));
                                imagesc (IM)
                                title ('Vybrana pajeci plocha')
                                figure (2)
                                histogram(IM(:))    
                                title ('zvol rozhodovaci uroven pro stupen sedi voidu (mezi laloky v histogramu)')
                                [lx,ly]=ginput(1);
                                lx=floor(lx);
                                figure (3)
                                imagesc(IM>lx)
                                title ('detekovane voidy')
                                text (10,10,sprintf('PLOCHA VOIDU: %2.2g procent',100*sum(IM(:)>lx)/numel(IM(:))),'Color','red');
                                fprintf(1,'soubor:\t%s\tplocha voidu\t%g\n',ab(qii).name,100*sum(IM(:)>lx)/numel(IM(:)));
                            end
                        end
                    end
                end
               
            end

        end
    
    end


end
