%%%11.24.20 classify images using net16 for splitBatch1



load('network16.mat')

path_in='/gpfs/summit/scratch/apsendro@colostate.edu/mackClassify/Images/splitBatch5'
path_out='/gpfs/summit/scratch/apsendro@colostate.edu/mackClassify/Classified/splitBatch5_c'

files=dir(fullfile(path_in,'*.TIF'));

rbg1=[3,2,1];
rbg2=[5,3,2];
step=1024;

delta_cover_5=NaN(length(files),1);
wood_cover_5=NaN(length(files),1);

for zz=1:length(files)
    zz

    [img,R] = geotiffread(fullfile(path_in,files(zz).name));
    info = geotiffinfo(fullfile(path_in,files(zz).name));

    key = info.GeoTIFFTags.GeoKeyDirectoryTag;

    delta_cover_5(zz)=size(img,1)*size(img,2)*R.CellExtentInWorldX*R.CellExtentInWorldY;
    delta_cover_5(zz)
    
    s=size(img);
    if s(3)<6;
       img_rbg=img(:,:,rbg1);
    elseif s(3)>7; 
       img_rbg=img(:,:,rbg2);
    end
    clear s 
    
    a=size(img_rbg);
    
    C=uint8(NaN(a(1),a(2)));
        
    n_h = fix(a(1)/step);
    n_w = fix(a(2)/step);
    
    for ii=0:n_w-1
        for jj=0:n_h-1
    
            x=1+(step*jj);
            y=1+(step*ii);
            width=step;
            height=step;
        
            C(x:x+width,y:y+height)=semanticseg(img_rbg(x:x+width,y:y+height,:),net16,'OutputType','double','ExecutionEnvironment','cpu');
            
        end
    end
    clear ii jj x y width height

    C((a(1)-step:a(1)),:)=semanticseg(img_rbg((a(1)-step:a(1)),:,:),net16,'OutputType','double','ExecutionEnvironment','cpu');
    C(:,(a(2)-step):a(2))=semanticseg(img_rbg(:,(a(2)-step):a(2),:),net16,'OutputType','double','ExecutionEnvironment','cpu');
    
%     C=semanticseg(img_rbg,net16,'OutputType','double','ExecutionEnvironment','cpu');
    
    wood=sum(C(:)==1);
    wood_cover_5(zz)=wood*R.CellExtentInWorldX*R.CellExtentInWorldY;
    wood_cover_5(zz)
    
    [blob,name1]=fileparts(files(zz).name);
    namesave=fullfile(path_out,strcat(name1,'_classified.tif'));
    geotiffwrite(namesave,C,R,'GeoKeyDirectoryTag',key);
    
    ClassInfo5(zz).File=files(zz).name;
    ClassInfo5(zz).DeltaCover=delta_cover_5(zz);
    ClassInfo5(zz).WoodCover=wood_cover_5(zz);

clear img R info key img_rbg a C n_h n_w name1 namesave wood blob
end

save(fullfile(path_out,'classified_info_splitBatch5.mat'),'delta_cover_5','wood_cover_5','ClassInfo5')

clear files path_in path_out rbg1 rbg2 step zz p