source: trunk/src/read_rdvision.m @ 913

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