| 1 |
% Copyright (c), ETH Zurich and UNC Chapel Hill. |
| 2 |
% All rights reserved. |
| 3 |
% |
| 4 |
% Redistribution and use in source and binary forms, with or without |
| 5 |
% modification, are permitted provided that the following conditions are met: |
| 6 |
% |
| 7 |
% * Redistributions of source code must retain the above copyright |
| 8 |
% notice, this list of conditions and the following disclaimer. |
| 9 |
% |
| 10 |
% * Redistributions in binary form must reproduce the above copyright |
| 11 |
% notice, this list of conditions and the following disclaimer in the |
| 12 |
% documentation and/or other materials provided with the distribution. |
| 13 |
% |
| 14 |
% * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of |
| 15 |
% its contributors may be used to endorse or promote products derived |
| 16 |
% from this software without specific prior written permission. |
| 17 |
% |
| 18 |
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 |
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 |
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 |
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
% POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
|
| 31 |
function [cameras, images, points3D] = read_model(path) |
| 32 |
% Read COLMAP model from folder, which contains a |
| 33 |
% cameras.txt, images.txt, and points3D.txt. |
| 34 |
|
| 35 |
if numel(path) > 0 && path(end) ~= '/' |
| 36 |
path = [path '/']; |
| 37 |
end |
| 38 |
|
| 39 |
cameras = read_cameras([path 'cameras.txt']); |
| 40 |
images = read_images([path 'images.txt']); |
| 41 |
points3D = read_points3D([path 'points3D.txt']); |
| 42 |
|
| 43 |
end |
| 44 |
|
| 45 |
function cameras = read_cameras(path) |
| 46 |
|
| 47 |
cameras = containers.Map('KeyType', 'int64', 'ValueType', 'any'); |
| 48 |
|
| 49 |
fid = fopen(path); |
| 50 |
tline = fgets(fid); |
| 51 |
while ischar(tline) |
| 52 |
elems = strsplit(tline); |
| 53 |
if numel(elems) < 4 || strcmp(elems(1), '#') |
| 54 |
tline = fgets(fid); |
| 55 |
continue |
| 56 |
end |
| 57 |
|
| 58 |
if mod(cameras.Count, 10) == 0 |
| 59 |
fprintf('Reading camera %d\n', cameras.length); |
| 60 |
end |
| 61 |
|
| 62 |
camera = struct; |
| 63 |
camera.camera_id = str2num(elems{1}); |
| 64 |
camera.model = elems{2}; |
| 65 |
camera.width = str2num(elems{3}); |
| 66 |
camera.height = str2num(elems{4}); |
| 67 |
|
| 68 |
camera.params = zeros(numel(elems) - 5, 1); |
| 69 |
for i = 5:numel(elems) - 1 |
| 70 |
camera.params(i - 4) = str2double(elems{i}); |
| 71 |
end |
| 72 |
|
| 73 |
cameras(camera.camera_id) = camera; |
| 74 |
|
| 75 |
tline = fgets(fid); |
| 76 |
end |
| 77 |
|
| 78 |
fclose(fid); |
| 79 |
|
| 80 |
end |
| 81 |
|
| 82 |
function images = read_images(path) |
| 83 |
|
| 84 |
images = containers.Map('KeyType', 'int64', 'ValueType', 'any'); |
| 85 |
|
| 86 |
fid = fopen(path); |
| 87 |
tline = fgets(fid); |
| 88 |
while ischar(tline) |
| 89 |
elems = strsplit(tline); |
| 90 |
if numel(elems) < 4 || strcmp(elems(1), '#') |
| 91 |
tline = fgets(fid); |
| 92 |
continue |
| 93 |
end |
| 94 |
|
| 95 |
if mod(images.Count, 10) == 0 |
| 96 |
fprintf('Reading image %d\n', images.length); |
| 97 |
end |
| 98 |
|
| 99 |
image = struct; |
| 100 |
image.image_id = str2num(elems{1}); |
| 101 |
qw = str2double(elems{2}); |
| 102 |
qx = str2double(elems{3}); |
| 103 |
qy = str2double(elems{4}); |
| 104 |
qz = str2double(elems{5}); |
| 105 |
image.R = quat2rotmat([qw, qx, qy, qz]); |
| 106 |
tx = str2double(elems{6}); |
| 107 |
ty = str2double(elems{7}); |
| 108 |
tz = str2double(elems{8}); |
| 109 |
image.t = [tx; ty; tz]; |
| 110 |
image.camera_id = str2num(elems{9}); |
| 111 |
image.name = elems{10}; |
| 112 |
|
| 113 |
tline = fgets(fid); |
| 114 |
elems = sscanf(tline, '%f'); |
| 115 |
elems = reshape(elems, [3, numel(elems) / 3]); |
| 116 |
image.xys = elems(1:2,:)'; |
| 117 |
image.point3D_ids = elems(3,:)'; |
| 118 |
|
| 119 |
images(image.image_id) = image; |
| 120 |
|
| 121 |
tline = fgets(fid); |
| 122 |
end |
| 123 |
|
| 124 |
fclose(fid); |
| 125 |
|
| 126 |
end |
| 127 |
|
| 128 |
function points3D = read_points3D(path) |
| 129 |
|
| 130 |
points3D = containers.Map('KeyType', 'int64', 'ValueType', 'any'); |
| 131 |
|
| 132 |
fid = fopen(path); |
| 133 |
tline = fgets(fid); |
| 134 |
while ischar(tline) |
| 135 |
if numel(tline) == 0 || strcmp(tline(1), '#') |
| 136 |
tline = fgets(fid); |
| 137 |
continue; |
| 138 |
end |
| 139 |
|
| 140 |
elems = sscanf(tline, '%f'); |
| 141 |
if numel(elems) == 0 |
| 142 |
tline = fgets(fid); |
| 143 |
continue; |
| 144 |
end |
| 145 |
|
| 146 |
if mod(points3D.Count, 1000) == 0 |
| 147 |
fprintf('Reading point %d\n', points3D.length); |
| 148 |
end |
| 149 |
|
| 150 |
point = struct; |
| 151 |
point.point3D_id = int64(elems(1)); |
| 152 |
point.xyz = elems(2:4); |
| 153 |
point.rgb = uint8(elems(5:7)); |
| 154 |
point.error = elems(8); |
| 155 |
point.track = int64(elems(9:end)); |
| 156 |
point.track = reshape(point.track, [2, numel(point.track) / 2])'; |
| 157 |
point.track(:,2) = point.track(:,2) + 1; |
| 158 |
|
| 159 |
points3D(point.point3D_id) = point; |
| 160 |
|
| 161 |
tline = fgets(fid); |
| 162 |
end |
| 163 |
|
| 164 |
fclose(fid); |
| 165 |
|
| 166 |
end |