-
Notifications
You must be signed in to change notification settings - Fork 9
/
readLineROIs.m
130 lines (120 loc) · 8.08 KB
/
readLineROIs.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
function [roiIdx roishapeIdx] = readLineROIs(roifile)
%Point this to an ROI file to read out the ROI and ROIShapes in it. Will
%return roiIdx containing the indexes of the true ROIs and the T, Z, X, Y,
%Width and Height information for each ROI Shape withing each ROI.
%
%Author Michael Porter
% Copyright (C) 2009-2014 University of Dundee.
% All rights reserved.
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%[filename filepath] = uigetfile('*.xml');
%Read the ROI xml file into a Matlab struct
xmlStruct = xml2struct(roifile);
%Get the ROI elements from the struct and store their index in roiIdx. Use
%these to get the roiShapes from the struct too.
roiIdx = [];
thisroishapeIdx = [];
counter = 1;
for i = 1:length(xmlStruct.children)
if strcmpi(xmlStruct.children(i).name, 'roi')
roiIdx = [roiIdx i];
numROIShapes = length(xmlStruct.children(i).children);
for j = 1:numROIShapes
if strcmpi(xmlStruct.children(i).children(j).name, 'roishape')
thisroishapeIdx = [thisroishapeIdx j];
end
end
roishapeIdx{counter}.R = thisroishapeIdx;
counter = counter + 1;
thisroishapeIdx = [];
end
end
%For each roishape fetch out the Z and T information and store it in the
%same struct as the roishape.R (R = ROI) index. Then dig deeper and get the
%svg data for the x and y centres of the ROI, and the width and
%height. Since this is line we also need the angle, length and the
%name of the ROI.
thisroishapeT = [];
thisroishapeZ = [];
thisroishapeStartX = [];
thisroishapeStartY = [];
thisroishapeEndX = [];
thisroishapeEndY = [];
thisroishapeName = [];
thisroishapeAngle = [];
thisroishapeLength = [];
for thisROIIdx = 1:length(roishapeIdx)
for thisROIShapeIdx = 1:length(roishapeIdx{thisROIIdx}.R)
thisroishapeT = [thisroishapeT str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).attributes(1).value)];
thisroishapeZ = [thisroishapeZ str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).attributes(2).value)];
%Check if the ROI is an line.
for thisSVG = 1:length(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children)
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(thisSVG).name, 'svg')
for thisShape = 1:length(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(thisSVG).children)
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(thisSVG).children(thisShape).name, 'line')
roishapeIdx{thisROIIdx}.shape = 'line';
end
end
end
end
%Find the name of the ROI, measurementAngle, measurementLength and start/end stroke points from the Annotation section of the xml.
for this3rdGen = 1:length(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children)
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).name, 'annotation')
for thisAnnotation = 1:length(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children)
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).name, 'basicTextAnnotation')
thisroishapeName = xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).attributes(2).value;
if ~strcmp(thisroishapeName, ' ')
roishapeIdx{thisROIIdx}.ROIName = thisroishapeName;
end
end
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).name, 'measurementAngle')
thisroishapeAngle = str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).children(2).attributes(2).value);
end
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).name, 'measurementLength')
thisroishapeLength = str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).children(2).attributes(2).value);
end
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).name, 'measurementStartPointX')
thisroishapeStartX = str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).attributes(2).value);
end
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).name, 'measurementEndPointX')
thisroishapeEndX = str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).attributes(2).value);
end
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).name, 'measurementStartPointY')
thisroishapeStartY = str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).attributes(2).value);
end
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).name, 'measurementEndPointY')
thisroishapeEndY = str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).attributes(2).value);
end
end
end
end
roishapeIdx{thisROIIdx}.T(thisROIShapeIdx) = thisroishapeT;
roishapeIdx{thisROIIdx}.Z(thisROIShapeIdx) = thisroishapeZ;
roishapeIdx{thisROIIdx}.startX(thisROIShapeIdx) = thisroishapeStartX;
roishapeIdx{thisROIIdx}.startY(thisROIShapeIdx) = thisroishapeStartY;
roishapeIdx{thisROIIdx}.endX(thisROIShapeIdx) = thisroishapeEndX;
roishapeIdx{thisROIIdx}.endY(thisROIShapeIdx) = thisroishapeEndY;
roishapeIdx{thisROIIdx}.Angle(thisROIShapeIdx) = thisroishapeAngle;
roishapeIdx{thisROIIdx}.Length(thisROIShapeIdx) = thisroishapeLength;
thisroishapeT = [];
thisroishapeZ = [];
thisroishapeStartX = [];
thisroishapeStartY = [];
thisroishapeEndX = [];
thisroishapeEndY = [];
end
end
end