source: trunk/src/read_rdvision.m @ 794

Last change on this file since 794 was 794, checked in by sommeria, 10 years ago

read_rdvision corrected

File size: 8.0 KB
Line 
1function [A,FileInfo,timestamps,errormsg]=read_rdvision(filename,frame_idx)
2% BINREAD_RDV Permet de lire les fichiers bin générés par Hiris à partir du
3% fichier seq associé.
4%   [IMGS,TIMESTAMPS,NB_FRAMES] = BINREAD_RDV(FILENAME,FRAME_IDX) lit
5%   l'image d'indice FRAME_IDX de la séquence FILENAME.
6%
7%   Entrées
8%   -------
9%   FILENAME  : Nom du fichier séquence (.seq).
10%   FRAME_IDX : Indice de l'image à lire. Si FRAME_IDX vaut -1 alors la
11%   séquence est entièrement lue. Si FRAME_IDX est un tableau d'indices
12%   alors toutes les images d'incides correspondant sont lues. Si FRAME_IDX
13%   est un tableau vide alors aucune image n'est lue mais le nombre
14%   d'images et tous les timestamps sont renvoyés. Les indices commencent à
15%   1 et se termines à NB_FRAMES.
16%
17%   Sorties
18%   -------
19%   IMGS        : Images de sortie.
20%   TIMESTAMPS  : Timestaps des images lues.
21%   NB_FRAMES   : Nombres d'images dans la séquence.
22
23errormsg='';
24if nargin<2% no frame indices specified
25   frame_idx=-1;% all the images in the series are read
26end
27A=[];
28timestamps=[];
29[PathDir,RootFile,Ext]=fileparts(filename);
30RootPath=fileparts(PathDir);
31switch Ext
32    case '.seq'
33        filename_seq=filename;
34        filename_sqb=fullfile(PathDir,[RootFile '.sqb']);
35    case '.sqb'
36        filename_seq=fullfile(PathDir,[RootFile '.seq']);
37        filename_sqb=filename;
38    otherwise
39        errormsg='input file extension must be .seq or .sqb';
40end
41if ~exist(filename_seq,'file')
42    errormsg=[filename_seq ' does not exist'];
43    return
44end
45s=ini2struct(filename_seq);
46FileInfo=s.sequenceSettings;
47if isfield(s.sequenceSettings,'numberoffiles')
48    FileInfo.NumberOfFrames=str2double(s.sequenceSettings.numberoffiles);
49    FileInfo.FrameRate=str2double(s.sequenceSettings.framepersecond);
50    FileInfo.ColorType='grayscale';
51else
52    FileInfo.FileType='';
53    return
54end
55FileInfo.FileType='rdvision'; % file used to store info from image acquisition systems of rdvision
56nbfield=numel(fieldnames(FileInfo));
57FileInfo=orderfields(FileInfo,[nbfield nbfield-1 nbfield-2 (1:nbfield-3)]); %reorder the fields of fileInfo for clarity
58
59% read the images the input frame_idxis not empty
60if ~isempty(frame_idx)
61    w=str2double(FileInfo.width);
62    h=str2double(FileInfo.height);
63    bpp=str2double(FileInfo.bytesperpixel);
64    bin_file=FileInfo.binfile;
65    nb_frames=str2double(FileInfo.numberoffiles);
66    m = memmapfile(filename_sqb,'Format', { 'uint32' [1 1] 'offset'; ...
67        'uint32' [1 1] 'garbage1';...
68        'double' [1 1] 'timestamp';...
69        'uint32' [1 1] 'file_idx';...
70        'uint32' [1 1] 'garbage2' },'Repeat',nb_frames);
71   
72    data=m.Data;
73    timestamps=[data.timestamp];
74   
75    if frame_idx==-1
76        frame_idx=1:nb_frames;
77    end
78   
79    classname=sprintf('uint%d',bpp*8);
80    A=zeros([h,w,length(frame_idx)],classname);
81   
82    classname=['*' classname];
83   
84    for i=1:length(frame_idx)
85        ii=frame_idx(i);
86        if ~isempty(FileInfo.binrepertoire)
87            binrepertoire=FileInfo.binrepertoire;
88        else %used when binrepertoire empty, strange feature of rdvision
89            binrepertoire=regexprep(FileInfo.bindirectory,'\\$','');%tranform Windows notation to Linux
90            binrepertoire=regexprep(binrepertoire,'\','/');
91            [tild,binrepertoire,DirExt]=fileparts(binrepertoire);
92            binrepertoire=[binrepertoire DirExt];
93        end
94        binfile=fullfile(RootPath,binrepertoire,sprintf('%s%.5d.bin',bin_file,data(ii).file_idx));
95        fid=fopen(binfile,'rb');
96        fseek(fid,data(ii).offset,-1);
97        A(:,:,i)=reshape(fread(fid,w*h,classname),w,h)';
98        fclose(fid);
99    end
100   
101    if ~isempty(frame_idx)
102        timestamps=timestamps(frame_idx);
103    end
104end
105
106function Result = ini2struct(FileName)
107%==========================================================================
108%  Author: Andriy Nych ( nych.andriy@gmail.com )
109% Version:        733341.4155741782200
110%==========================================================================
111%
112% INI = ini2struct(FileName)
113%
114% This function parses INI file FileName and returns it as a structure with
115% section names and keys as fields.
116%
117% Sections from INI file are returned as fields of INI structure.
118% Each fiels (section of INI file) in turn is structure.
119% It's fields are variables from the corresponding section of the INI file.
120%
121% If INI file contains "oprhan" variables at the beginning, they will be
122% added as fields to INI structure.
123%
124% Lines starting with ';' and '#' are ignored (comments).
125%
126% See example below for more information.
127%
128% Usually, INI files allow to put spaces and numbers in section names
129% without restrictions as long as section name is between '[' and ']'.
130% It makes people crazy to convert them to valid Matlab variables.
131% For this purpose Matlab provides GENVARNAME function, which does
132%  "Construct a valid MATLAB variable name from a given candidate".
133% See 'help genvarname' for more information.
134%
135% The INI2STRUCT function uses the GENVARNAME to convert strange INI
136% file string into valid Matlab field names.
137%
138% [ test.ini ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139%
140%     SectionlessVar1=Oops
141%     SectionlessVar2=I did it again ;o)
142%     [Application]
143%     Title = Cool program
144%     LastDir = c:\Far\Far\Away
145%     NumberOFSections = 2
146%     [1st section]
147%     param1 = val1
148%     Param 2 = Val 2
149%     [Section #2]
150%     param1 = val1
151%     Param 2 = Val 2
152%
153% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154%
155% The function converts this INI file it to the following structure:
156%
157% [ MatLab session (R2006b) ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
158%  >> INI = ini2struct('test.ini');
159%  >> disp(INI)
160%         sectionlessvar1: 'Oops'
161%         sectionlessvar2: 'I did it again ;o)'
162%             application: [1x1 struct]
163%             x1stSection: [1x1 struct]
164%            section0x232: [1x1 struct]
165%
166%  >> disp(INI.application)
167%                    title: 'Cool program'
168%                  lastdir: 'c:\Far\Far\Away'
169%         numberofsections: '2'
170%
171%  >> disp(INI.x1stSection)
172%         param1: 'val1'
173%         param2: 'Val 2'
174%
175%  >> disp(INI.section0x232)
176%         param1: 'val1'
177%         param2: 'Val 2'
178%
179% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
180%
181% NOTE.
182% WhatToDoWithMyVeryCoolSectionAndVariableNamesInIniFileMyVeryCoolProgramWrites?
183% GENVARNAME also does the following:
184%   "Any string that exceeds NAMELENGTHMAX is truncated". (doc genvarname)
185% Period.
186%
187% =========================================================================
188Result = [];                            % we have to return something
189CurrMainField = '';                     % it will be used later
190f = fopen(FileName,'r');                % open file
191while ~feof(f)                          % and read until it ends
192    s = strtrim(fgetl(f));              % Remove any leading/trailing spaces
193    if isempty(s)
194        continue;
195    end;
196    if (s(1)==';')                      % ';' start comment lines
197        continue;
198    end;
199    if (s(1)=='#')                      % '#' start comment lines
200        continue;
201    end;
202    if ( s(1)=='[' ) && (s(end)==']' )
203        % We found section
204        CurrMainField = genvarname(lower(s(2:end-1)));
205        Result.(CurrMainField) = [];    % Create field in Result
206    else
207        % ??? This is not a section start
208        [par,val] = strtok(s, '=');
209        val = CleanValue(val);
210        if ~isempty(CurrMainField)
211            % But we found section before and have to fill it
212            Result.(CurrMainField).(lower(genvarname(par))) = val;
213        else
214            % No sections found before. Orphan value
215            Result.(lower(genvarname(par))) = val;
216        end
217    end
218end
219fclose(f);
220return;
221
222function res = CleanValue(s)
223res = strtrim(s);
224if strcmpi(res(1),'=')
225    res(1)=[];
226end
227res = strtrim(res);
228return;
Note: See TracBrowser for help on using the repository browser.