forked from erhanbas/navigator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_skeleton_graph_from_txt_files.m
61 lines (53 loc) · 2.16 KB
/
load_skeleton_graph_from_txt_files.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function [G, subs] = load_skeleton_graph_from_txt_files(skeleton_folder_name, sample_stack_shape)
% sample_stack_shape is in xyz order. This is the shape of the
% rendered sample, at the highest resolution in the 'octree'.
% Verify the folder exists
if ~exist(skeleton_folder_name, 'file') ,
error('The skeletonization output folder %s does not exist', skeleton_folder_name) ;
end
folder_listing = dir(fullfile(skeleton_folder_name,'*.txt'));
% check the format for a non zero file
txt_file_names = {folder_listing.name} ;
txt_file_sizes = [folder_listing.bytes];
% Figure out the number of columns in each txt file
index_of_first_nonempty_file = find(txt_file_sizes>0,1) ;
fid = fopen(fullfile(skeleton_folder_name, txt_file_names{index_of_first_nonempty_file}));
tline = fgetl(fid);
fclose(fid);
tlines = strsplit(tline,' ');
column_count = size(tlines,2); % first 2 are indicies, rest are weights
% Determine the format based on the column count
format = ['%f %f', sprintf('%s',repmat(' %f',ones(1,column_count-2)))];
txt_file_count = length(txt_file_names) ;
pairs = cell(1,txt_file_count) ;
use_all_cores() ;
parfor idx = 1:txt_file_count
if txt_file_sizes(idx) == 0 ,
continue
end
% read text file line by line
fid = fopen(fullfile(skeleton_folder_name, txt_file_names{idx})) ;
tmp = textscan(fid, format) ;
fclose(fid);
pairs{idx} = cat(2,tmp{:})';
end
raw_edges = [pairs{:}]' ;
% [keepthese,ia,ic] = unique(edges(:, [1 2])) ;
[keepthese, ~, ic] = unique(raw_edges(:, [1 2])) ;
[subs(:,1),subs(:,2),subs(:,3)] = ind2sub(sample_stack_shape([1 2 3]), keepthese) ;
edges = reshape(ic, [], 2) ;
% weights = edges(ia,3:end);
if isempty(edges) ,
return
end
% if isempty(weights) ,
% weights = ones(size(edges_,1),1);
% end
selfinds = find((edges(:,1)==edges(:,2))) ;
if ~isempty(selfinds) ,
edges(selfinds,:) = [] ;
end
A_raw = sparse(edges(:,1),edges(:,2),1,max(edges(:)),max(edges(:)));
A = max(A_raw',A_raw);
G = graph(A) ;
end