source: trunk/src/read_rdvision.m @ 797

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

read_rdvision corrected

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