how can i open .out extension file in matlab ?

I'm trying to open a bundle.out file in matlab. Does anyone has any idea of how to do so ?

6 Comments

*fopen*, maybe???
What is the content of the file? One presumes it probably is simply a text file, but how are we to know unless you give us some clues...
ba
ba on 4 Aug 2014
Edited: ba on 4 Aug 2014
I'm currently working with bundler for bundle adjustment. I have the bundle.out file and points001.ply files as output. I need to open the bundle.out file in matlab and locate the position of the cameras and points in 3D. The format of .out file is :
269 31913
3.0107383211e+003 1.2457159171e-001 -3.2061110722e-001
6.3846423609e-001 -2.0702958325e-002 7.6937299585e-001
-3.4926013391e-001 8.8299277113e-001 3.1359388546e-001
-6.8584311478e-001 -4.6892979608e-001 5.5652858709e-001
8.8146166784e+001 1.0107379995e+001 2.3539905552e+001
4.7501507199e-001 2.3681082673e+001 -9.9221710766e+001
43 78 72
4 80 3252 1270.5100 90.1400 81 3258 1233.3401 64.4800 76 3183 1233.0701
85.0601 65 1896
which follows the following convention :
num_cameras num_points [two integers]
f k1 k2 [the focal length, followed by two radial distortion coeffs]
R [a 3x3 matrix representing the camera rotation]
t [a 3-vector describing the camera translation]
position [a 3-vector describing the 3D position of the point]
color [a 3-vector describing the RGB color of the point]
list [a list of views the point is visible in]
I need to produce a 3D reconstruction of cameras and a rough point-based scene geometry as output in matlab.
The links I tried return
The page you were looking for doesn't exist.
These are not links in blue color.
dpb
dpb on 9 Aug 2014
Edited: dpb on 9 Aug 2014
Looks like a link, acts like a link, is a link...excepting no target given. Don't do that in future, please... most confusing at first, then irritating when discover what poster has done.
You can use bold or italic (or both) to emphasize and/or identify items--I tend to use bold for Matlab functions and italics for variables in my postings but that's just my personal convention, no Forum protocol specifically.
Okay! Will take care of this from next time.

Sign in to comment.

 Accepted Answer

line no. 4 to 6 is a 3x3 matrix. how can i read it?
Create a dummy file w/ a 3x3 array...
>> dlmwrite('ba.txt',rand(3)*10,'precision','%12.8e','delimiter',' ')
>> type ba.txt
5.67821641e+00 5.30797553e+00 1.29906208e+00
7.58542896e-01 7.79167230e+00 5.68823661e+00
5.39501187e-01 9.34010684e+00 4.69390641e+00
Read it back...
>> fid=fopen('ba.txt');
>> x=fscanf(fid,'%f',[3,3]).'
x =
5.6782 5.3080 1.2991
0.7585 7.7917 5.6882
0.5395 9.3401 4.6939
>> fid=fclose(fid);
>>
NB: the .' element transpose to order by row since Matlab fills in column order on reading the nine values. You can, of course, also simply read 9 values and reshape and transpose...
As outlined in initial answer, use a combination of textscan and/or fscanf and friends that operate on an open file handle with the rosetta stone of how the file is structured to return the various pieces in sequence using things like your magic number of 5*NumCameras and knowing it's a 3x3 array where appropriate.
It's not really terribly difficult, but is as (I believe it was IA?) mentioned, drudgery initially to work through and get going. Once done, however, if done properly, you should be able to parse any file of the given structure easily.

More Answers (1)

dpb
dpb on 4 Aug 2014
Edited: dpb on 9 Aug 2014
num_cameras num_points [two integers]
f k1 k2 [the focal length, followed by two radial distortion coeffs]
R [a 3x3 matrix representing the camera rotation]
t [a 3-vector describing the camera translation]
position [a 3-vector describing the 3D position of the point]
Well, reading in the camera and position points doesn't appear too difficult; but there are some uncertainties left --
1) I presume perhaps there are num_cameras datasets in the file, maybe?
2) How does one determine the length of this list array?
fid=fopen('yourfile');
n=fscanf(fid,'%d %d'); % the two number values--camera, points-->[n(1) n(2)]
t=cell2mat(textscan(fid,'%f %f %f','headerlines',2)); % camera position???
p=cell2mat(textscan(fid,'%f %f %f')); % point position
Now the question is as above, what need to get to the next group. In general one builds a small snippet for each file section like the above and then place that in a loop for either a counted number of times or while(~feof(fid)) if length is unknown.

13 Comments

Actually I want to parse this complete file and locate the position of cameras and points in 3D.
function header = readOutHeader(~,F)
token = fscanf(F,'%s',[1 1]);
if strcmpi(token,'OUT')
header.type = 'OUT';
else
WARNING('Unsupported filetype');
header = NaN;
end
end
function cameraHeader = readOutCameraHeader(~,F)
cameraHeader.ncams = fscanf(F,'%d', [1 1]);
end
function cameras = readOutCameras(~,F,header)
cameras = cell(header.ncams,1);
for i = 1:header.ncams
camera.focal_length = fscanf(F,'%f', [1 1]);
camera.radial_distortion1 = fscanf(F,'%f', [1 1]);
camera.radial_distortion2 = fscanf(F,'%f', [1 1]);
camera.rotation = fscanf(F,'%f\n', [1 9]);
camera.translation = fscanf(F,'%f\n', [1 3]);
fscanf(F,'%f', [1 1]);
cameras{i} = camera;
end
end
function [features,points ] = readOutPoints(~,F,descriptors)
positions = fscanf(F,'%d', [1 3]);
color = fscanf(F,'%d\n', [1 3]);
for i = 1:positions
points(i,:) = fscanf(F,'%f',[1 3]);
[~] = fscanf(F,'%d',[1 3]);
nproj = fscanf(F,'%d',[1 1]);
for j = 1:nproj
camera_idx = fscanf(F,'%d',[1 1]) + 1;
feature_idx = fscanf(F,'%d',[1 1]) + 1;
c_features = descriptors{camera_idx};
c_feature = single(c_features(feature_idx,:));
features(i,:) = features(i,:) + c_feature;
[~] = fscanf(F, '%f',[1 2]);
end
features(i,:) = features(i,:) / nproj;
end
end
function model = readOutModel(obj,filename,datadir)
model = {};
if exist(filename,'file')
F = fopen(filename,'r');
header = obj.readOutHeader(F);
end
end
function model = loadOutModel(obj,filename,datadir)
model ={};
if exist(filename,'file')
F = fopen(filename,'r');
header = obj.readOutHeader(F);
c = 0;
while(true)
c = c+1;
cameraHeader = obj.readOutCameraHeader(F);
if cameraHeader.ncams>0
cameras = obj.readOutCameras(F,cameraHeader);
descriptors = obj.readOutDescriptors(datadir,cameras, cameraHeader);
[features,points ] = obj.readOutPoints(F,descriptors);
tmpmodel = Model();
tmpmodel.setCameras(cameras);
tmpmodel.setFeatures(features);
tmpmodel.setPoints(points);
model{numel(model)+1} = tmpmodel;
else
break;
end
end
else
WARNING(['Invalid model file name : ' filename]);
end
end
function saveMatModel(~,model,filename) %#ok<INUSL>
save(filename,'model');
end
function model=loadMatModel(~,filename)
load(filename);
end
end
This is what I have written. Can you please go through it and let me know where is the error ?
What error(s) did you get?
I got this : Illegal use of reserved keyword "while"
while(true)
No space between the while and the paren's...check the doc's for proper syntax.
I'd write
while true
as the parens are totally superfluous here.
Oh..Okay! Thanks!
Still not acceptable. Very very bad, dangerous code. There's no failsafe! Possibility of getting stuck in infinite loop. You need so pick some number that you know will never be exceeded and check for. Like you know for a fact that if c ever hits 10 or 100 or 1000, something went very wrong.
while c < 10 % Failsafe : quit if c ever gets as high as 10.
c = c + 1
% more code
end
if c >= 10
uiwait(warndlg('Error: did not find whatever.....'));
end
Maybe you think you'll never need it, but you should write code to be robust enough to handle unexpected situations.
Actually, as I had mentioned before, this is the
while ~feof(fid)
loop. Hadn't look at the actual code enough to really tell previously; just fixed the syntax problem. That'll die, eventually, if nothing else does first on a bum file.
I agree the "do forever" loop is generally a bad idea unless that's what's really intended.
ba
ba on 8 Aug 2014
Edited: ba on 8 Aug 2014
I agree the code is very bad..it didn'T work also. But if you look at the complete file..I couldn'T find any other way except using while loop because the format followed is different in different files as the no. of cameras are different.
so the first format starts from 3rd line until the line no. is 5 times the no. of cameras. and after that the other format continues.
So, can you help me on how to parse such a file ?
Not sure at this point what you're referring to as "first format" and "the other format" but you've given a clue that wasn't there before in the phrase until the line no. is 5 times the no. of cameras. It's that fixed repetition that's the key--once you read the number of cameras, then you have the piece of information to know either how many iterations of a given format or how large an array it is to be read.
ya..and if you see the attached zipped file..line no. 4 to 6 is a 3x3 matrix. how can i read it ?
Can anyone tell me how to project all the points and camera poses in 3D in matlab after reading the bundle.out file?
Don't know what, precisely, project all the points and camera poses in 3D means, but look at
help graph3d
for the list of 3D plotting routines and see if anything there seems to fit the bill.
I've never used any of it, but the camera control sections may be what you're looking for?

Sign in to comment.

Categories

Tags

Asked:

ba
on 4 Aug 2014

Edited:

on 14 Jan 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!