-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathim2xml.m
85 lines (75 loc) · 2.57 KB
/
im2xml.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function im2xml(fname_EM,size_thr,mode)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% fname_EM: input segmentation file name
% size_thr: minimum element size
% mode: either convhull (findx the convex hull) or fill (only hole fill)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[folder, base_fn, ~] = fileparts(fname_EM);
fname_xml = fullfile(folder, sprintf('%s.xml', base_fn));
if nargin < 3, mode = 'convhull'; end
if nargin < 2, size_thr = 250; end
%-- define template file name and scaling factor
template_xml = 'template.xml';
scale_x = 0.0137;
scale_y = 0.0137;
%-- read xml template
fid = fopen(template_xml,'r');
if fid < 0
error("XML template not found. Ensure that %s is in the working directory.", ...
template_xml);
end
tline = fgetl(fid);
A = cell(0,1);
while ischar(tline)
A{end+1,1} = tline; %#ok<AGROW>
tline = fgetl(fid);
end
fclose(fid);
%-- log image file name and scaling factors
A{10} = sprintf('%s',[' <filename>',fname_EM,'</filename>']);
A{16} = sprintf('%s',[' <scale x="',num2str(scale_x),'" y="',num2str(scale_y),'"/>']);
%-- read image file
img = imread(fname_EM);
%-- clean image and fill holes
CC = bwconncomp(img);
for i=1:CC.NumObjects
if numel(CC.PixelIdxList{i}) < size_thr
img(CC.PixelIdxList{i})=0;
end
end
img = imdilate(img, strel('disk', 2));
if strcmp(mode,'fill')
img = imfill(img,'hole');
% img = bwconvhull(img,'objects'); % optional
end
% imwrite(img, 'preprocessed_hull.tiff'); % for debug
%-- trace the boundary of individual connected components
if strcmp(mode,'fill')
bound_pts = bwboundaries(img);
% points are returned as y,x coordinates but we need x,y
bound_pts = arrayfun(@(v) fliplr(v), bound_pts);
else
bound_pts = {regionprops(img,'ConvexHull').ConvexHull}';
end
%-- log the boundary coordinates into a xml file
fid = fopen(fname_xml,'w');
for i = 1:numel(A)
fprintf(fid,'%s\n', A{i});
end
for iobj = 1 : length(bound_pts)
[x, y] = reducem(bound_pts{iobj}(:,1), bound_pts{iobj}(:,2));
fprintf(fid,'%s\n','<contour name="Unmyelinated Axon" color="#FF8000" closed="true" shape="Contour">');
fprintf(fid,'%s\n',' <property name="GUID"><s></s></property>');
fprintf(fid,'%s\n',' <property name="FillDensity"><n>0</n></property>');
for idx=1:numel(x)
fprintf(fid,'%s\n',[' <point x="',num2str(round(x(idx)*scale_x,2)),...
'" y="',num2str(-round(y(idx)*scale_y,2)),...
'" z="0.00" d="0.03"/>']);
end
fprintf(fid,'%s\n','</contour>');
if mod(iobj,200) == 0, fprintf('.'); end
end
fprintf('\n');
fprintf(fid,'%s','</mbf>');
fclose(fid);
end