Jun 12, 2012, 12:29:19 AM (12 years ago)

bugs repaired in aver_stat and check_data_file

sub_background largely modified. Tests needed
other functions not yet translated

1 edited


  • trunk/src/series/sub_background.m

    r447 r451  
    11%'sub_background': substract background to an image series, used with series.fig
    3 % function GUI_input=sub_background(Param)
    4 %
    5 %OUTPUT
    6 % GUI_input=list of options in the GUI series.fig needed for the function
    7 %
    8 %INPUT:
    9 %num_i1: series of first indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
    10 %num_i2: series of second indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
    11 %num_j1: series of first indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ )
    12 %num_j2: series of second indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ)
    13 %Series: Matlab structure containing information set by the series interface
    14 %       .RootPath: path to the image series
    15 %       .RootFile: root file name
    16 %       .FileExt: image file extension
    17 %       .NomType: nomenclature type for file indexing
    18 %       .NbSlice: %number of slices defined on the interface
    19 %----------------------------------------------------------------------
    203% Method:
    214    %calculate the background image by sorting the luminosity of each point
    3114    % nbfield2 successive images for a given slice (mode 'multilevel')
    3215    % In the mode 'volume', nbfield2=1 (1 image at each level)
     17% function GUI_input=sub_background(Param)
     19%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
     21% GUI_input=list of options in the GUI series.fig needed for the function
     24% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
     25% In batch mode, Param is the name of the corresponding xml file containing the same information
     26% In the absence of input (as activated when the current Action is selected
     27% in series), the function ouput GUI_input set the activation of the needed GUI elements
     29% Param contains the elements:(use the menu bar command 'export/GUI config' in series to see the current structure Param)
     30%    .InputTable: cell of input file names, (several lines for multiple input)
     31%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
     32%    .OutputSubDir: name of the subdirectory for data outputs
     33%    .OutputDir: directory for data outputs, including path
     34%    .Action: .ActionName: name of the current activated function
     35%             .ActionPath:   path of the current activated function
     36%    .IndexRange: set the file or frame indices on which the action must be performed
     37%    .FieldTransform: .TransformName: name of the selected transform function
     38%                     .TransformPath:   path  of the selected transform function
     39%                     .TransformHandle: corresponding function handle
     40%    .InputFields: sub structure describing the input fields withfields
     41%              .FieldName: name of the field
     42%              .VelType: velocity type
     43%              .FieldName_1: name of the second field in case of two input series
     44%              .VelType_1: velocity type of the second field in case of two input series
     45%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
    3449function GUI_input=sub_background (Param)
    36 %------------------------------------------------------------------------
    37 %requests for the visibility of input windows in the GUI series  (activated directly by the selection in the menu ACTION)
    38 if ~exist('Param','var')
    39     GUI_input={'RootPath';'on';...
    40         'SubDir';'off';... % subdirectory of derived files (PIV fields), ('on' by default)
    41         'RootFile';'on';... %root input file name ('on' by default)
    42         'FileExt';'on';... %inputf file extension ('on' by default)
    43         'NomType';'on';...%type of file indexing ('on' by default)
     51%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
     52if ~exist('Param','var') % case with no input parameter
     53    GUI_input={'NbViewMax';1;...% max nbre of input file series (default='' , no limitation)
     54        'AllowInputSort';'off';...% allow alphabetic sorting of the list of input files (options 'off'/'on', 'off' by default)
    4455        'NbSlice';'on'; ...%nbre of slices ('off' by default)
    45         'OutputDirExt';'.sbk';...
    46         %'VelTypeMenu';'on';...% menu for selecting the velocity type (civ1,..)('off' by default)
    47         %'FieldMenu';'on';...% menu for selecting the velocity field (s) in the input file ('off' by default)
    48         %'VelTypeMenu_1';'on';...% menu for selecting the velocity type (civ1,..)('off' by default)
    49         %'FieldMenu_1';'on';...% menu for selecting the velocity field (s) in the input file ('off' by default)
    50         %'CoordType';...%can use a transform function
    51         %'GetObject';...;%can use projection object
    52         %'GetMask';...;%can use mask option 
    53         %'PARAMETER';'NbSliding';...
    54         %'PARAMETER';'VolumeScan';...
    55         %'PARAMETER';'RankBrightness';...
     56        'VelType';'off';...% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
     57        'FieldName';'off';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
     58        'FieldTransform'; 'off';...%can use a transform function
     59        'ProjObject';'off';...%can use projection object(option 'off'/'on',
     60        'Mask';'off';...%can use mask option   (option 'off'/'on', 'off' by default)
     61        'OutputDirExt';'.sbk';...%set the output dir extension
    5662               ''};
    57     return %exit the function
    58 end
    60 %% input parameters
    61 % read the xml file for batch case
    62 if ischar(Param) && ~isempty(find(regexp('Param','.xml$')))
     63        return
     66%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
     67%% get input parameters, file names and indices
     68% BATCH  case: read the xml file for batch case
     69if ischar(Param) && ~isempty(find(regexp(Param,'.xml$')))
    6370    Param=xml2struct(Param);
    6471    checkrun=0;
    65 else %  RUN case: parameters introduced as the input structure Param
     72% RUN case: parameters introduced as the input structure Param 
    6674    hseries=guidata(Param.hseries);%handles of the GUI series
    67     WaitbarPos=get(hseries.waitbar_frame,'Position');
    68     checkrun=1;
    69 end
    70 filebase=fullfile(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3});
    71 dir_images=Param.InputTable{1,1};
    72 NomType=Param.InputTable{1,4};
    73 SubDir=Param.InputTable{1,2};
    74 FileExt=Param.InputTable{1,5};
    75 [filecell,i1_series,tild,j1_series]=get_file_series(Param);%generates the set of input file names
    76 if size(filecell,1)>1
    77     msgbox_uvmat('WARNING','This function uses only the first input image series')
     75    WaitbarPos=get(hseries.waitbar_frame,'Position');%position of the waitbar on the GUI series
     76    checkrun=1; % indicate the RUN option is used
     78% get the set of input file names (cell array filecell), and the lists of
     79% input file or frame indices i1_series,i2_series,j1_series,j2_series
     81% filecell{iview,fileindex}: cell array representing the list of file names
     82%        iview: line in the table corresponding to a given file series
     83%        fileindex: file index within  the file series,
     84% i1_series(iview,ref_j,ref_i)... are the corresponding arrays of indices i1,i2,j1,j2, depending on the input line iview and the two reference indices ref_i,ref_j
     85% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
     86% set of frame indices used for movie or multimage input
     87if ~isempty(j1_series)
     88    frame_index=j1_series;
     90    frame_index=i1_series;
     93%% root input file(s) and type
     100% numbers of slices and file indices
     102if isfield(Param.IndexRange,'NbSlice')
     103    NbSlice=Param.IndexRange.NbSlice;
     105nbview=size(i1_series,1);%number of input file series (lines in InputTable)
     106nbfield_j=size(i1_series,2); %nb of consecutive fields at each level(burst
     107nbfield=nbfield_j*size(i1_series,3); %total number of files or frames
     108nbfield_i=floor(nbfield/NbSlice);%total number of i indexes (adjusted to an integer number of slices)
     109nbfield=nbfield_i*nbfield_j; %total number of fields after adjustement
     111%determine the file type on each line from the first input file
     115% % for iview=1:nbview
     116%     if ~exist(filecell{iview,1}','file')
     117%         msgbox_uvmat('ERROR',['the first input file ' filecell{iview,1} ' does not exist'])
     118%         return
     119%     end
     120%     [FileType{iview},FileInfo{iview},Object{iview}]=get_file_type(filecell{iview,1});
     121%     CheckImage{iview}=~isempty(find(strcmp(FileType{iview},ImageTypeOptions)));% =1 for images
     122%     CheckNc{iview}=~isempty(find(strcmp(FileType{iview},NcTypeOptions)));% =1 for netcdf files
     123% end
     126CheckImage=~isempty(find(strcmp(FileType,ImageTypeOptions)));% =1 for images
     128%% calibration data and timing: read the ImaDoc files
     129%not relevant here
     131%% check coincidence in time for several input file series
     132%not relevant here
     134%% coordinate transform or other user defined transform
     135%not relevant here
     137%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
     138 % EDIT FROM HERE
     140 %% check the validity of  input file types
     141if CheckImage
     142    FileExtOut='.png'; % write result as .png images for image inputs
     143    NomTypeOut='_1_1';
     145    msgbox_uvmat('ERROR',['invalid file type input: ' FileType{1} ' not an image'])
    78146    return
    81 %% determine input image type
    82 FileType=[];%default
    83 MovieObject=[];
    84 %FileExt=Series.FileExt;
    86 if isequal(lower(FileExt),'.avi')
    87     hhh=which('mmreader');
    88     if ~isequal(hhh,'')&& mmreader.isPlatformSupported()
    89         MovieObject=mmreader(fullfile(RootPath,SubDir,[RootFile FileExt]));
    90         FileType='movie';
    91     else
    92         FileType='avi';
    93     end
    94 elseif isequal(lower(FileExt),'.vol')
    95     FileType='vol';
    96 else
    97     form=imformats(FileExt(2:end));
    98     if ~isempty(form)% if the extension corresponds to an image format recognized by Matlab
    99         if isequal(NomType,'*');
    100             FileType='multimage';
    101         else
    102             FileType='image';
    103         end
    104     end
    105 end
    106 if isempty(FileType)
    107     msgbox_uvmat('ERROR',['invalid file extension ' FileExt ': this function only accepts image or movie input'])
    108     return
    109 end
    111 nbslice_i=Param.NbSlice; %number of slices
    112 siz=size(i1_series{1});
     149% NomTypeOut='_1-2_1';% output file index will indicate the first and last ref index in the series
     150% if NbSlice~=nbfield_j
     151%     answer=msgbox_uvmat('INPUT_Y-N',['will not average slice by slice: for so cancel and set NbSlice= ' num2str(nbfield_j)]);
     152%     if ~strcmp(answer,'Yes')
     153%         return
     154%     end
     155% end
     157%% Set field names and velocity types
     158%not relevant here
     161%% Initiate output fields
     162%not relevant here
     165NbSlice=Param.IndexRange.NbSlice; %number of slices
    113167nbaver_init=23;%approximate number of images used for the sliding background: to be adjusted later to include an integer number of bursts
    120174%% adjust the proposed number of images in the sliding average to include an integer number of bursts
    121 if siz(2)~=1
    122     nbaver=floor(nbaver_init/siz(1)); % number of bursts used for the sliding background,
     175if siz(3)~=1
     176    nbaver=floor(nbaver_init/siz(2)); % number of bursts used for the sliding background,
    123177    if isequal(floor(nbaver/2),nbaver)
    124178        nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined)
    125179    end
    126     nbaver_init=nbaver*siz(1);%propose by default an integer number of bursts
    127 end
    129 %% create dir of the new images
    130 if test_level
    131     term='.sbk.lev';
    132 else
    133     term='.sbk';
    134 end
    135 SubdirResult=[Param.InputTable{1,2} term];
    136 try
    137     mkdir(fullfile(Param.InputTable{1,1},SubdirResult));
    138 catch ME
    139     msgbox_uvmat('ERROR',['error in creating result directory: ' ME.message]);
    140     return
    141 end
    142 [xx,msg2] = fileattrib(fullfile(Param.InputTable{1,1},SubdirResult),'+w','g'); %yield writing access (+w) to user group (g)
    143 if ~strcmp(msg2,'')
    144     msgbox_uvmat('ERROR',['pb of permission for ' fullfile(Param.InputTable{1,1},SubdirResult) ': ' msg2])%error message for directory creation
    145     return
     180    nbaver_init=nbaver*siz(2);%propose by default an integer number of bursts
    149184prompt = {'Number of images for the sliding background (MUST FIT IN COMPUETER MEMORY)';'The number of positions (laser slices)';'volume scan mode (Yes/No)';...
    150185    'the luminosity rank chosen to define the background (0.1=for dense particle seeding, 0.5 (median) for sparse particles'};
    151 dlg_title = ['get (slice by slice) a sliding background and substract to each image, result in subdir ' SubdirResult];
     186dlg_title = ['get (slice by slice) a sliding background and substract to each image, result in subdir ' Param.OutputDir];
    152187num_lines= 3;
    153 def     = { num2str(nbaver_init);num2str(nbslice_i);'No';'0.1'};
     188def     = { num2str(nbaver_init);num2str(NbSlice);'No';'0.1'};
    154189answer = inputdlg(prompt,dlg_title,num_lines,def);
    155190set(hseries.ParamVal,'String',answer([1 [3:4]]))
    158193nbaver_ima=str2num(answer{1});%number of images for the sliding background
    159 nbaver=ceil(nbaver_ima/siz(1));%number of bursts for the sliding background
     194nbaver=ceil(nbaver_ima/siz(2));%number of bursts for the sliding background
    160195if isequal(floor(nbaver/2),nbaver)
    161196    nbaver=nbaver+1;%put the number of burst to an odd number (so the middle burst is defined)
    163 step=siz(1);%case of bursts: the sliding background is shifted by one burst
     198step=siz(2);%case of bursts: the sliding background is shifted by one burst
    165200if isequal(vol_test,'Yes')
    166201    nbfield2=1;%case of volume: no consecutive series at a given level
    167     nbslice_i=siz(1);%number of slices
     202    NbSlice=siz(2);%number of slices
    169     nbfield2=siz(1); %nb of consecutive images at each level(burst)
    170     if siz(2)>1
    171         % nbslice_i=str2num(answer{2})/(num_i1(1,2)-num_i1(1,1));% number of slices
    172         nbslice_i=str2num(answer{2})/(i1_series{1}(1,2)-i1_series{1}(1,1));% number of slices
     204    nbfield2=siz(2); %nb of consecutive images at each level(burst)
     205    if siz(3)>1
     206        % NbSlice=str2num(answer{2})/(num_i1(1,2)-num_i1(1,1));% number of slices
     207        NbSlice=str2num(answer{2})/(i1_series(1,1,2)-i1_series(1,1,1));% number of slices
    173208    else
    174         nbslice_i=1;
    175     end
    176     if ~isequal(floor(nbslice_i),nbslice_i)
     209        NbSlice=1;
     210    end
     211    if ~isequal(floor(NbSlice),NbSlice)
    177212        msgbox_uvmat('ERROR','the number of slices must be a multiple of the i increment')
    178213        return
    183218    rank=1;%rank selected in the sorted image series
    185 lengthtot=siz(1)*siz(2);
    186 nbfield=floor(lengthtot/(nbfield2*nbslice_i));%total number of i indexes (adjusted to an integer number of slices)
     221nbfield=floor(lengthtot/(nbfield2*NbSlice));%total number of i indexes (adjusted to an integer number of slices)
    187222nbfield_slice=nbfield*nbfield2;% number of fields per slice
    188223if nbaver_ima > nbfield*nbfield2
    199234%% prealocate memory for the sliding background
    200 % first_image=name_generator(filebase,num_i1(1),num_j1(1),FileExt,NomType);
    201 % Afirst=read_image(first_image,FileType,num_i1(1),MovieObject);
    202 Afirst=read_image(filecell{1,1},FileType,i1_series{1}(1),MovieObject);
    212245%% update the xml file
    213246SubDirBase=regexprep(Param.InputTable{1,2},'\..*','');%take the root part of SubDir, before the first dot '.'
    214 filexml=fullfile(Param.InputTable{1,1},[SubDirBase '.xml']);
     247filexml=fullfile(RootPath{1},[SubDirBase '.xml']);
    215248if ~exist(filexml,'file') && exist([filebase '.xml'],'file')% xml inside the image directory
    216249    copyfile([filebase '.xml'],filexml);% copy the .xml file
    227260        j1=j1_series{1}(1);
    228261    end
    229     ImageName=fullfile_uvmat([dir_images term],'',Param.InputTable{1,3},'.png',NomType,i1_series{1}(1),[],j1);
    230     %     ImageName=name_generator(filebase_b,num_i1(1),num_j1(1),'.png',NomType);
     262    ImageName=fullfile_uvmat([dir_images term],'',RootFile{1},'.png',NomType,i1_series(1,1),[],j1);
    231263    [pth,ImageName]=fileparts(ImageName);
    232264    ImageName=[ImageName '.png'];
    250282    end
    251283    [t,NbSlice_uid]=add(t,new_uid,'element','NbSlice');
    252     [t]=add(t,new_uid,'chardata',num2str(nbslice_i));
     284    [t]=add(t,new_uid,'chardata',num2str(NbSlice));
    253285    [t,NbSlidingImages_uid]=add(t,new_uid,'element','NbSlidingImages');
    254286    [t]=add(t,NbSlidingImages_uid,'chardata',num2str(nbaver));
    265 for islice=1:nbslice_i
     297for islice=1:NbSlice
    266298    %% select the series of image indices at the level islice
    267299    for ifield=1:nbfield
    268300        for iburst=1:nbfield2
    269             indselect(iburst,ifield)=((ifield-1)*nbslice_i+(islice-1))*nbfield2+iburst;
     301            indselect(iburst,ifield)=((ifield-1)*NbSlice+(islice-1))*nbfield2+iburst;
    270302        end
    271303    end
    275307        ifile=indselect(ifield);
    276308        filename=filecell{1,ifile};
    277         % filename=name_generator(filebase,num_i1(ifile),num_j1(ifile),FileExt,NomType);
    278         Aread=read_image(filename,FileType,i1_series{1}(ifile),MovieObject);
     309        Aread=read_image(filename,FileType,MovieObject,i1_series{1}(ifile));
    279310        Ak(:,:,ifield)=Aread;
    280311    end
    291322            j1=j1_series{1}(ifile);
    292323        end
    293         newname=fullfile_uvmat(Param.InputTable{1,1},SubdirResult,Param.InputTable{1,3},'.png',NomType,i1_series{1}(ifile),[],j1);
     324        newname=fullfile_uvmat(RootPath{1},Param.OutputSubDir,RootFile{1},FileExtOut,NomTypeOut,i1_series(1,ifile),[],i_slice,[]);
     325%         newname=fullfile_uvmat(RootPath{1},SubdirResult,RootFile{1},'.png',NomType,i1_series{1}(ifile),[],j1);
    294326        %newname=name_generator(filebase_b,i1_series{1}(ifile),j1_series{1}(ifile),'.png',NomType);% makes the new file name
    295327        if test_level
    307339            if checkrun
    308340                stopstate=get(hseries.RUN,'BusyAction');
    309                 update_waitbar(hseries.waitbar_frame,WaitbarPos,(ifield+(islice-1)*nbfield_slice)/(nbfield_slice*nbslice_i))
     341                update_waitbar(hseries.waitbar_frame,WaitbarPos,(ifield+(islice-1)*nbfield_slice)/(nbfield_slice*NbSlice))
    310342            else
    311343                stopstate='queue';
    316348                for iburst=1:step
    317349                    ifile=indselect(ifield+step*floor(nbaver/2)+iburst-1);
    318                     filename=fullfile_uvmat(Param.InputTable{1,1},SubDir,Param.InputTable{1,3},FileExt,NomType,i1_series{1}(ifile),[],j1_series{1}(ifile));
     350                    filename=fullfile_uvmat(RootPath{1},SubDir,RootFile{1},FileExt,NomType,i1_series(1,ifile),[],j1_series(1,ifile));
    319351                    %filename=name_generator(filebase,num_i1(ifile),num_j1(ifile),FileExt,NomType);
    320                     Aread=read_image(filename,FileType,i1_series{1}(ifile),MovieObject);
     352                    Aread=read_image(filename,FileType,MovieObject,i1_series(1,ifile));
    321353                    Ak(:,:,nbaver_ima-step+iburst)=Aread;
    322354                end
    332364                        j1=j1_series{1}(ifile);
    333365                    end
    334                     newname=fullfile_uvmat(Param.InputTable{1,1},SubdirResult,Param.InputTable{1,3},'.png',NomType,i1_series{1}(ifile),[],j1);
     366                    newname=fullfile_uvmat(RootPath{1},Param.OutputSubDir,RootFile{1},FileExtOut,NomTypeOut,i1_series(1,1),[],i_slice,[]);
     367                   % newname=fullfile_uvmat(Param.InputTable{1,1},SubdirResult,Param.InputTable{1,3},'.png',NomType,i1_series{1}(ifile),[],j1);
    335368                    %[newname]=name_generator(filebase_b,num_i1(ifile),num_j1(ifile),'.png',NomType) % makes the new file name
    336369                    if test_level
    359392            j1=j1_series{1}(ifile);
    360393        end
    361         newname=fullfile_uvmat(Param.InputTable{1,1},SubdirResult,Param.InputTable{1,3},'.png',NomType,i1_series{1}(ifile),[],j1);
     394        newname=fullfile_uvmat(RootPath{1},Param.OutputSubDir,RootFile{1},FileExtOut,NomTypeOut,i1_series(1,ifile),[],j1);
     395%         newname=fullfile_uvmat(Param.InputTable{1,1},SubdirResult,Param.InputTable{1,3},'.png',NomType,i1_series{1}(ifile),[],j1);
    362396        if test_level
    363397            C=levels(C);
    377411%--read images and convert them to the uint16 format used for PIV
    378 function A=read_image(filename,type_ima,num,MovieObject)
     412function A=read_image(FileName,FileType,VideoObject,num)
    380414%num is the view number needed for an avi movie
    381 switch type_ima
    382     case 'movie'
    383         A=read(MovieObject,num);
    384     case 'avi'
    385         mov=aviread(filename,num);
    386         A=frame2im(mov(1));
     415switch FileType
     416    case {'video','mmreader'}
     417        A=read(VideoObject,num);
    387418    case 'multimage'
    388         A=imread(filename,num);
     419        A=imread(FileName,num);
    389420    case 'image'   
    390         A=imread(filename);
     421        A=imread(FileName);
    393424if length(siz)==3;%color images
    394     A=sum(double(A),3);
     425    A=sum(double(A),3);% take the sum of color components
Note: See TracChangeset for help on using the changeset viewer.