forked from mporter-gre/mtools
/
readEllipseROIs.asv
133 lines (123 loc) · 8.92 KB
/
readEllipseROIs.asv
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
131
132
133
function [roiIdx roishapeIdx xmlStruct] = readEllipseROIs(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.
%[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 ellipse we also need the angle of rotation and the
%name of the ROI.
thisroishapeT = [];
thisroishapeZ = [];
thisroishapeX = [];
thisroishapeY = [];
thisroishapeWidth = [];
thisroishapeHeight = [];
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 ellipse and get the rotation angle from the
%transform if it exists. From the SVG section of the xml.
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, 'ellipse')
for thisShapeAttr = 1:length(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(thisSVG).children(thisShape).attributes)
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(thisSVG).children(thisShape).attributes(thisShapeAttr).name, 'transform')
thisroishapeTransformMatrix = xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(thisSVG).children(thisShape).attributes(thisShapeAttr).value;
partText = thisroishapeTransformMatrix(8:end);
[firstPart theRest] = strtok(partText);
if str2double(firstPart) >0 && str2double(firstPart) <1
rotAngle = acosd(str2double(firstPart)); %Get the angle in degress by taking the inverse cos (acosd) of the first part of the transformation matrix.
continue;
else
[firstPart theRest] = strtok(theRest);
if str2double(firstPart) >0 && str2double(firstPart) <1
rotAngle = asind(str2double(firstPart)); %Get the angle in degress by taking the inverse sin (asind) of the second part of the transformation matrix.
continue;
else
[firstPart theRest] = strtok(theRest);
if str2double(firstPart) >0 && str2double(firstPart) <1
rotAngle = -asind(str2double(firstPart)); %Get the angle in degress by taking the negative inverse sin (-asind) of the third part of the transformation matrix.
continue;
else
[firstPart theRest] = strtok(theRest, ')');
if str2double(firstPart) >0 && str2double(firstPart) <1
rotAngle = acosd(str2double(firstPart)); %Get the angle in degress by taking the inverse cos (acosd) of the forth part of the transformation matrix.
continue;
end
end
end
end
end
end
end
end
end
end
%Find the name of the ROI and the cx, cy etc. 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;
roishapeIdx{thisROIIdx}.ROIName = thisroishapeName;
end
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).name, 'cx')
thisroishapeX = floor(str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).attributes(thisShapeAttr).value));
end
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).name, 'cy')
thisroishapeY = floor(str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).value));
end
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).name, 'rx')
thisroishapeWidth = floor(str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(thisSVG).children(thisShape).attributes(thisShapeAttr).value))*2;
end
if strcmpi(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(this3rdGen).children(thisAnnotation).name, 'ry')
thisroishapeHeight = floor(str2double(xmlStruct.children(roiIdx(thisROIIdx)).children(roishapeIdx{thisROIIdx}.R(thisROIShapeIdx)).children(thisSVG).children(thisShape).attributes(thisShapeAttr).value))*2;
end
end
end
end
roishapeIdx{thisROIIdx}.T(thisROIShapeIdx) = thisroishapeT;
roishapeIdx{thisROIIdx}.Z(thisROIShapeIdx) = thisroishapeZ;
roishapeIdx{thisROIIdx}.X(thisROIShapeIdx) = thisroishapeX;
roishapeIdx{thisROIIdx}.Y(thisROIShapeIdx) = thisroishapeY;
roishapeIdx{thisROIIdx}.Width(thisROIShapeIdx) = thisroishapeWidth;
roishapeIdx{thisROIIdx}.Height(thisROIShapeIdx) = thisroishapeHeight;
if exist('rotAngle')
roishapeIdx{thisROIIdx}.Angle(thisROIShapeIdx) = rotAngle;
else
roishapeIdx{thisROIIdx}.Angle(thisROIShapeIdx) = 0;
end
thisroishapeT = [];
thisroishapeZ = [];
thisroishapeX = [];
thisroishapeY = [];
thisroishapeWidth = [];
thisroishapeHeight = [];
end
end
end