%*************** Fonctions Segmentation ***************


if strcmp(action,'Seg'),

   inter = {num2str(get(hdl.slide_min,'value')),num2str(get(hdl.slide_max,'value'))};
   answer = inputdlg({'min','max'},'seuil de segmentation',1,inter);
if ~isempty(answer)

   smin = str2num(answer{1})
   smax = str2num(answer{2})

   Vol = Volume(hdl.Num).data(:,:,:);
   Bw = (smin<Vol) & (Vol<smax);
   
   if ~hdl.Bw
      titreBw = strcat('masque :min max : ',num2str([smin smax]));
      hdl.Bw = NewFigure('masque');
      set(hdl.Bw,'Position',[576 364 421 360]);
      set(FigNum,'UserData',hdl);
      figure(FigNum);
  end
end
      
elseif strcmp(action,'LoadMask'),
    [Bw,DESCRIP,M,ORIGIN,dim] = readvol(Result_path,'wher you want from');
nx=dim(1);
   Bw(1:nx,:,:) = Bw(nx:-1:1,:,:);
   %I don't like non numerical ...
   Bw(isnan(Bw))=0;

   if hdl.Bw==0
   	titreBw = strcat('masque :',num2str(size(Bw)));
   	hdl.Bw = NewFigure(titreBw);
	set(hdl.Bw,'Position',[576 364 421 360]);
      	set(FigNum,'UserData',hdl);
        figure(FigNum);         
   end
  
elseif strcmp(action,'LoadBw'),
   cd ([Result_path,'volume',filesep,'Bw'])
      [Fname, Pname] = uigetfile('*.mat', 'where');
   cd (workdir)
   
   if Fname
      Fname = cat(2,Pname,Fname);
      Fname = cat(2,'load ',Fname);
      eval(Fname); %charge une variable Bw
      
	   if hdl.Bw==0
   		titreBw = strcat('masque :',num2str(size(Bw)));
   		hdl.Bw = NewFigure(titreBw);
		set(hdl.Bw,'Position',[576 364 421 360]);
      		set(FigNum,'UserData',hdl);
        	figure(FigNum);         
  	   end
   end
   
   
elseif strcmp(action,'Bw2Pos')
   kp = size(Pos,1)+1;

   for c = 1:NbCoupe
   	[indl indc] = find (Bw(:,:,c));
	   dimc = length(indl)
   	for k = 1:dimc
      	Pos(kp,:) = [indl(k) indc(k) c ];
      	kp = kp+1;
   	end
   end
   Volume(hdl.Num).Pos =Pos;

   

elseif strcmp(action,'Hist'),  
   Im = Volume(hdl.Num).data(:,:,coupe);   
   dim = prod(size(Im));   Im  = reshape(Im,1,dim);
   titre= strcat('histograme de  ',Volume(hdl.Num).titre,num2str(coupe));
   
   hdl.Hist = NewFigure(titre) ;
    hist(Im,200);
 
 
elseif strcmp(action,'Segone'),
   answer = inputdlg({'min','max'},'seuil de segmentation',1,{'150','300'});
   smin = str2num(answer{1})
   smax = str2num(answer{2})

   CoupeVol = Volume(hdl.Num).data(:,:,coupe);
   Bw(:,:,coupe) = (smin<CoupeVol) & (Volcoupe<smax);
elseif strcmp(action,'choose'),
   if hdl.Bw
      figure(hdl.Bw);
      Bw(:,:,coupe) = bwselect(4);
   end
elseif strcmp(action,'rempli'),
   bruit = ~Bw(:,:,coupe);  
   rien  = bwselect(bruit,1,1,4);
   Bw(:,:,coupe) = ~rien;      


        
elseif strcmp(action,'SaveBw'),   
   cd ([Result_path,'volume',filesep,'Bw'])
   [Fname, Pname] = uiputfile('*.mat', 'where');
   if Fname
	   Fname = cat(2,Pname,Fname)
	   Fname = cat(2,'save ',Fname,' Bw');
   	eval(Fname);
   end
   cd (workdir)


elseif strcmp(action,'CloseBw'),
    delete(hdl.Bw);  hdl.Bw=0;
    %Bw=[];on le laisse pour le contour
    set(gcf,'UserData',hdl);

   
elseif strcmp(action,'CleanCont&Bw'),
   delete(hdl.Bw);  hdl.Bw=0;
    
   Bw=[];Cont=[];
   set(gcf,'UserData',hdl);
   Volume(hdl.Num).Cont = Cont;
   
   
end;
