Ignore:
Timestamp:
Mar 6, 2011, 10:22:33 PM (13 years ago)
Author:
sommeria
Message:

relabel_i_j and sub_background improved. Possibility of using levels

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/series/sub_background.m

    r169 r214  
    5050        %'GetObject';...;%can use projection object
    5151        %'GetMask';...;%can use mask option 
    52         'PARAMETER';'NbSliding';...
    53         'PARAMETER';'VolumeScan';...
    54         'PARAMETER';'RankBrightness';...
     52        %'PARAMETER';'NbSliding';...
     53        %'PARAMETER';'VolumeScan';...
     54        %'PARAMETER';'RankBrightness';...
    5555               ''};
    5656    return %exit the function
     
    5858
    5959%----------------------------------------------------------------
    60 % initiate the waitbar
     60%% initiate the waitbar
    6161hseries=guidata(Series.hseries);%handles of the GUI series
    6262WaitbarPos=get(hseries.waitbar_frame,'Position');
     
    6767end
    6868
    69 %determine input image type
     69%% determine input image type
    7070FileType=[];%default
    7171MovieObject=[];
     
    101101nbaver_init=23;%approximate number of images used for the sliding background: to be adjusted later to include an integer number of bursts
    102102
    103 %adjust the proposed number of images in the sliding average to include an integer number of bursts
     103
     104%% apply the image rescaling function 'level' (avoid the blinking effects of bright particles)
     105answer=msgbox_uvmat('INPUT_Y-N','apply image rescaling function levels.m after sub_background');
     106test_level=isequal(answer,'Yes');
     107
     108%% adjust the proposed number of images in the sliding average to include an integer number of bursts
    104109if siz(2)~=1
    105110    nbaver=floor(nbaver_init/siz(1)); % number of bursts used for the sliding background,
     
    114119nom_type=Series.NomType;
    115120
    116 %create dir of the new images
    117 [dir_images,namebase]=fileparts(filebase);
    118 [path,subdir_ima]=fileparts(dir_images);
    119 curdir=pwd;
    120 cd(path);
    121 mkdir([subdir_ima '_b']);
    122 [xx,msg2] = fileattrib([subdir_ima '_b'],'+w','g'); %yield writing access (+w) to user group (g)
     121%% create dir of the new images
     122% [dir_images,namebase]=fileparts(filebase);
     123if test_level
     124    term='_b_levels';
     125else
     126    term='_b';
     127end
     128[pp,subdir_ima]=fileparts(Series.RootPath);
     129try
     130    mkdir([dir_images term]);
     131catch
     132    errormsg=lasterr
     133            msgbox_uvmat('ERROR',errormsg);
     134            return
     135end
     136[xx,msg2] = fileattrib([dir_images term],'+w','g'); %yield writing access (+w) to user group (g)
    123137if ~strcmp(msg2,'')
    124     msgbox_uvmat('ERROR',['pb of permission for ' subdir_ima '_b' ': ' msg2])%error message for directory creation
    125     cd(curdir)
     138    msgbox_uvmat('ERROR',['pb of permission for ' subdir_ima term ': ' msg2])%error message for directory creation
    126139    return
    127140end
    128 cd(curdir);
    129 filebase_b=fullfile(path,[subdir_ima '_b'],namebase);
    130 
     141filebase_b=fullfile([dir_images term],Series.RootFile);
     142
     143%% set processing parameters
    131144prompt = {'Number of images for the sliding background';'The number of positions (laser slices)';'volume scan mode (Yes/No)';...
    132145        'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles'};
    133 dlg_title = ['get (slice by slice) a sliding background and substract to each image, result in subdir ' subdir_ima '_b'];
     146dlg_title = ['get (slice by slice) a sliding background and substract to each image, result in subdir ' subdir_ima term];
    134147num_lines= 3;
    135148def     = { num2str(nbaver_init);num2str(nbslice_i);'No';'0.1'};
     
    139152
    140153nbaver_ima=str2num(answer{1});%number of images for the sliding background
    141 nbaver=ceil(nbaver_ima/siz(1))%number of bursts for the sliding background
     154nbaver=ceil(nbaver_ima/siz(1));%number of bursts for the sliding background
    142155if isequal(floor(nbaver/2),nbaver)
    143156   nbaver=nbaver+1%put the number of burst to an odd number (so the middle burst is defined)
    144157end
    145 % if isequal(nbaver,round(nbaver))
    146158step=siz(1);%case of bursts: the sliding background is shifted by one burst
    147 % else
    148 %    step=1;
    149 % end
    150159vol_test=answer{3};
    151160if isequal(vol_test,'Yes')
     
    164173    end
    165174end
    166 % nbaver=floor(nbaver_init/nbfield2); % number of bursts used for the sliding background,
    167 % if isequal(floor(nbaver/2),nbaver)
    168 %     nbaver=nbaver+1;%put the number of burst to an odd number
    169 % end
    170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    171 %nbaver_ima=nbaver*nbfield2;% adjust the number of sliding images A  REMETRE
    172 % if ~isequal(nbaver_ima,nbaver_init)
    173 %     hwarn=warndlg(['number of images in the sliding average adjusted to ' num2str(nbaver_ima)]);
    174 %     set(hwarn,'Units','normalized')
    175 %     set(hwarn,'Position',[0.3 0.3 0.4 0.1])
    176 % end
    177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    178175rank=floor(str2num(answer{4})*nbaver_ima);
    179176if rank==0
     
    183180nbfield=floor(lengthtot/(nbfield2*nbslice_i));%total number of i indexes (adjusted to an integer number of slices)
    184181nbfield_slice=nbfield*nbfield2;% number of fields per slice
    185 % test_plot=isequal(answer{5},'Yes'); %=1 to display background images
    186182if nbaver_ima > nbfield*nbfield2
    187183    errordlg('number of images in a slice smaller than the proposed number of images for the sliding average')
     
    190186nbfirst=(ceil(nbaver/2))*step;
    191187if nbfirst>nbaver_ima
    192     nbfirst=ceil(nbaver_ima/2)
     188    nbfirst=ceil(nbaver_ima/2);
    193189    step=1;
    194190    nbaver=nbaver_ima;
    195191end
    196192
    197 %copy the xml file
     193%% copy the xml file
    198194if exist([filebase '.xml'],'file')
    199195    copyfile([filebase '.xml'],[filebase_b '.xml']);% copy the .civ file
     
    223219    [t,NameFunction_uid]=add(t,new_uid,'element','NameFunction');
    224220    [t]=add(t,NameFunction_uid,'chardata','sub_background');     
     221    if test_levels
     222            [t,NameFunction_uid]=add(t,new_uid,'element','NameFunction');
     223            [t]=add(t,NameFunction_uid,'chardata','levels');
     224    end
    225225    [t,NbSlice_uid]=add(t,new_uid,'element','NbSlice');
    226226    [t]=add(t,new_uid,'chardata',num2str(nbslice_i));
     
    240240%MAIN LOOP ON SLICES
    241241for islice=1:nbslice_i
    242     %select the series of image indices at the level islice
     242    %% select the series of image indices at the level islice
    243243    for ifield=1:nbfield
    244244        for iburst=1:nbfield2
     
    246246        end
    247247    end 
    248     %read the first series of nbaver_ima images and sort by luminosity at each pixel
     248   
     249    %% read the first series of nbaver_ima images and sort by luminosity at each pixel
    249250    for ifield = 1:nbaver_ima
    250251        ifile=indselect(ifield);
     
    255256    Asort=sort(Ak,3);%sort the luminosity of images at each point
    256257    B=Asort(:,:,rank);%background image
    257 %
    258 %     namemean=name_generator([filebase '_back'],islice,[],'.png','_i');% makes the file name
    259 %     imwrite(B,namemean,'BitDepth',16); % save the first background image
    260 %     ['background image for slice ' num2str(islice) ' saved in ' namemean]
    261     %substract the background from each of the first images and save the new images
    262 %     for ifield=1:floor(nbaver_ima/2)+1
    263     'first background image will be substracted'
    264 
     258   display( 'first background image will be substracted')
    265259    for ifield=1:nbfirst
    266260            Acor=double(Ak(:,:,ifield))-double(B);%substract background to the current image
     
    269263            ifile=indselect(ifield);
    270264            newname=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',nom_type)% makes the new file name
    271             imwrite(C,newname,'BitDepth',16); % save the new image
    272     end
    273     %repeat the operation on a sliding series of nbaver*nbfield2 images
    274     'sliding background image will be substracted'
     265            if test_level
     266                C=levels(C);
     267                 imwrite(C,newname,'BitDepth',8); % save the new image
     268            else
     269                 imwrite(C,newname,'BitDepth',16); % save the new image
     270            end
     271    end
     272   
     273    %% repeat the operation on a sliding series of nbaver*nbfield2 images
     274    display('sliding background image will be substracted')
    275275    if nbfield_slice > nbaver_ima
    276 %         for ifield = floor(nbaver_ima/2)+2:step:nbfield_slice-floor(nbaver_ima/2)
    277276        for ifield = step*ceil(nbaver/2)+1:step:nbfield_slice-step*floor(nbaver/2)
    278277            stopstate=get(hseries.RUN,'BusyAction');
     
    291290                B=Asort(:,:,rank);%current background image
    292291                for iburst=1:step
    293 %                     Acor=double(Ak(:,:,floor(nbaver_ima/2)+iburst-1))-double(B);
    294292                    index=step*floor(nbaver/2)+iburst;
    295293                    Acor=double(Ak(:,:,index))-double(B);
     
    298296                    ifile=indselect(ifield+iburst-1);
    299297                    [newname]=...
    300                        name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',Series.NomType)
    301 %                     newname=name_generator(filebase_b,num_i1(indselect(ifield+iburst-1)),num_j1(indselect(ifield+iburst-1)),'.png',nom_type)% makes the new file name
    302                     imwrite(C,newname,'BitDepth',16); % save the new image
    303                 end 
     298                        name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',Series.NomType) % makes the new file name
     299                    if test_level
     300                        C=levels(C);
     301                        imwrite(C,newname,'BitDepth',8); % save the new image
     302                    else
     303                        imwrite(C,newname,'BitDepth',16); % save the new image
     304                    end
     305                end
    304306            else
    305307                return
     
    308310    end
    309311
    310 %substract the background from the last images
    311 %     for ifield=nbfield_slice-floor(nbaver_ima/2)+1:nbfield_slice
    312      'last background image will be substracted'
     312%% substract the background from the last images
     313    display('last background image will be substracted')
    313314     ifield=nbfield_slice-(step*ceil(nbaver/2))+1:nbfield_slice;
    314     for ifield=nbfield_slice-(step*floor(nbaver/2))+1:nbfield_slice 
    315         index=ifield-nbfield_slice+step*(2*floor(nbaver/2)+1);
    316         Acor=double(Ak(:,:,index))-double(B);
    317         Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
    318         C=uint16(Acor);
    319         ifile=indselect(ifield);
    320         newname=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',nom_type)% makes the new file name
    321         imwrite(C,newname,'BitDepth',16); % save the new image
    322     end
     315     for ifield=nbfield_slice-(step*floor(nbaver/2))+1:nbfield_slice
     316         index=ifield-nbfield_slice+step*(2*floor(nbaver/2)+1);
     317         Acor=double(Ak(:,:,index))-double(B);
     318         Acor=(Acor>0).*Acor; % put to 0 the negative elements in Acor
     319         C=uint16(Acor);
     320         ifile=indselect(ifield);
     321         newname=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',nom_type)% makes the new file name
     322         if test_level
     323             C=levels(C);
     324             imwrite(C,newname,'BitDepth',8); % save the new image
     325         else
     326             imwrite(C,newname,'BitDepth',16); % save the new image
     327         end
     328     end
    323329end
    324330
     
    348354end
    349355   
     356
     357function C=levels(A)
     358%whos A;
     359B=double(A(:,:,1));
     360windowsize=round(min(size(B,1),size(B,2))/20);
     361windowsize=floor(windowsize/2)*2+1;
     362ix=[1/2-windowsize/2:-1/2+windowsize/2];%
     363%del=np/3;
     364%fct=exp(-(ix/del).^2);
     365fct2=cos(ix/(windowsize-1)/2*pi/2);
     366%Mfiltre=(ones(5,5)/5^2);
     367%Mfiltre=fct2';
     368Mfiltre=fct2'*fct2;
     369Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
     370
     371C=filter2(Mfiltre,B);
     372C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
     373C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
     374C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
     375C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
     376C=tanh(B./(2*C));
     377[n,c]=hist(reshape(C,1,[]),100);
     378% figure;plot(c,n);
     379
     380[m,i]=max(n);
     381c_max=c(i);
     382[dummy,index]=sort(abs(c-c(i)));
     383n=n(index);
     384c=c(index);
     385i_select = find(cumsum(n)<0.95*sum(n));
     386if isempty(i_select)
     387    i_select = 1:length(c);
     388end
     389c_select=c(i_select);
     390n_select=n(i_select);
     391cmin=min(c_select);
     392cmax=max(c_select);
     393C=(C-cmin)/(cmax-cmin)*256;
     394C=uint8(C);
Note: See TracChangeset for help on using the changeset viewer.