-
Notifications
You must be signed in to change notification settings - Fork 2
/
xml_to_QuPath.groovy
119 lines (89 loc) · 3.42 KB
/
xml_to_QuPath.groovy
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
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
//import qupath.lib.roi.PathROIToolsAwt
import qupath.lib.objects.*
import qupath.lib.roi.*
import qupath.lib.regions.*
import qupath.lib.objects.classes.*
String xmlDirectory = 'F:'
boolean use_xmlDir = false
def server = getCurrentImageData().getServer()
String imgPath = server.getPath()
String path2 = server.getPath()
int ind1 = path2.lastIndexOf("/") + 1;
int ind2 = path2.lastIndexOf(".") - 1;
name = path2[ind1..ind2]
path = path2[path2.indexOf('/')+1..path2.lastIndexOf("/")-1]
maskFilename = path + File.separator + name + '.xml'
File fileMask = new File(maskFilename)
if(!fileMask.exists() || use_xmlDir) {
print(maskFilename + ' does not exist or use_xmlDir is set')
maskFilename = xmlDirectory + File.separator + File.separator + name + '.xml'
//maskFilename = maskFilename.replaceFirst('[\\.].*$',customSuffix)
fileMask = new File(maskFilename)
}
if(!fileMask.exists()) {
print(maskFilename + ' does not exist')
return
}
else {
print('Loading mask file ' + fileMask)
}
File xmlFile = fileMask
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance()
DocumentBuilder dBuilder
dBuilder = dbFactory.newDocumentBuilder()
Document doc = dBuilder.parse(xmlFile)
doc.getDocumentElement().normalize();
NodeList Annotation = doc.getElementsByTagName('Annotations');
NodeList Annotations = Annotation.item(0).getElementsByTagName('Annotation');
for (A = 0; A < Annotations.getLength(); A++) {
Integer linecolor = Integer.parseInt(Annotations.item(A).getAttribute('LineColor'));
String name = Annotations.item(A).getAttribute('Name')
NodeList Regions = Annotations.item(A).getElementsByTagName('Regions');
NodeList Region = Regions.item(0).getElementsByTagName('Region');
// Loop through the annotations
for (R = 0; R < Region.getLength(); R++) {
NodeList Vertices = Region.item(R).getElementsByTagName('Vertices')
NodeList Vertex = Vertices.item(0).getElementsByTagName('Vertex')
def coordinatesX = new float[Vertex.getLength()]
def coordinatesY = new float[Vertex.getLength()]
for (V = 0; V < Vertex.getLength(); V++) {
coordinatesX[V] = Float.parseFloat(Vertex.item(V).getAttribute('X'))
coordinatesY[V] = Float.parseFloat(Vertex.item(V).getAttribute('Y'))
}
//def roi = new PolygonROI(coordinatesX,coordinatesY,-1,0,0)
def roi = new PolygonROI(coordinatesX,coordinatesY,ImagePlane.getDefaultPlane())
//pathclass = PathClassFactory.getPathClass('ru',linecolor)
// decode linecolor
Integer[] gbr = [linecolor>>16,linecolor>>8&255,linecolor&255]
if(linecolor == 16711680) {
pathclass = null
}
else {
pathclassLocal = PathClassFactory.getPathClass(name)
//pathClasses = getQuPath().getAvailablePathClasses()
if(pathclassLocal.getColor() - (255<<24) == linecolor) {
pathclass = PathClassFactory.getPathClass(name,ColorTools.makeRGB(gbr[0],gbr[1],gbr[2]))
}
else {
pathclass = PathClassFactory.getPathClass(String.valueOf(linecolor),ColorTools.makeRGB(gbr[0],gbr[1],gbr[2]))
}
}
def pathObject = new PathAnnotationObject(roi, pathclass)
// Add object to hierarchy
addObject(pathObject)
}
}
// lock all annotations
getAnnotationObjects().each {it.setLocked(true)}