source: trunk/src/read_rdvision.m @ 1060

Last change on this file since 1060 was 1058, checked in by sommeria, 3 years ago

merge_proj_polar updated + other corrected bugs

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