source: trunk/src/series/extract_multitif_multislice.m @ 1129

Last change on this file since 1129 was 1129, checked in by sommeria, 3 months ago

LIF calibration update and extrct_multitif

File size: 10.9 KB
Line 
1%'extract_multitif_multiplane': read image series from PCO cameras (tiff image series) and write .png images
2% case of multislice views with a distinct geometric calibration for each slice
3%------------------------------------------------------
4% the output file indexing is based on the xml file requested by the
5% function when it is selected (or possibly inserted in this function in the section TEST)
6%  This xml file must contain the following information:
7%   NbDti: the number of slices -1
8%   NbDtj: number of frames in each slice -1
9%   NbDtk: number of repetitions of the slice scanning process -1
10% Therefore the total number of frames is  (NbDti+1)*(NbDtj+1)*(NbDtk+1)
11% The frame series in each slice is stored in a separate folder labeled by_i, i=1:NbDti+1,
12% an xml file with the same name must be created with the appropiate geometric calibration and  time interval Dti between
13% successive image names in each folder are labeled as _1k_1,_1k_2...._2k_1,_2k_2,...
14%
15% To run the function in the cluster in parallel for each multitif file, indicate nb-slice_i equal to the
16% number input multitif files
17
18%------------------------------------------------------------------------
19   
20% function ParamOut=extract_multitif_multiplane(Param)
21%
22%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
23%
24%OUTPUT
25% ParamOut: sets options in the GUI series.fig needed for the function
26%
27%INPUT:
28% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
29% In batch mode, Param is the name of the corresponding xml file containing the same information
30% when Param.Action.RUN=0 (as activated when the current Action is selected
31% in series), the function ouput paramOut set the activation of the needed GUI elements
32%
33% Param contains the elements:(use the menu bar command 'export/GUI config' in series to
34% see the current structure Param)
35%    .InputTable: cell of input file names, (several lines for multiple input)
36%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
37%    .OutputSubDir: name of the subdirectory for data outputs
38%    .OutputDirExt: directory extension for data outputs
39%    .Action: .ActionName: name of the current activated function
40%             .ActionPath:   path of the current activated function
41%             .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled   Matlab fct
42%             .RUN =0 for GUI input, =1 for function activation
43%             .RunMode='local','background', 'cluster': type of function  use
44%             
45%    .IndexRange: set the file or frame indices on which the action must be performed
46%    .FieldTransform: .TransformName: name of the selected transform function
47%                     .TransformPath:   path  of the selected transform function
48%    .InputFields: sub structure describing the input fields withfields
49%              .FieldName: name(s) of the field
50%              .VelType: velocity type
51%              .FieldName_1: name of the second field in case of two input series
52%              .VelType_1: velocity type of the second field in case of two input series
53%              .Coord_y: name of y coordinate variable
54%              .Coord_x: name of x coordinate variable
55%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
56%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57
58%=======================================================================
59% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
60%   http://www.legi.grenoble-inp.fr
61%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
62%
63%     This file is part of the toolbox UVMAT.
64%
65%     UVMAT is free software; you can redistribute it and/or modify
66%     it under the terms of the GNU General Public License as published
67%     by the Free Software Foundation; either version 2 of the license,
68%     or (at your option) any later version.
69%
70%     UVMAT is distributed in the hope that it will be useful,
71%     but WITHOUT ANY WARRANTY; without even the implied warranty of
72%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
73%     GNU General Public License (see LICENSE.txt) for more details.
74%=======================================================================
75
76function ParamOut=extract_multitif(Param)
77
78%%%%%%%%%%%%%%%%%    INPUT PREPARATION MODE (no RUN)    %%%%%%%%%%%%%%%%%
79if isstruct(Param) && isequal(Param.Action.RUN,0)
80    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
81    ParamOut.WholeIndexRange='on';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
82    ParamOut.NbSlice='on'; % impose calculation in a single process (no parallel processing to avoid 'holes'))
83    ParamOut.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
84    ParamOut.FieldName='off';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
85    ParamOut.FieldTransform = 'off';%can use a transform function
86    ParamOut.ProjObject='off';%can use projection object(option 'off'/'on',
87    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
88    ParamOut.OutputDirExt='.png';%set the output dir extension
89    ParamOut.OutputFileMode='NbSlice';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice
90    ParamOut.CheckOverwriteVisible='on'; % manage the overwrite of existing files (default=1)
91    ParamOut.CPUTime=10;% expected time for writting the output of one source image ( in minute)
92    %% root input file(s) and type
93    % check the existence of the first file in the series
94        first_j=[];% note that the function will propose to cover the whole range of indices
95    if isfield(Param.IndexRange,'MinIndex_j'); first_j=Param.IndexRange.MinIndex_j; end
96    last_j=[];
97    if isfield(Param.IndexRange,'MaxIndex_j'); last_j=Param.IndexRange.MaxIndex_j; end
98    PairString='';
99    if isfield(Param.IndexRange,'PairString'); PairString=Param.IndexRange.PairString; end
100    [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,PairString);
101    FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
102        Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
103    if ~exist(FirstFileName,'file')
104        msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist'])
105    end
106
107    %% check the validity of  input file types
108    FileInfo=get_file_info(FirstFileName);
109    if ~strcmp(FileInfo.FileType,'multimage')
110        msgbox_uvmat('ERROR',['invalid file type input: ' FileInfo.FileType ' not an image'])
111        return
112    end
113    ParamOut.ActionInput.XmlFile=uigetfile_uvmat('pick xml file for timing',fileparts(fileparts(FirstFileName)),'.xml'); 
114    return
115end
116%%%%%%%%%%%%%%%%%    STOP HERE FOR PAMETER INPUT MODE   %%%%%%%%%%%%%%%%%
117
118%% read input parameters from an xml file if input is a file name (batch mode)
119checkrun=1;
120RUNHandle=[];
121WaitbarHandle=[];
122if ischar(Param)
123    Param=xml2struct(Param);% read Param as input file (batch case)
124    checkrun=0;
125else
126    hseries=findobj(allchild(0),'Tag','series');
127    RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
128    WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
129end
130
131%% Timing
132XmlInputFile=Param.ActionInput.XmlFile;
133[XmlInput,errormsg]=imadoc2struct(XmlInputFile,'Camera');
134if ~isempty(errormsg)
135    disp(['bad xml input file: ' errormsg])
136    return
137end
138ImagesPerLevel=size(XmlInput.Time,2)-1;%100;%use the xmlinformation to get the nbre of j indices
139
140%% output directory
141Nbre_level=XmlInput.Camera.BurstTiming.NbDti+1;
142for idir=1:Nbre_level
143OutputDir{idir}=fullfile(Param.InputTable{1,1},[Param.OutputSubDir '_' num2str(idir) Param.OutputDirExt]);
144if ~exist(OutputDir{idir},'dir')
145    mkdir(OutputDir{idir})
146end
147end
148
149%% create the xml file for timing if it does not exist : example to adapt
150TEST=0;
151if TEST
152    count0=14;
153    Dtj=0.05;% time interval between frames
154    ImagesPerLevel=455;% total number of images per position, ImagesPerLevel-Nbj images skiiped during motion between two positions
155    Nbj=390; %Nbre of images kept at a given position
156    Dti=Dtj*ImagesPerLevel;
157    NbLevel=11;
158    NbScan=3;
159    TimeReturn=268.5; %time needed to return back to the first position (in sec)
160    NbReturn=round(TimeReturn/Dtj);
161    NbSkipReturn=NbReturn+1-NbLevel*ImagesPerLevel;
162   
163    Newxml=fullfile(Param.InputTable{1,1},[Param.InputTable{1,2} '.xml']);
164    if ~exist(Newxml,'file')
165        XmlInput.Camera.CameraName='PCO';
166        XmlInput.Camera.TimeUnit='s';
167        XmlInput.Camera.BurstTiming.FrameFrequency=1;
168        XmlInput.Camera.BurstTiming.Time=0;% for 200
169        XmlInput.Camera.BurstTiming.Dtj=Dtj;
170        XmlInput.Camera.BurstTiming.NbDtj=Nbj-1;
171        XmlInput.Camera.BurstTiming.Dti=Dti;
172        XmlInput.Camera.BurstTiming.NbDti=NbLevel-1;
173        XmlInput.Camera.BurstTiming.Dtk=TimeReturn;
174        XmlInput.Camera.BurstTiming.NbDtk=NbScan-1;
175        t=struct2xml(XmlInput);
176        t=set(t,1,'name','ImaDoc');
177        save(t,Newxml);
178    end
179end
180
181%% loop on the files
182% include the first tiff file with no index in the first iteration
183if Param.IndexRange.first_i==1% first slice of processing
184    firstindex=0;% to read the first multitif with no number
185   count=0;
186else
187    firstindex=Param.IndexRange.first_i;
188    ImageName=fullfile(Param.InputTable{1,1},Param.InputTable{1,2},'im.tif');
189    NbFrames=numel(imfinfo(ImageName));%nbre of frames in each multitif image
190   count=Param.IndexRange.first_i*NbFrames;
191end
192for ifile=firstindex:Param.IndexRange.last_i
193    tic
194    if firstindex==0 && ifile==0% first slice of processing
195        ImageName=fullfile(Param.InputTable{1,1},Param.InputTable{1,2},'im.tif')
196    else
197        ImageName=fullfile(Param.InputTable{1,1},Param.InputTable{1,2},['im@' num2str(ifile,'%04d') '.tif'])
198    end
199    NbFrames=numel(imfinfo(ImageName));% nbre of frames in each multitif file
200    for iframe=1:NbFrames
201        if isequal(ImagesPerLevel,1)% mode series
202            OutputFile=fullfile(OutputDir,['img_' num2str(count+1) '.png']);
203        else % indices i and j
204            i_index=fix(count/ImagesPerLevel)+1;
205            dir_index=mod(i_index-1,XmlInput.Camera.BurstTiming.NbDti+1)+1;
206            inew=floor((i_index-1)/(XmlInput.Camera.BurstTiming.NbDti+1))+1;
207            j_index=mod(count,ImagesPerLevel)+1;
208            OutputFile=fullfile(OutputDir{dir_index},['img_' num2str(inew) 'k_' num2str(j_index) '.png']);
209        end
210        if Param.CheckOverwrite ||~exist(OutputFile,'file')
211           A=imread(ImageName,iframe);
212           imwrite(A,OutputFile,'BitDepth',16);
213            disp([OutputFile ' written'])
214        else
215            disp([OutputFile ' already exists'])
216        end
217        count=count+1;
218    end
219    tt=toc;
220    disp(['elapsed time (in min.) for the file im@' num2str(ifile,'%04d')])
221    disp(num2str(tt/60))
222end
223
224
225
Note: See TracBrowser for help on using the repository browser.