clear all;
close all;

ROI_Drawing=0;   % For ROI or Seed : 1:Tractography
ManualSegTag=0;
%%
inputFileTag= 'mat';  %'img'; %'mat'
FiberName='Cg';  

Scanner='GE';  %  'Siemens';  % 'Phillips' ;  %

addpath F:\MatlabCodes

addr=  'Z:\Epilepsy_ANALYZE\EP1218'
addpath addr; 

cd  (addr) 
   
if ROI_Drawing 
    %%
    str=[FiberName 'ROI'];  %Seg'];
else
     str=[FiberName 'Seed'];
end

SeedPoints=[];
r=1;Label=1;

[filename, pathname] = uigetfile('DTI.mat','Load DTI File');
exist_f = isa(filename,'char');
if exist_f
    Inputfile = fullfile(pathname,filename);
else
    return;
end

cd (pathname);
load (Inputfile,'FEFA','VDims');


[M,N,N_Slice,dim]=size(FEFA);
%


if inputFileTag=='mat'
        
    % [filename, pathname] = uiputfile('Cg_Seed.mat', 'Save ROI as...');
    [filename, pathname] = uiputfile([str '.mat'], 'Save ROI as...');

    % [filename, pathname] = uiputfile('ROI_CST_2Labels.mat', 'Save ROI as...');
    exist_f = isa(filename,'char');
    if exist_f
        targetfile = fullfile(pathname,filename);
    else
        return;
    end
    %strFile=[FiberName,'ROIMatfile.mat'];
    if exist((targetfile),'file')==2
        load (targetfile)   %ROI0
%         ROI(ROI>0)=1;
%         ROI(ROI<=0)=0;
    else
        ROI=zeros(M,N,N_Slice);
    end

    try
        ROI=data;
        ROI(ROI>0)=1;
        ROI(ROI<=0)=0;

    catch
        try
            ROI=fiberImage;

            ROI(ROI>0)=1;
            ROI(ROI<=0)=0;
        catch
            try
                 ROI=FiberImage;

                ROI(ROI>0)=1;
                ROI(ROI<=0)=0;
            catch
            
            end

        end
    end
else
     [filename, pathname] = uigetfile('*.img', 'Segmented Fiber Bundle...');
     exist_f = isa(filename,'char');
     if exist_f
         targetfile = fullfile(pathname,filename);
     else
         return;
     end
     
     
     [ROI, MDims, Precision, VDims] = Read_Analyze(targetfile);

end

FiberImageSize=[M,N,N_Slice];

% for i=1:N_Slice
%     
%     im3d(:,:,i,2)=(FEFA(:,:,i,1));
%     im3d(:,:,i,1)=(FEFA(:,:,i,2));
%     im3d(:,:,i,3)=(FEFA(:,:,i,3));
%        
% end

% find(isnan(FEFA))

im3d=FEFA;

MDims=[M,N,N_Slice];
resizeFactor=1;   %VDims(3)/VDims(1);
im3dORIG=im3d;

mx=max(im3d(:));

VisROI=zeros(size(ROI));

VisROI(find(ROI~=0))=255;

im3d(:,:,:,1)=im3d(:,:,:,1)+VisROI;
im3d(:,:,:,2)=im3d(:,:,:,2)+VisROI;
im3d(:,:,:,3)=im3d(:,:,:,3)+VisROI;

im3d(im3d>1)=1;

%------------Saving Prev im3d and ROI in Matlab format------------------------

p(1)=round(M/2);
p(2)=round(N/2);
p(3)=round(N_Slice/2);

SelectedPoints=[];
count=1;

scrsz = get(0,'ScreenSize');
h1=figure('Position',[10 scrsz(3)/3 scrsz(3)/2-20 scrsz(4)/2]);
% hold on;

h2=figure('Position',[scrsz(3)/2 scrsz(4)/2 scrsz(3)/2-20 scrsz(4)/3]);
% hold on;

h3=figure('Position',[scrsz(3)/2 20 scrsz(3)/2-20 scrsz(4)/3]);
% hold on;

% imp=reshape(im3d(:,p(2),:,:),M,N_Slice,3);
im=[];
for i=1:3
    im(:,:,i)=flipud(imresize(reshape(im3d(:,p(2),:,i),M,N_Slice)',[N_Slice*resizeFactor M] ));  % (imp(:,:,i)');
end

figure(2),    imshow(im,[],'InitialMagnification',350);
hold on
plot( p(1), N_Slice+1-p(3), 'wx' );
hold off
% imp=reshape(im3d(p(1),:,:,:),N,N_Slice,3);
im=[];
for i=1:3
    im(:,:,i)= flipud(imresize(reshape(im3d(p(1),:,:,i),N,N_Slice)',[N_Slice*resizeFactor N] ));  %(imp(:,:,i)');
end
figure(3),        imshow(im,[],'InitialMagnification',350);
hold on
plot( p(2), N_Slice+1-p(3), 'wx' );
hold off
imp=[];
imp=reshape(im3d(:,:,p(3),:),M,N,3); %N_Slice+1-
im=[];
for i=1:3
    im(:,:,i)=(imp(:,:,i)');
end
figure(1),    imshow(im,[],'InitialMagnification',350);
hold on
plot( p(1), p(2),'wx' );
hold off
f=1;

[p1(1),p1(2),button] = ginput(1);
p1=round(p1);
% p1(1)=round(p1(1));
% p1(2)=fix(p1(2));


while (button ~= 70 && button ~=102)  % F OR f pressed  (FINISHED) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    if button==27   %Escape pressed
        break;
        
    elseif button==108   || button==76   %----------------Label--------------------------
        
        Label=Label+1
        
        [p1(1),p1(2),button] = ginput(1);
        p1=round(p1);
        
        
   elseif button==112   || button==80 %--------------p or P pressed (Previous)-------------------------------------------------------
      
        if f==1
            p(3)=p(3)-1;
        elseif f==2
            p(2)=p(2)-1;
        elseif f==3
            p(1)=p(1)-1;
        end
        
        if (sum(p > MDims) + sum(p < [1 1 1] ) == 0)
            
            clear imp
            imp=reshape(im3d(:,:,p(3),:),M,N,3);
            im=[];
            for i=1:3
                im(:,:,i)=(imp(:,:,i)' );
            end
            clear imp
            figure(1),    try                 imshow(im,[]);             catch             end
            hold on
            plot( p(1), p(2), 'wx' );
            hold off
            
            % imp=reshape(im3d(:,p(2),:,:),M,N_Slice,3);
            im=[];
            for i=1:3
                im(:,:,i)=flipud(imresize(reshape(im3d(:,p(2),:,i),M,N_Slice)',[N_Slice*resizeFactor M] ));  % (imp(:,:,i)');
            end
            figure(2),    try                 imshow(im,[]);             catch             end
            hold on
            plot( p(1), N_Slice+1-p(3), 'wx' );
            hold off
            im=[];
            for i=1:3
                im(:,:,i)= flipud(imresize(reshape(im3d(p(1),:,:,i),N,N_Slice)',[N_Slice*resizeFactor N] ));  %(imp(:,:,i)');
            end
            figure(3),        try                 imshow(im,[]);             catch             end
            hold on
            plot( p(2), N_Slice+1-p(3), 'wx' );
            hold off
            SelectedPoints=[];
            
        else
            if f==1
                p(3)=p(3)+1;
            elseif f==2
                p(2)=p(2)+1;
            elseif f==3
                p(1)=p(1)+1;
            end
        end
        
        figure(f),
        [p1(1),p1(2),button] = ginput(1);
        p1=round(p1);
        
    elseif button==110  || button==78%-------------------n or N PRESSED (NEXT)------------------------------------------------
        
        if f==1
            p(3)=p(3)+1;
        elseif f==2
            p(2)=p(2)+1;
        elseif f==3
            p(1)=p(1)+1;
        end
        
        if (sum(p > MDims) + sum(p < [1 1 1] ) == 0)
            
            imp=reshape(im3d(:,:,p(3),:),M,N,3);
            im=[];
            for i=1:3
                im(:,:,i)=(imp(:,:,i)');
            end
            clear imp
            figure(1),    try                 imshow(im,[]);             catch             end
            hold on
            plot( p(1), p(2), 'wx' );
            hold off
            
            % imp=reshape(im3d(:,p(2),:,:),M,N_Slice,3);
            im=[];
            for i=1:3
                im(:,:,i)=flipud(imresize(reshape(im3d(:,p(2),:,i),M,N_Slice)',[N_Slice*resizeFactor M]) );  % (imp(:,:,i)');
            end
            figure(2),    try                 imshow(im,[]);             catch             end
            hold on
            plot( p(1), N_Slice+1-p(3), 'wx' );
            hold off
            
            im=[];
            for i=1:3
                im(:,:,i)=flipud( imresize(reshape(im3d(p(1),:,:,i),N,N_Slice)',[N_Slice*resizeFactor N] ));  %(imp(:,:,i)');
            end
            figure(3),        try                 imshow(im,[]);             catch             end
            hold on
            plot( p(2), N_Slice+1-p(3), 'wx' );
            hold off
            SelectedPoints=[];
        else
            
            if f==1
                p(3)=p(3)-1;
            elseif f==2
                p(2)=p(2)-1;
            elseif f==3
                p(1)=p(1)-1;
            end
        end
        
        figure(f),
        [p1(1),p1(2),button] = ginput(1);
        p1=round(p1);
        
    elseif button==67 || button==99  || button==9 %--------c or C or Tab pressed--(Change)----------------------------------------------------------
        
        
        f=f+1;
        
        if f==4
            %             reply = input('Do you want to change the figure? Y/N [Y]: ', 's');
            %             %             if isempty(reply)
            %             %                 reply = 'Y';
            %             %             end
            %             %             pause;
            f=1;
        end
        
        SelectedPoints=[];
        
        figure(f),
        [p1(1),p1(2),button] = ginput(1);
        p1=round(p1);
        
        
        
    elseif button==1  %--------------------(Left Click)------------------------------------
        
        if f ~= 1
            p1(2)=ceil(p1(2)/resizeFactor);
        end
        
        if f==1
            p(1)=p1(1);
            p(2)=p1(2);
        elseif f==2
            p(1)=p1(1);
            p(3)=p1(2);
        elseif f==3
            p(2)=p1(1);
            p(3)=p1(2);
        end
        
        SelectedPoints = [SelectedPoints; p(1) p(2) p(3)];
        %         SelectedPoints=unique(SelectedPoints','rows')';
        
        imp=reshape(im3d(:,:,p(3),:),M,N,3);
        im=[];
        for i=1:3
            im(:,:,i)=(imp(:,:,i)');
        end
        clear imp
        figure(1),   
        try
        imshow(im,[]);
        catch
        end
 
        
        % imp=reshape(im3d(:,p(2),:,:),M,N_Slice,3);
        im=[];
        for i=1:3
            im(:,:,i)=flipud(imresize(reshape(im3d(:,p(2),:,i),M,N_Slice)',[N_Slice*resizeFactor M] ));  % (imp(:,:,i)');
        end
        figure(2),   
        try
            imshow(im,[]);
        catch
        end
       
        im=[];
        for i=1:3
            im(:,:,i)= flipud(imresize(reshape(im3d(p(1),:,:,i),N,N_Slice)',[N_Slice*resizeFactor N] ));  %(imp(:,:,i)');
        end
        figure(3),       
        try
            imshow(im,[]);
        catch
        end
       
         
         
        figure(f)
        if f==1
            hold on
            plot( p(1), p(2), 'w:' );
            
            if size(SelectedPoints , 1) >=2
%                 line(SelectedPoints(end-1:end,1),SelectedPoints(end-1:end,2),'Marker','.','Color','w');
                 line(SelectedPoints(:,1),SelectedPoints(:,2),'Marker','.','Color','w');
            end
            drawnow;
            hold off;
            
        elseif f==2
            hold on
            plot( p(1), p(3)*resizeFactor, 'w:' );
            if size(SelectedPoints , 1) >=2
%                 line(SelectedPoints(end-1:end,1),SelectedPoints(end-1:end,3)*resizeFactor,'Marker','.','Color','w');
                line(SelectedPoints(:,1),SelectedPoints(:,3),'Marker','.','Color','w');
            end
            drawnow;
            
              hold off;
              
        elseif f==3
            hold on
            plot( p(2), p(3)*resizeFactor, 'w:' );
            if size(SelectedPoints , 1) >=2
%                 line(SelectedPoints(end-1:end,2),SelectedPoints(end-1:end,3)*resizeFactor,'Marker','.','Color','w');
                line(SelectedPoints(:,2),SelectedPoints(:,3),'Marker','.','Color','w');
            end
            drawnow;
            
              hold off;
              
        end
        
        %       plot3( p(1), p(2),p(3), 'w:' );
        %       if size(SelectedPoints , 1) >=2
        %          line(SelectedPoints(end-1:end,1),SelectedPoints(end-1:end,2),SelectedPoints(end-1:end,3),'Marker','.','Color','w');
        %       end
        %       drawnow;
        
        
        [p1(1),p1(2),button] = ginput(1);
        p1=round(p1);
        
    elseif button==2 %-----------------------(Middle click)---------------------------------------------------
        
        if f ~= 1
%             p1(2)=ceil(p1(2)/resizeFactor);
             p1(2)=N_Slice+1-ceil(p1(2)/resizeFactor);
        end
        
        if f==1
            p(1)=p1(1);
            p(2)=p1(2);
        elseif f==2
            p(1)=p1(1);
            p(3)=p1(2);
        elseif f==3
            p(2)=p1(1);
            p(3)=p1(2);
        end
        
%         ROI(p(1),p(2),p(3));
        
        imp=reshape(im3d(:,:,p(3),:),M,N,3);
        im=[];
        for i=1:3
            im(:,:,i)=(imp(:,:,i)');
        end
        clear imp
        figure(1),    try                 imshow(im,[]);             catch             end
        hold on
        plot( p(1), p(2), 'wx' );
        hold off
        
        
        % imp=reshape(im3d(:,p(2),:,:),M,N_Slice,3);
        im=[];
        for i=1:3
            im(:,:,i)=flipud(imresize(reshape(im3d(:,p(2),:,i),M,N_Slice)',[N_Slice*resizeFactor M] ));  % (imp(:,:,i)');
        end
        figure(2),    try                 imshow(im,[]);             catch             end
         hold on
        plot( p(1), N_Slice+1-p(3)*resizeFactor, 'wx' );
         hold off
        im=[];
        for i=1:3
            im(:,:,i)= flipud(imresize(reshape(im3d(p(1),:,:,i),N,N_Slice)',[N_Slice*resizeFactor N] ));  %(imp(:,:,i)');
        end
        figure(3),        try                 imshow(im,[]);             catch             end
         hold on
        plot( p(2),  N_Slice+1-p(3)*resizeFactor, 'wx' );
         hold off
        SelectedPoints=[];
        
        %         Signal_Data=double (reshape(DWI(p(1),p(2),N_Slice+1-p(3),2:end),NrOfDirections,1));
        %
        %         %         [i j k]=ind2sub([M N N_Slice],69868);
        %         %         Signal_Data=double (reshape(DWI(i, j, k,2:end),NrOfDirections,1));
        %
        %         ODF_PDD_GUI();
        
        %         m=1.2;
        %         Precision=1;
        %         DIFF_C=0.05;
        %
        %         lmax=8;
        %         Lambda=2.0e-2;  %1.0e-18;
        %
        %         [ODF PDDs_ALL_k NPDDs_k] = fOD_ODF_PDDs_inThisVoxel_Mex(Signal_Data,lmax,Lambda,m,Precision,DIFF_C,e2_e1,eta,ODF_tag) ;
        % %         [ODF PDDs] = ODF_PDDs_inThisVoxel(Signal_Data,lmax,Lambda,Beta_Test_Treshold,m,Precision,DIFF_C);
        %         PDDs=reshape(PDDs_ALL_k,3,4);
        %
        %         [x,y,z] = sph2cart(PHI_U, THETA_U,ODF);
        %         figure(5);
        %         newplot
        %         title('ODF calculated with PDDs');
        %
        %         plot3(x,-y,-z,'.');
        %         hold on;
        %
        %         for j=1:size(PDDs,2)
        %             line([0 PDDs(1,j)],[0 -PDDs(2,j)],[0 -PDDs(3,j)],'Color','r');
        %         end
        %
        %         hold off;
        %         axis equal;
        
        
        figure(f),
        
        [p1(1),p1(2),button] = ginput(1);
        p1=round(p1);
        
        
    elseif button==3 %-----------------------(Righ click)------------------------------------------------------
        
        if f ~= 1
            p1(2)=N_Slice+1-ceil(p1(2)/resizeFactor);
        end
        
        if f==1
            p(1)=p1(1);
            p(2)=p1(2);
        elseif f==2
            p(1)=p1(1);
            p(3)=p1(2);
        elseif f==3
            p(2)=p1(1);
            p(3)=p1(2);
        end
        SeedPoint=[p(1) p(2) p(3)]';
        SeedPoints= [SeedPoints SeedPoint]; 
        
        if (ROI(p(1),p(2),p(3))==0)
            im3d(p(1),p(2),p(3),:)=mx;
            ROI(p(1),p(2),p(3))=Label;
        else
            im3d(p(1),p(2),p(3),:)=im3dORIG(p(1),p(2),p(3),:);
            ROI(p(1),p(2),p(3))=0;
        end
        
        imp=reshape(im3d(:,:,p(3),:),M,N,3);
        im=[];
        for i=1:3
            im(:,:,i)=(imp(:,:,i)');
        end
        clear imp
        figure(1),    try                 imshow(im,[]);             catch             end
        
        % imp=reshape(im3d(:,p(2),:,:),M,N_Slice,3);
        im=[];
        for i=1:3
            im(:,:,i)=flipud(imresize(reshape(im3d(:,p(2),:,i),M,N_Slice)',[N_Slice*resizeFactor M] ));  % (imp(:,:,i)');
        end
        figure(2),    try                 imshow(im,[]);             catch             end
        
        
        im=[];
        for i=1:3
            im(:,:,i)= flipud(imresize(reshape(im3d(p(1),:,:,i),N,N_Slice)',[N_Slice*resizeFactor N] ));  %(imp(:,:,i)');
        end
        
        
        figure(3),        
        try                 imshow(im,[]);             catch             end
        
        SelectedPoints=[];
        
        
        figure(f),
        [p1(1),p1(2),button] = ginput(1);
        p1=round(p1);
        
        
        
        
        
    elseif button==68 || button==100 %-----------------------D or d pressed  (Delete ROI and im3d for display ) ------------------------------------------------
        
      try
            
            SelectedPoints(:,3)=N_Slice+1- SelectedPoints(:,3);
            
            %            if size(SelectedPoints,1) < 3
            %                msgbox('Select at least 3 points...');
            %            end
            %
            %
            %            if size(SelectedPoints,1) >= 3
            %--------------------------
            if f==1
                bw = poly2mask(SelectedPoints(:,1), SelectedPoints(:,2),N,M);
                
                [Y,X] = find(bw);
                
                Z = repmat(p(3),[length(Y),1]);
                
                for i=1:length(Z)
                    
                    ROI(X(i),Y(i),Z(i))=0;
                    im3d(X(i),Y(i),Z(i),:)=im3dORIG(X(i),Y(i),Z(i),:);
                    
                end
                
            elseif f==2
                bw = poly2mask(SelectedPoints(:,1), SelectedPoints(:,3),N_Slice,N);
                
                [Z,X] = find(bw);
                Y = repmat(p(2),[length(X),1]);
                
                for i=1:length(Y)
                    
                    ROI(X(i),Y(i),Z(i))=0;
                    im3d(X(i),Y(i),Z(i),:)=im3dORIG(X(i),Y(i),Z(i),:);
                    
                end
                
                
            elseif f==3
                
                bw = poly2mask(SelectedPoints(:,2), SelectedPoints(:,3),N_Slice,M);
                
                [Z,Y] = find(bw);
                X = repmat(p(1),[length(Y),1]);
                
                for i=1:length(X)
                    
                    ROI(X(i),Y(i),Z(i))=0;
                    im3d(X(i),Y(i),Z(i),:)=im3dORIG(X(i),Y(i),Z(i),:);
                    
                end
            end
            
            
            
            
            
        catch
            Label=1;
            ROI=zeros(M,N,N_Slice);
            VisROI=ROI; %+DoseEndRTBinned*0.1;
            im3d=FEFA;
            
            im3d(:,:,:,1)=im3d(:,:,:,1)+VisROI;
            im3d(:,:,:,2)=im3d(:,:,:,2)+VisROI;
            im3d(:,:,:,3)=im3d(:,:,:,3)+VisROI;
            
            im3d(im3d>1)=1;
            
            
        end
        
        %--------------------------
        
        
        imp=reshape(im3d(:,:,p(3),:),M,N,3);
        im=[];
        for i=1:3
            im(:,:,i)=(imp(:,:,i)');
        end
        clear imp
        figure(1),
        try
            imshow(im,[]);
        catch
        end
        
        
        % imp=reshape(im3d(:,p(2),:,:),M,N_Slice,3);
        im=[];
        for i=1:3
            im(:,:,i)=flipud(imresize(reshape(im3d(:,p(2),:,i),M,N_Slice)',[N_Slice*resizeFactor M] ));  % (imp(:,:,i)');
        end
        figure(2),
        try
            imshow(im,[]);
        catch
            ssss=0;
        end
        
        im=[];
        for i=1:3
            im(:,:,i)= flipud(imresize(reshape(im3d(p(1),:,:,i),N,N_Slice)',[N_Slice*resizeFactor N] ));  %(imp(:,:,i)');
        end
        figure(3),
        try
            imshow(im,[]);
        catch
            ssss=0;
            
        end
        
        
        
        
        SelectedPoints=[];
        
        figure(f),
        [p1(1),p1(2),button] = ginput(1);
        p1=round(p1);
        
    elseif button==117 || button==85 %-----------------------U or u pressed  (update ROI and im3d for display ) ------------------------------------------------
        
         SelectedPoints(:,3)=N_Slice+1- SelectedPoints(:,3);
        if size(SelectedPoints,1) < 3
            msgbox('Select at least 3 points...');
        end
        
        
        if size(SelectedPoints,1) >= 3
            %--------------------------
            if f==1
                bw = poly2mask(SelectedPoints(:,1), SelectedPoints(:,2),N,M);
                
                [Y,X] = find(bw);
                
                Z = repmat(p(3),[length(Y),1]);
                
                for i=1:length(Z)
                    
                    ROI(X(i),Y(i),Z(i))=Label;
                    im3d(X(i),Y(i),Z(i),:)=mx;
                    
                    
                    
                end
                   
            elseif f==2
                bw = poly2mask(SelectedPoints(:,1), SelectedPoints(:,3),N_Slice,N);
                
                [Z,X] = find(bw);
                Y = repmat(p(2),[length(X),1]);
                
                for i=1:length(Y)
                    
                    ROI(X(i),Y(i),Z(i))=Label;
                    im3d(X(i),Y(i),Z(i),:)=mx;
                    
                end
                
                
            elseif f==3
                
                bw = poly2mask(SelectedPoints(:,2), SelectedPoints(:,3),N_Slice,M);
                
                [Z,Y] = find(bw);
                X = repmat(p(1),[length(Y),1]);
                
                for i=1:length(X)
                    
                    ROI(X(i),Y(i),Z(i))=Label;
                    im3d(X(i),Y(i),Z(i),:)=mx;
                    
                end
            end
            
            
            %--------------------------
            
            
            imp=reshape(im3d(:,:,p(3),:),M,N,3);
            im=[];
            for i=1:3
                im(:,:,i)=(imp(:,:,i)');
            end
            clear imp
            figure(1),    
            try
                imshow(im,[]);
            catch
            end
            
            % imp=reshape(im3d(:,p(2),:,:),M,N_Slice,3);
            im=[];
            for i=1:3
                im(:,:,i)=flipud(imresize(reshape(im3d(:,p(2),:,i),M,N_Slice)',[N_Slice*resizeFactor M] ));  % (imp(:,:,i)');
            end
            figure(2),    
            try
                imshow(im,[]);
            catch
            end
            
            im=[];
            for i=1:3
                im(:,:,i)= flipud(imresize(reshape(im3d(p(1),:,:,i),N,N_Slice)',[N_Slice*resizeFactor N] ));  %(imp(:,:,i)');
            end
            figure(3),        
            try    
                imshow(im,[]);  
            catch
            end
        end
        
        
        SelectedPoints=[];
        
        figure(f),
        [p1(1),p1(2),button] = ginput(1);
        p1=round(p1);
        
    elseif button==115 || button==83 %-----------------------s or S pressed  (Save) ------------------------------------------------
        %------------Saving im3d and ROI in Matlab format------------------------
        
        %         strFile=[FiberName,'ROIMatfile.mat'];
        %         save(strFile, 'ROI');
        SeedPoints
        
        try
            save (filename, 'ROI','Act_Right_Posterior', 'Act_Right_Superior' , 'Act_Right_Anterior','Act_Left_Posterior', 'Act_Left_Superior' , 'Act_Left_Anterior' );
        
        catch
            
            save(targetfile, 'ROI','SeedPoints');
        end
         Analyze = Write_Analyze (targetfile, 32*ROI ,'int16', VDims);
        
        
        %         strFile=[FiberName,'im3dMatfile.mat'];
        %         save(strFile, 'im3d');
                
        figure(f),
        [p1(1),p1(2),button] = ginput(1);
        p1=round(p1);
    else
        figure(f),
        [p1(1),p1(2),button] = ginput(1);
        p1=round(p1);
    end
    
end

if button ~=27
    
    if ROI_Drawing
        
        ROIImageSize=size(ROI);
        N_Slice=ROIImageSize(3);
        
        [filename, pathname] = uiputfile([FiberName, 'ROIImage.img'], 'Save ROIImage as...');
        
        exist_f = isa(filename,'char');
        if exist_f
            targetfile = fullfile(pathname,filename);
        else
            return;
        end
              
        Analyze = Write_Analyze (targetfile, 90*ROI ,'int16', VDims);
        
        [X,Y,Z] = meshgrid(1:M,1:N,1:N_Slice) ;
        [faces,Boundry]=(isosurface(Y,X,Z, ROI, 0));
        
        VolumeBoundry = round(Boundry');
            
        offset=0; %fix(N_Slice/8);
        L2=fix(sqrt((N_Slice-offset)/1));%+2;
        L1=fix((N_Slice-offset)/L2)-1;
        
        im=zeros(L1*M,L2*N,3);
        impp=zeros(M,N,3);
       
        
        for i1=1:L1
            for i2=1:L2
                
                vz=VolumeBoundry(3,:)==(i1-1)*L2+i2+offset; imp=reshape(FEFA(:,:,(i1-1)*L2+i2+offset,:),M,N,3);
                a=sub2ind([M,N,3],VolumeBoundry(1,vz),VolumeBoundry(2,vz),repmat(1,1,length(VolumeBoundry(3,vz)) ));
                imp(ind2sub([M,N,3], a))=2*max(imp(:));
                b=sub2ind([M,N,3],VolumeBoundry(1,vz),VolumeBoundry(2,vz),repmat(2,1,length(VolumeBoundry(3,vz)) ));
                imp(ind2sub([M,N,3], b))=2*max(imp(:));
                c=sub2ind([M,N,3],VolumeBoundry(1,vz),VolumeBoundry(2,vz),repmat(3,1,length(VolumeBoundry(3,vz)) ));
                imp(ind2sub([M,N,3], c))=2*max(imp(:));
                for i3=1:3
                    impp(:,:,i3)=(imp(:,:,i3)');
                end
                
                im((i1-1)*M+1:i1*M,(i2-1)*N+1:i2*N,:)=impp;
                
            end
        end
        
        
        figure(h1);
        
        imshow((im));
        
        %Coronal
        
        offset=fix(N/10);
        
        L2=fix(sqrt((N-offset)/2))+2;
        
        L1=fix((N-offset)/(2*L2))-1;
        
        
        im=zeros(L2*N_Slice,L1*M,3);
        impp=zeros(N_Slice,M,3);
        
        for i1=1:L1
            for i2=1:L2
                
                vz=VolumeBoundry(2,:)==(2*i1-1)*L2+2*i2+offset; imp=reshape(FEFA(:,(2*i1-1)*L2+2*i2+offset,:,:),M,N_Slice,3);
                a=sub2ind([M,N_Slice,3],VolumeBoundry(1,vz),VolumeBoundry(3,vz),repmat(1,1,length(VolumeBoundry(2,vz)) ));
                imp(ind2sub([M,N_Slice,3], a))=2*max(imp(:));
                b=sub2ind([M,N_Slice,3],VolumeBoundry(1,vz),VolumeBoundry(3,vz),repmat(2,1,length(VolumeBoundry(2,vz)) ));
                imp(ind2sub([M,N_Slice,3], b))=2*max(imp(:));
                c=sub2ind([M,N_Slice,3],VolumeBoundry(1,vz),VolumeBoundry(3,vz),repmat(3,1,length(VolumeBoundry(2,vz)) ));
                imp(ind2sub([M,N_Slice,3], c))=2*max(imp(:));
                for i3=1:3
                    impp(:,:,i3)=flipud(imp(:,:,i3)'); %
                end
                
                im((i2-1)*N_Slice+1:i2*N_Slice,(i1-1)*M+1:i1*M,:)=impp;
                
                
            end
        end
        
        figure(h2);
        imshow((im));
        
        %Sagittal
       
        
        offset=fix(M/3)+5;
        
        L2=fix(sqrt((M-offset)/1))+0;
        
        L1=fix((M-1*offset)/(2*L2));
        
        im=[];
        im=zeros(L2*N_Slice,L1*N,3);
        impp=zeros(N_Slice,N,3);
        
        
        for i1=1:L1
            for i2=1:L2
                
                vz=VolumeBoundry(1,:)==(1*i1-1)*L2+1*i2+offset; imp=reshape(FEFA((1*i1-1)*L2+1*i2+offset,:,:,:),N,N_Slice,3);
                a=sub2ind([N,N_Slice,3],VolumeBoundry(2,vz),VolumeBoundry(3,vz),repmat(1,1,length(VolumeBoundry(1,vz)) ));
                imp(ind2sub([N,N_Slice,3], a))=2*max(imp(:));
                b=sub2ind([N,N_Slice,3],VolumeBoundry(2,vz),VolumeBoundry(3,vz),repmat(2,1,length(VolumeBoundry(1,vz)) ));
                imp(ind2sub([N,N_Slice,3], b))=2*max(imp(:));
                c=sub2ind([N,N_Slice,3],VolumeBoundry(2,vz),VolumeBoundry(3,vz),repmat(3,1,length(VolumeBoundry(1,vz)) ));
                imp(ind2sub([N,N_Slice,3], c))=2*max(imp(:));
                for i3=1:3
                    impp(:,:,i3)=flipud(imp(:,:,i3)');%
                end
                
                im((i2-1)*N_Slice+1:i2*N_Slice,(i1-1)*N+1:i1*N,:)=impp;
                
                
            end
        end
        
        
        figure(h3);
        
        imshow((im));
        
        str=pwd;
        
        if ManualSegTag
            
            stri=[str,'/',FiberName,'_Segmentation'];
            trFile=[stri,'.mat'];
            
            strFigFile=[stri,'_Axl.Fig'];
            hgsave(h1,strFigFile);
            strFigFile=[stri,'_Cor.Fig'];
            hgsave(h2,strFigFile);
            strFigFile=[stri,'_Sag.Fig'];
            hgsave(h3,strFigFile);
            
        else
            trFile=[str,'/ROI.mat'];
            
            strFigFile=[str,'/ROI_Axl.Fig'];
            hgsave(h1,strFigFile);
            strFigFile=[str,'/ROI_Cor.Fig'];
            hgsave(h2,strFigFile);
            strFigFile=[str,'/ROI_Sag.Fig'];
            hgsave(h3,strFigFile);
            
        end
        
    end
    
end



