Skip to content

Instantly share code, notes, and snippets.

@lesolorzanov
Last active December 9, 2020 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lesolorzanov/998a85aa8ec7a3e87b1de41817d1c940 to your computer and use it in GitHub Desktop.
Save lesolorzanov/998a85aa8ec7a3e87b1de41817d1c940 to your computer and use it in GitHub Desktop.
Importig Annotation path objects after classifying outside of QuPath
// Title: Importig Annotation path objects after classifying outside of QuPath
// Author: Leslie Solorzano
// Date: 9 dec 2020
import qupath.lib.objects.PathObjects
import qupath.lib.roi.ROIs
import qupath.lib.regions.ImagePlane
import qupath.lib.io.GsonTools
import qupath.lib.geom.Point2
import qupath.lib.objects.classes.PathClass
import qupath.lib.objects.classes.PathClassFactory
def plane = ImagePlane.getPlane(0, 0)
//Dialogs didn't work so I just add the filename manually
def loc="/home/leslie/"
def gson=GsonTools.getInstance(true)
BufferedReader bufferedReader = new BufferedReader(new FileReader(loc+"file.json"));
HashMap<String, String> myjson = gson.fromJson(bufferedReader, HashMap.class);
def classmap=["Class0":[41, 83, 185] ,"Class1":[255, 127, 14] ,"Class2":[234, 134, 213] ,
"Class3":[234, 35, 37] ,"Class4":[148, 103, 189] ,"Class5":[38, 153, 177]]
pathClasses=[]
for(c in classmap){
v=c.value;k=c.key;
pathClasses.add(PathClassFactory.getPathClass(k, getColorRGB(v[0],v[1],v[2])));
}
xCoords = myjson["allx"]; yCoords = myjson["ally"]
classes_index = myjson["allc"]
//this should be empty , I just call it to get the right data type for the list
annotations = getAnnotationObjects()
for (c=0; c < xCoords.size(); c++) {
//copy points because (double[], double[], plane) didn't work
List<Point2> points = []
def xarr= xCoords[c] as double[]
def yarr= yCoords[c] as double[]
for( i=0; i< xarr.size();i++){
points.add(new Point2(xarr[i], yarr[i]));
}
def cell_roi = ROIs.createPolygonROI(points, plane)
def cell = PathObjects.createAnnotationObject(cell_roi)
annotations.add(cell)
cell.setPathClass(pathClasses[ classes_index[c] ])
}
def viewer = getCurrentViewer()
def imageData = viewer.getImageData()
def hierarchy = imageData.getHierarchy()
hierarchy.getRootObject().addPathObjects(annotations);
hierarchy.fireHierarchyChangedEvent(this)
print "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment