[5a7589]: / scripts / export_qupath_roi_json.groovy

Download this file

84 lines (68 with data), 2.8 kB

 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
// a script for exporting ROIs as vertices and other metadata in JSON format
// to be replaced with proper serialization of qupath.lib.roi.AreaROI
// In order to run it, you'll need to create a QuPath project,
// load the image and annotation,
// go to the "Automate" drop down menu,
// and press first item "Show script editor".
// From the script editor, open and run the script attached --
// it must export coordinates into the project directory.
//--------------------------------------------------------
//groovy.json.methods.collect { println it.name };
//@Grab("org.codehaus.groovy:groovy-json")
//import groovy.json.*
//import flexjson.JSONDeserializer
//import com.fasterxml.jackson.core.JsonGenerationException;
//import com.fasterxml.jackson.databind.JsonMappingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.SerializationFeature;
//ObjectMapper mapper = new ObjectMapper();
// enable pretty printing
//mapper.enable(SerializationFeature.INDENT_OUTPUT);
//JSONSerializer serializer = new JSONSerializer();
// serializer.serialize( person );
//public interface JsonSerializer<qupath.lib.roi.AreaROI>
// class AreaSerializer implements JsonSerializer<qupath.lib.roi.AreaROI>() {
// public JsonElement serialize(Id id, Type typeOfId, JsonSerializationContext context) {
// return new JsonPrimitive(id.getValue());
// }
// }
//import java.io.IOException;
import org.json.*
def serializeArea(roi){
JSONObject jroi = new JSONObject();
// serialize the object
for (prp in ['roiType', 'empty', 'area']){
jroi.put(prp, roi[prp]);
}
for (prp in ["geometryType"]){
jroi.put(prp, roi.geometry[prp]);
}
jroi.put("interiorPoint", [roi.geometry.interiorPoint.x, roi.geometry.interiorPoint.y])
jroi.put("centroid", [roi.centroidX, roi.centroidY])
jroi.put("boundsStart", [roi.boundsX, roi.boundsY])
jroi.put("boundsSize", [roi.boundsWidth, roi.boundsHeight])
// println roi.geometry.coordinates[0].x
// jroi.put("vertices", roi.geometry.coordinates)
def jvert = new ArrayList<>();;
for (point in roi.getPolygonPoints()){
jvert.add([point.x, point.y]);
}
jroi.put("vertices", jvert)
return jroi;
}
// make a file name and directory
def entry = getProjectEntry()
def name = entry.getImageName() + '.txt'
def path = buildFilePath(PROJECT_BASE_DIR, 'annotation')
mkdirs(path)
path = buildFilePath(path, name)
// collect ROIs
JSONArray jstr = new JSONArray();
for (annotation in getAnnotationObjects()) {
def pathClass = annotation.getPathClass()
def roi = annotation.getROI()
jstr.put(serializeArea(roi));
}
File file = new File(path)
file.write jstr.toString();
println jstr;