source: trunk/src/read_rdvision.m @ 790

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

read_rdvision corrected to get both cameras

File size: 7.5 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        binfile=fullfile(RootPath,FileInfo.binrepertoire,sprintf('%s%.5d.bin',bin_file,data(ii).file_idx));
87        fid=fopen(binfile,'rb');
88        fseek(fid,data(ii).offset,-1);
89        A(:,:,i)=reshape(fread(fid,w*h,classname),w,h)';
90        fclose(fid);
91    end
92   
93    if ~isempty(frame_idx)
94        timestamps=timestamps(frame_idx);
95    end
96end
97
98function Result = ini2struct(FileName)
99%==========================================================================
100%  Author: Andriy Nych ( nych.andriy@gmail.com )
101% Version:        733341.4155741782200
102%==========================================================================
103%
104% INI = ini2struct(FileName)
105%
106% This function parses INI file FileName and returns it as a structure with
107% section names and keys as fields.
108%
109% Sections from INI file are returned as fields of INI structure.
110% Each fiels (section of INI file) in turn is structure.
111% It's fields are variables from the corresponding section of the INI file.
112%
113% If INI file contains "oprhan" variables at the beginning, they will be
114% added as fields to INI structure.
115%
116% Lines starting with ';' and '#' are ignored (comments).
117%
118% See example below for more information.
119%
120% Usually, INI files allow to put spaces and numbers in section names
121% without restrictions as long as section name is between '[' and ']'.
122% It makes people crazy to convert them to valid Matlab variables.
123% For this purpose Matlab provides GENVARNAME function, which does
124%  "Construct a valid MATLAB variable name from a given candidate".
125% See 'help genvarname' for more information.
126%
127% The INI2STRUCT function uses the GENVARNAME to convert strange INI
128% file string into valid Matlab field names.
129%
130% [ test.ini ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131%
132%     SectionlessVar1=Oops
133%     SectionlessVar2=I did it again ;o)
134%     [Application]
135%     Title = Cool program
136%     LastDir = c:\Far\Far\Away
137%     NumberOFSections = 2
138%     [1st section]
139%     param1 = val1
140%     Param 2 = Val 2
141%     [Section #2]
142%     param1 = val1
143%     Param 2 = Val 2
144%
145% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
146%
147% The function converts this INI file it to the following structure:
148%
149% [ MatLab session (R2006b) ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
150%  >> INI = ini2struct('test.ini');
151%  >> disp(INI)
152%         sectionlessvar1: 'Oops'
153%         sectionlessvar2: 'I did it again ;o)'
154%             application: [1x1 struct]
155%             x1stSection: [1x1 struct]
156%            section0x232: [1x1 struct]
157%
158%  >> disp(INI.application)
159%                    title: 'Cool program'
160%                  lastdir: 'c:\Far\Far\Away'
161%         numberofsections: '2'
162%
163%  >> disp(INI.x1stSection)
164%         param1: 'val1'
165%         param2: 'Val 2'
166%
167%  >> disp(INI.section0x232)
168%         param1: 'val1'
169%         param2: 'Val 2'
170%
171% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
172%
173% NOTE.
174% WhatToDoWithMyVeryCoolSectionAndVariableNamesInIniFileMyVeryCoolProgramWrites?
175% GENVARNAME also does the following:
176%   "Any string that exceeds NAMELENGTHMAX is truncated". (doc genvarname)
177% Period.
178%
179% =========================================================================
180Result = [];                            % we have to return something
181CurrMainField = '';                     % it will be used later
182f = fopen(FileName,'r');                % open file
183while ~feof(f)                          % and read until it ends
184    s = strtrim(fgetl(f));              % Remove any leading/trailing spaces
185    if isempty(s)
186        continue;
187    end;
188    if (s(1)==';')                      % ';' start comment lines
189        continue;
190    end;
191    if (s(1)=='#')                      % '#' start comment lines
192        continue;
193    end;
194    if ( s(1)=='[' ) && (s(end)==']' )
195        % We found section
196        CurrMainField = genvarname(lower(s(2:end-1)));
197        Result.(CurrMainField) = [];    % Create field in Result
198    else
199        % ??? This is not a section start
200        [par,val] = strtok(s, '=');
201        val = CleanValue(val);
202        if ~isempty(CurrMainField)
203            % But we found section before and have to fill it
204            Result.(CurrMainField).(lower(genvarname(par))) = val;
205        else
206            % No sections found before. Orphan value
207            Result.(lower(genvarname(par))) = val;
208        end
209    end
210end
211fclose(f);
212return;
213
214function res = CleanValue(s)
215res = strtrim(s);
216if strcmpi(res(1),'=')
217    res(1)=[];
218end
219res = strtrim(res);
220return;
Note: See TracBrowser for help on using the repository browser.