source: trunk/src/read_rdvision.m @ 796

Last change on this file since 796 was 796, checked in by sommeria, 10 years ago
File size: 8.4 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   % bin_file='Dalsa1_';
66    nb_frames=str2double(FileInfo.numberoffiles);
67 %%%%%%BRICOLAGE
68    m = memmapfile(filename_sqb,'Format', { 'uint32' [1 1] 'offset'; ...
69        'uint32' [1 1] 'garbage1';...
70        'double' [1 1] 'timestamp';...
71        'uint32' [1 1] 'file_idx';...
72        'uint32' [1 1] 'garbage2' },'Repeat',nb_frames);
73   
74    data=m.Data;
75    %%%%%%%BRICOLAGE
76%     ind=[60 63:152];
77%     for ii=1:32*numel(ind)
78%         data(ii).offset=mod(ii-1,32)*4194304+2097152;
79%         data(ii).file_idx=ind(ceil(ii/32));
80%         data(ii).timestamp=[0:0.2:32*numel(ind)-1];
81%     end
82    timestamps=[data.timestamp];
83    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84    if frame_idx==-1
85        frame_idx=1:nb_frames;
86    end
87   
88    classname=sprintf('uint%d',bpp*8);
89    A=zeros([h,w,length(frame_idx)],classname);
90   
91    classname=['*' classname];
92   
93    for i=1:length(frame_idx)
94        ii=frame_idx(i);
95        if ~isempty(FileInfo.binrepertoire)
96            binrepertoire=FileInfo.binrepertoire;
97        else %used when binrepertoire empty, strange feature of rdvision
98            binrepertoire=regexprep(FileInfo.bindirectory,'\\$','');%tranform Windows notation to Linux
99            binrepertoire=regexprep(binrepertoire,'\','/');
100            [tild,binrepertoire,DirExt]=fileparts(binrepertoire);
101            binrepertoire=[binrepertoire DirExt];
102        end
103      %  binrepertoire='2014-07-04T10.48.46'% case FJORD5a %%%%%%%%%%%%%%%%%%%%%%%%%
104        binfile=fullfile(RootPath,binrepertoire,sprintf('%s%.5d.bin',bin_file,data(ii).file_idx));
105        fid=fopen(binfile,'rb');
106        fseek(fid,data(ii).offset,-1);
107        A(:,:,i)=reshape(fread(fid,w*h,classname),w,h)';
108        fclose(fid);
109    end
110   
111    if ~isempty(frame_idx)
112        timestamps=timestamps(frame_idx);
113    end
114end
115
116function Result = ini2struct(FileName)
117%==========================================================================
118%  Author: Andriy Nych ( nych.andriy@gmail.com )
119% Version:        733341.4155741782200
120%==========================================================================
121%
122% INI = ini2struct(FileName)
123%
124% This function parses INI file FileName and returns it as a structure with
125% section names and keys as fields.
126%
127% Sections from INI file are returned as fields of INI structure.
128% Each fiels (section of INI file) in turn is structure.
129% It's fields are variables from the corresponding section of the INI file.
130%
131% If INI file contains "oprhan" variables at the beginning, they will be
132% added as fields to INI structure.
133%
134% Lines starting with ';' and '#' are ignored (comments).
135%
136% See example below for more information.
137%
138% Usually, INI files allow to put spaces and numbers in section names
139% without restrictions as long as section name is between '[' and ']'.
140% It makes people crazy to convert them to valid Matlab variables.
141% For this purpose Matlab provides GENVARNAME function, which does
142%  "Construct a valid MATLAB variable name from a given candidate".
143% See 'help genvarname' for more information.
144%
145% The INI2STRUCT function uses the GENVARNAME to convert strange INI
146% file string into valid Matlab field names.
147%
148% [ test.ini ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
149%
150%     SectionlessVar1=Oops
151%     SectionlessVar2=I did it again ;o)
152%     [Application]
153%     Title = Cool program
154%     LastDir = c:\Far\Far\Away
155%     NumberOFSections = 2
156%     [1st section]
157%     param1 = val1
158%     Param 2 = Val 2
159%     [Section #2]
160%     param1 = val1
161%     Param 2 = Val 2
162%
163% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
164%
165% The function converts this INI file it to the following structure:
166%
167% [ MatLab session (R2006b) ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
168%  >> INI = ini2struct('test.ini');
169%  >> disp(INI)
170%         sectionlessvar1: 'Oops'
171%         sectionlessvar2: 'I did it again ;o)'
172%             application: [1x1 struct]
173%             x1stSection: [1x1 struct]
174%            section0x232: [1x1 struct]
175%
176%  >> disp(INI.application)
177%                    title: 'Cool program'
178%                  lastdir: 'c:\Far\Far\Away'
179%         numberofsections: '2'
180%
181%  >> disp(INI.x1stSection)
182%         param1: 'val1'
183%         param2: 'Val 2'
184%
185%  >> disp(INI.section0x232)
186%         param1: 'val1'
187%         param2: 'Val 2'
188%
189% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190%
191% NOTE.
192% WhatToDoWithMyVeryCoolSectionAndVariableNamesInIniFileMyVeryCoolProgramWrites?
193% GENVARNAME also does the following:
194%   "Any string that exceeds NAMELENGTHMAX is truncated". (doc genvarname)
195% Period.
196%
197% =========================================================================
198Result = [];                            % we have to return something
199CurrMainField = '';                     % it will be used later
200f = fopen(FileName,'r');                % open file
201while ~feof(f)                          % and read until it ends
202    s = strtrim(fgetl(f));              % Remove any leading/trailing spaces
203    if isempty(s)
204        continue;
205    end;
206    if (s(1)==';')                      % ';' start comment lines
207        continue;
208    end;
209    if (s(1)=='#')                      % '#' start comment lines
210        continue;
211    end;
212    if ( s(1)=='[' ) && (s(end)==']' )
213        % We found section
214        CurrMainField = genvarname(lower(s(2:end-1)));
215        Result.(CurrMainField) = [];    % Create field in Result
216    else
217        % ??? This is not a section start
218        [par,val] = strtok(s, '=');
219        val = CleanValue(val);
220        if ~isempty(CurrMainField)
221            % But we found section before and have to fill it
222            Result.(CurrMainField).(lower(genvarname(par))) = val;
223        else
224            % No sections found before. Orphan value
225            Result.(lower(genvarname(par))) = val;
226        end
227    end
228end
229fclose(f);
230return;
231
232function res = CleanValue(s)
233res = strtrim(s);
234if strcmpi(res(1),'=')
235    res(1)=[];
236end
237res = strtrim(res);
238return;
Note: See TracBrowser for help on using the repository browser.