source: trunk/src/read_rdvision.m @ 1180

Last change on this file since 1180 was 1178, checked in by sommeria, 4 weeks ago

virtual frame indexing introduced

File size: 10.4 KB
RevLine 
[809]1%=======================================================================
[1126]2% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[809]3%   http://www.legi.grenoble-inp.fr
[1127]4%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
[809]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
[790]19function [A,FileInfo,timestamps,errormsg]=read_rdvision(filename,frame_idx)
[991]20% BINREAD_RDV Permet de lire les fichiers bin g???n???r???s par Hiris ??? partir du
21% fichier seq associ???.
[785]22%   [IMGS,TIMESTAMPS,NB_FRAMES] = BINREAD_RDV(FILENAME,FRAME_IDX) lit
[991]23%   l'image d'indice FRAME_IDX de la s???quence FILENAME.
[785]24%
[991]25%   Entr???es
[785]26%   -------
[991]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
[785]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
[991]32%   d'images et tous les timestamps sont renvoy???s. Les indices commencent ???
33%   1 et se termines ??? NB_FRAMES.
[785]34%
35%   Sorties
36%   -------
37%   IMGS        : Images de sortie.
38%   TIMESTAMPS  : Timestaps des images lues.
[1106]39%   NB_FRAMES   : Nombres d'images dans la sequence.
[785]40
[790]41errormsg='';
[785]42if nargin<2% no frame indices specified
43   frame_idx=-1;% all the images in the series are read
44end
45A=[];
46timestamps=[];
[790]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);
[785]64FileInfo=s.sequenceSettings;
[1178]65FileInfo.Width=str2double(FileInfo.width);
66FileInfo.Height=str2double(FileInfo.height);
67if isequal(FileInfo.bytesperpixel,'2')
68    FileInfo.BitDepth=16;
69else
70    FileInfo.BitDepth=8;
71end
[790]72if isfield(s.sequenceSettings,'numberoffiles')
73    FileInfo.NumberOfFrames=str2double(s.sequenceSettings.numberoffiles);
74    FileInfo.FrameRate=str2double(s.sequenceSettings.framepersecond);
75    FileInfo.ColorType='grayscale';
[1032]76    FileInfo.TimeName='timestamp';
[790]77else
78    FileInfo.FileType='';
79    return
80end
81FileInfo.FileType='rdvision'; % file used to store info from image acquisition systems of rdvision
82nbfield=numel(fieldnames(FileInfo));
83FileInfo=orderfields(FileInfo,[nbfield nbfield-1 nbfield-2 (1:nbfield-3)]); %reorder the fields of fileInfo for clarity
[785]84
85% read the images the input frame_idxis not empty
86if ~isempty(frame_idx)
87    w=str2double(FileInfo.width);
88    h=str2double(FileInfo.height);
89    bpp=str2double(FileInfo.bytesperpixel);
90    bin_file=FileInfo.binfile;
[796]91   % bin_file='Dalsa1_';
[785]92    nb_frames=str2double(FileInfo.numberoffiles);
[797]93    %%%% reading the .sqb file
[790]94    m = memmapfile(filename_sqb,'Format', { 'uint32' [1 1] 'offset'; ...
[785]95        'uint32' [1 1] 'garbage1';...
96        'double' [1 1] 'timestamp';...
97        'uint32' [1 1] 'file_idx';...
[797]98        'uint32' [1 1] 'garbage2' },'Repeat',nb_frames);   
[785]99    data=m.Data;
[797]100       %%%%%%%BRICOLAGE in case of unreadable .sqb file
[896]101%      ind=[144 149:646];%indices of bin files
102%      lengthimage=w*h*bpp;% lengthof an image record on the binary file
[796]103%     for ii=1:32*numel(ind)
[797]104%         data(ii).offset=mod(ii-1,32)*2*lengthimage+lengthimage;%Dalsa_2
105%         %data(ii).offset=mod(ii-1,32)*2*lengthimage;%Dalsa_1
[796]106%         data(ii).file_idx=ind(ceil(ii/32));
[797]107%         data(ii).timestamp=0.2*(ii-1);
[796]108%     end
[797]109    %%%%%%%
[785]110    timestamps=[data.timestamp];
[796]111    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[785]112    if frame_idx==-1
113        frame_idx=1:nb_frames;
114    end
115   
116    classname=sprintf('uint%d',bpp*8);
117    A=zeros([h,w,length(frame_idx)],classname);
118   
119    classname=['*' classname];
120   
121    for i=1:length(frame_idx)
122        ii=frame_idx(i);
[794]123        if ~isempty(FileInfo.binrepertoire)
124            binrepertoire=FileInfo.binrepertoire;
125        else %used when binrepertoire empty, strange feature of rdvision
126            binrepertoire=regexprep(FileInfo.bindirectory,'\\$','');%tranform Windows notation to Linux
127            binrepertoire=regexprep(binrepertoire,'\','/');
[1178]128            [~,binrepertoire,DirExt]=fileparts(binrepertoire);
[991]129            binrepertoire=[binrepertoire DirExt];     
[794]130        end
[796]131      %  binrepertoire='2014-07-04T10.48.46'% case FJORD5a %%%%%%%%%%%%%%%%%%%%%%%%%
[1047]132      % binrepertoire='2017-01-19T22.12.182'% EXP14 %%%%%%%%%%%%%%%%%%%%%%%%%
[794]133        binfile=fullfile(RootPath,binrepertoire,sprintf('%s%.5d.bin',bin_file,data(ii).file_idx));
[1058]134        if ~exist(binfile,'file')
135            errormsg=[binfile ' does not exist'];
[1106]136            FileInfo.StartTime=regexprep(FileInfo.binrepertoire,'T',' ');
137            FileInfo.EndTime=datestr(datenum(FileInfo.StartTime,'yyyy-mm-dd HH.MM.SS')+timestamps(end)/86400);
138            disp(FileInfo)
[1058]139            return
140        else
[785]141        fid=fopen(binfile,'rb');
142        fseek(fid,data(ii).offset,-1);
143        A(:,:,i)=reshape(fread(fid,w*h,classname),w,h)';
144        fclose(fid);
[1058]145        end
[785]146    end
147   
148    if ~isempty(frame_idx)
149        timestamps=timestamps(frame_idx);
[1106]150    end 
151    FileInfo.StartTime=regexprep(FileInfo.binrepertoire,'T',' ');
152    FileInfo.EndTime=datestr(datenum(FileInfo.StartTime,'yyyy-mm-dd HH.MM.SS')+timestamps(end)/86400);
[785]153end
154
[1106]155
156
[785]157function Result = ini2struct(FileName)
158%==========================================================================
159%  Author: Andriy Nych ( nych.andriy@gmail.com )
160% Version:        733341.4155741782200
161%==========================================================================
162%
163% INI = ini2struct(FileName)
164%
165% This function parses INI file FileName and returns it as a structure with
166% section names and keys as fields.
167%
168% Sections from INI file are returned as fields of INI structure.
169% Each fiels (section of INI file) in turn is structure.
170% It's fields are variables from the corresponding section of the INI file.
171%
172% If INI file contains "oprhan" variables at the beginning, they will be
173% added as fields to INI structure.
174%
175% Lines starting with ';' and '#' are ignored (comments).
176%
177% See example below for more information.
178%
179% Usually, INI files allow to put spaces and numbers in section names
180% without restrictions as long as section name is between '[' and ']'.
181% It makes people crazy to convert them to valid Matlab variables.
182% For this purpose Matlab provides GENVARNAME function, which does
183%  "Construct a valid MATLAB variable name from a given candidate".
184% See 'help genvarname' for more information.
185%
186% The INI2STRUCT function uses the GENVARNAME to convert strange INI
187% file string into valid Matlab field names.
188%
189% [ test.ini ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190%
191%     SectionlessVar1=Oops
192%     SectionlessVar2=I did it again ;o)
193%     [Application]
194%     Title = Cool program
195%     LastDir = c:\Far\Far\Away
196%     NumberOFSections = 2
197%     [1st section]
198%     param1 = val1
199%     Param 2 = Val 2
200%     [Section #2]
201%     param1 = val1
202%     Param 2 = Val 2
203%
204% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205%
206% The function converts this INI file it to the following structure:
207%
208% [ MatLab session (R2006b) ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
209%  >> INI = ini2struct('test.ini');
210%  >> disp(INI)
211%         sectionlessvar1: 'Oops'
212%         sectionlessvar2: 'I did it again ;o)'
213%             application: [1x1 struct]
214%             x1stSection: [1x1 struct]
215%            section0x232: [1x1 struct]
216%
217%  >> disp(INI.application)
218%                    title: 'Cool program'
219%                  lastdir: 'c:\Far\Far\Away'
220%         numberofsections: '2'
221%
222%  >> disp(INI.x1stSection)
223%         param1: 'val1'
224%         param2: 'Val 2'
225%
226%  >> disp(INI.section0x232)
227%         param1: 'val1'
228%         param2: 'Val 2'
229%
230% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
231%
232% NOTE.
233% WhatToDoWithMyVeryCoolSectionAndVariableNamesInIniFileMyVeryCoolProgramWrites?
234% GENVARNAME also does the following:
235%   "Any string that exceeds NAMELENGTHMAX is truncated". (doc genvarname)
236% Period.
237%
238% =========================================================================
239Result = [];                            % we have to return something
240CurrMainField = '';                     % it will be used later
241f = fopen(FileName,'r');                % open file
242while ~feof(f)                          % and read until it ends
243    s = strtrim(fgetl(f));              % Remove any leading/trailing spaces
244    if isempty(s)
245        continue;
[1178]246    end
[785]247    if (s(1)==';')                      % ';' start comment lines
248        continue;
[1178]249    end
[785]250    if (s(1)=='#')                      % '#' start comment lines
251        continue;
[1178]252    end
[785]253    if ( s(1)=='[' ) && (s(end)==']' )
254        % We found section
255        CurrMainField = genvarname(lower(s(2:end-1)));
256        Result.(CurrMainField) = [];    % Create field in Result
257    else
258        % ??? This is not a section start
259        [par,val] = strtok(s, '=');
260        val = CleanValue(val);
261        if ~isempty(CurrMainField)
262            % But we found section before and have to fill it
263            Result.(CurrMainField).(lower(genvarname(par))) = val;
264        else
265            % No sections found before. Orphan value
266            Result.(lower(genvarname(par))) = val;
267        end
268    end
269end
270fclose(f);
271return;
272
273function res = CleanValue(s)
274res = strtrim(s);
275if strcmpi(res(1),'=')
276    res(1)=[];
277end
278res = strtrim(res);
279return;
Note: See TracBrowser for help on using the repository browser.