[823] | 1 | % 'movie2png': copy a movie to a series of grey scale .png images
|
---|
| 2 | %------------------------------------------------------------------------
|
---|
| 3 | % function ParamOut=movie2png(Param)
|
---|
| 4 | %------------------------------------------------------------------------
|
---|
| 5 | %
|
---|
| 6 | %OUTPUT
|
---|
| 7 | % ParamOut: sets options in the GUI series.fig needed for the function
|
---|
| 8 | %
|
---|
| 9 | %INPUT:
|
---|
| 10 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
|
---|
| 11 | % In batch mode, Param is the name of the corresponding xml file containing the same information
|
---|
| 12 | % when Param.Action.RUN=0 (as activated when the current Action is selected
|
---|
| 13 | % in series), the function ouput paramOut set the activation of the needed GUI elements
|
---|
| 14 | %
|
---|
| 15 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to
|
---|
| 16 | % see the current structure Param)
|
---|
| 17 | % .InputTable: cell of input file names, (several lines for multiple input)
|
---|
| 18 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
|
---|
| 19 | % .OutputSubDir: name of the subdirectory for data outputs
|
---|
| 20 | % .OutputDirExt: directory extension for data outputs
|
---|
| 21 | % .Action: .ActionName: name of the current activated function
|
---|
| 22 | % .ActionPath: path of the current activated function
|
---|
| 23 | % .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled Matlab fct
|
---|
| 24 | % .RUN =0 for GUI input, =1 for function activation
|
---|
| 25 | % .RunMode='local','background', 'cluster': type of function use
|
---|
| 26 | %
|
---|
| 27 | % .IndexRange: set the file or frame indices on which the action must be performed
|
---|
| 28 | % .FieldTransform: .TransformName: name of the selected transform function
|
---|
| 29 | % .TransformPath: path of the selected transform function
|
---|
| 30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
| 31 |
|
---|
| 32 | %=======================================================================
|
---|
| 33 | % Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France
|
---|
| 34 | % http://www.legi.grenoble-inp.fr
|
---|
| 35 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
|
---|
| 36 | %
|
---|
| 37 | % This file is part of the toolbox UVMAT.
|
---|
| 38 | %
|
---|
| 39 | % UVMAT is free software; you can redistribute it and/or modify
|
---|
| 40 | % it under the terms of the GNU General Public License as published
|
---|
| 41 | % by the Free Software Foundation; either version 2 of the license,
|
---|
| 42 | % or (at your option) any later version.
|
---|
| 43 | %
|
---|
| 44 | % UVMAT is distributed in the hope that it will be useful,
|
---|
| 45 | % but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 46 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 47 | % GNU General Public License (see LICENSE.txt) for more details.
|
---|
| 48 | %=======================================================================
|
---|
| 49 | function ParamOut=movie2png(Param)
|
---|
| 50 |
|
---|
| 51 | %% set the input elements needed on the GUI series when the function is selected in the menu ActionName or InputTable refreshed
|
---|
| 52 | if isstruct(Param) && isequal(Param.Action.RUN,0)
|
---|
| 53 | ParamOut.FieldTransform = 'on';%can use a transform function
|
---|
| 54 | ParamOut.TransformPath=fullfile(fileparts(which('uvmat')),'transform_field');% path to transform functions (needed for compilation only)
|
---|
| 55 | ParamOut.OutputDirExt='.png';%set the output dir extension
|
---|
| 56 | ParamOut.OutputFileMode='NbInput';% '=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice
|
---|
| 57 |
|
---|
| 58 | first_j=[];
|
---|
| 59 | if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end
|
---|
| 60 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,'');
|
---|
| 61 | FirstFileName=fullfile_uvmat(Param.InputTable{1,1},Param.InputTable{1,2},Param.InputTable{1,3},...
|
---|
| 62 | Param.InputTable{1,5},Param.InputTable{1,4},i1,i2,j1,j2);
|
---|
| 63 | if ~exist(FirstFileName,'file')
|
---|
| 64 | msgbox_uvmat('WARNING',['the first input file ' FirstFileName ' does not exist'])
|
---|
| 65 | end
|
---|
| 66 | return
|
---|
| 67 | end
|
---|
| 68 |
|
---|
| 69 | %% INPUT PARAMETERS (to edit)
|
---|
| 70 | increment=Param.IndexRange.incr_i;% frame increment: the frequency of the png images will be (initial frequency)/increment.
|
---|
| 71 | colorweight=[1 1 1] % relative weight of color components [r g b] for the resulting B/W image
|
---|
| 72 |
|
---|
| 73 | %%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
|
---|
| 74 | ParamOut=[]; %default output
|
---|
| 75 | %% read input parameters from an xml file if input is a file name (batch mode)
|
---|
| 76 | checkrun=1;
|
---|
| 77 | if ischar(Param)
|
---|
| 78 | Param=xml2struct(Param);% read Param as input file (batch case)
|
---|
| 79 | checkrun=0;
|
---|
| 80 | end
|
---|
| 81 | hseries=findobj(allchild(0),'Tag','series');
|
---|
| 82 | RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
|
---|
| 83 | WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
|
---|
| 84 |
|
---|
| 85 | %% define the directory for result file (with path=RootPath{1})
|
---|
| 86 | OutputDir=[Param.OutputSubDir Param.OutputDirExt];% subdirectory for output files
|
---|
| 87 | if ~isfield(Param,'InputFields')
|
---|
| 88 | Param.InputFields.FieldName='';
|
---|
| 89 | end
|
---|
| 90 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%%
|
---|
| 91 | % EDIT FROM HERE
|
---|
| 92 |
|
---|
| 93 | %% root input file type
|
---|
| 94 | RootPath=Param.InputTable{1,1};
|
---|
| 95 | RootFile=Param.InputTable{1,3};
|
---|
| 96 | SubDir=Param.InputTable{1,2};
|
---|
| 97 | NomType=Param.InputTable{1,4};
|
---|
| 98 | FileExt=Param.InputTable{1,5};
|
---|
| 99 | first_j=[];
|
---|
| 100 | if isfield(Param.IndexRange,'first_j'); first_j=Param.IndexRange.first_j; end
|
---|
| 101 | [i1,i2,j1,j2] = get_file_index(Param.IndexRange.first_i,first_j,'');
|
---|
| 102 | InputFileName=fullfile_uvmat(RootPath,SubDir,RootFile,FileExt,NomType,i1,i2,j1,j2);
|
---|
| 103 | [FileInfo,VideoObject]=get_file_info(InputFileName);
|
---|
| 104 |
|
---|
| 105 | %% determine the file type on each line from the first input file
|
---|
| 106 | if isempty(find(strcmp(FileInfo.FileType,{'mmreader','video'})));% =1 for images
|
---|
| 107 | disp_uvmat('ERROR','the input is not a movie or image series',checkrun)
|
---|
| 108 | end
|
---|
| 109 | FileExtOut='.png'; %image output (input and proj result = image)
|
---|
| 110 | NomTypeOut='_1';% output file index will indicate the first and last ref index in the series
|
---|
| 111 | RootFileOut=RootFile;
|
---|
| 112 | NbField=floor((Param.IndexRange.last_i-Param.IndexRange.first_i+1)/increment);
|
---|
| 113 | %% create xml file with timing:
|
---|
| 114 | %info=aviinfo(aviname);
|
---|
| 115 | t=xmltree;
|
---|
| 116 | t=set(t,1,'name','ImaDoc');
|
---|
| 117 | [t,uid]=add(t,1,'element','Heading');
|
---|
| 118 | % A AJOUTER
|
---|
| 119 | % Heading.Project='';
|
---|
| 120 | Heading.ImageName='frame_1.png';
|
---|
| 121 | t=struct2xml(Heading,t,uid);
|
---|
| 122 | [t,uid]=add(t,1,'element','Camera');
|
---|
| 123 | Camera.TimeUnit='s';
|
---|
| 124 | % Camera.BurstTiming.FrameFrequency=info.FramesPerSecond/increment;
|
---|
| 125 | Camera.BurstTiming.Dti=1/FileInfo.FrameRate;
|
---|
| 126 | Camera.BurstTiming.NbDti=NbField*increment;
|
---|
| 127 | Camera.BurstTiming.Time=i1/FileInfo.FrameRate;%time of the first frame of the avi movie
|
---|
| 128 | t=struct2xml(Camera,t,uid);
|
---|
| 129 | save(t,[fileparts(InputFileName) '.xml'])
|
---|
| 130 |
|
---|
| 131 | %% LOOP ON FRAMES
|
---|
| 132 |
|
---|
| 133 | for index=Param.IndexRange.first_i:increment:Param.IndexRange.last_i
|
---|
| 134 | update_waitbar(WaitbarHandle,(index-Param.IndexRange.first_i)/NbField)
|
---|
| 135 | if ~isempty(RUNHandle) && ~strcmp(get(RUNHandle,'BusyAction'),'queue')
|
---|
| 136 | disp('program stopped by user')
|
---|
| 137 | return
|
---|
| 138 | end
|
---|
| 139 | A=read_image(InputFileName,FileInfo.FileType,VideoObject,index);
|
---|
| 140 | if ndims(A)==3% convert color image to B/W
|
---|
| 141 | A=double(A);
|
---|
| 142 | A=colorweight(1)*A(:,:,1)+colorweight(2)*A(:,:,2)+colorweight(3)*A(:,:,3);
|
---|
| 143 | A=uint16(A);% transform to 16 bit integers
|
---|
| 144 | end
|
---|
| 145 | %new_index=1+floor((index-Param.IndexRange.first_i)/increment);
|
---|
| 146 | OutputFileName=fullfile_uvmat(RootPath,OutputDir,RootFile,FileExtOut,NomTypeOut,index);
|
---|
| 147 | %filename=[basename '_' num2str(new_index) '.png'];%create image name
|
---|
| 148 | imwrite(A,OutputFileName,'BitDepth',16);%write image
|
---|
| 149 | disp(['new frame ' num2str(index) ' written as png image'])
|
---|
| 150 | end
|
---|
| 151 |
|
---|
| 152 | %% main loop on frames
|
---|
| 153 | % for ifile=1:nbfield
|
---|
| 154 | % stopstate=get(hRUN,'BusyAction');
|
---|
| 155 | % if isequal(stopstate,'queue')% if STOP command is not activated
|
---|
| 156 | % waitbarpos(4)=(ifile/nbfield)*Series.WaitbarPos(4);
|
---|
| 157 | % waitbarpos(2)=Series.WaitbarPos(4)+Series.WaitbarPos(2)-waitbarpos(4);
|
---|
| 158 | % set(hwaitbar,'Position',waitbarpos)%update waitbar on the series interface
|
---|
| 159 | % drawnow
|
---|
| 160 | % A=read_image(aviname,'movie',num_i1(ifile),MovieObject);
|
---|
| 161 | % if ndims(A)==3% convert color image to B/W
|
---|
| 162 | % A=double(A);
|
---|
| 163 | % A=colorweight(1)*A(:,:,1)+colorweight(2)*A(:,:,2)+colorweight(3)*A(:,:,3);
|
---|
| 164 | % A=uint8(A);% transform to 8 bit integers
|
---|
| 165 | % end
|
---|
| 166 | % new_index=1+floor((num_i1(ifile)-num_i1(1))/increment);
|
---|
| 167 | % filename=[basename '_' num2str(new_index) '.png'];%create image name
|
---|
| 168 | % imwrite(A,filename,'BitDepth',8);%write image
|
---|
| 169 | % display(['new frame ' num2str(new_index) ' written as png image'])
|
---|
| 170 | % end
|
---|
| 171 | % end
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 |
|
---|