Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripting: export tiles within annotation? #62

Closed
schmolze opened this issue Apr 10, 2017 · 6 comments
Closed

scripting: export tiles within annotation? #62

schmolze opened this issue Apr 10, 2017 · 6 comments

Comments

@schmolze
Copy link

I'd like to use QuPath to generate training data for a deep learning model. I am annotating various features (let's say, tumor and stroma), and generating tiles within the annotated regions via Analyze -> Region identification -> Tiles & superpixels -> Create tiles. So far so good. Next I want to export the tiles as separate images, generating a file name according to the enclosing annotation name (e.g. "tumor_tile01.tiff"). I would have thought the following code would give me the enclosing annotation name:

for (annotation in getAnnotationObjects()) {
pathClass = annotation.getPathClass()
print(pathClass)
}

But instead the console prints (a bunch of times):

INFO: null

Any suggestions would be appreciated.

@Svidro
Copy link

Svidro commented Apr 10, 2017

You should get null for any tiles or annotations with no class set, and Tumor, or whatever class is set for any annotations with a set class. If the tiles do not have a class, they will return null.

If the tiles are detections, and have a parent annotation with a class name, you should be able to use something like for all detections, p= detection.getParent() followed by p.getPathClass() being assigned to a value in order to find out what the parent annotation's class was. I am not sure this works if you have converted all of the tiles to annotations (maybe it does!).

@Svidro
Copy link

Svidro commented Apr 10, 2017

Peter also had an imageJ script which might be useful to you in his fourth option on one of the lower responses in this topic.
#57
It involves using the macro runner to export TMA cores, but I suspect you could use it for tiles as well.

I have run into some issues handling names as well, apparently the names are frequently dynamically generated based on the class, so the getName function behaves oddly. Please let me know if you find a workable solution!

@schmolze
Copy link
Author

Thanks Svidro, the getParent() idea seems to be working. I will update this thread with the full script once I have tile export working.

@schmolze
Copy link
Author

I have it all working now, here is the full script for future reference:

import qupath.lib.gui.ImageWriterTools
import qupath.lib.regions.RegionRequest
import qupath.lib.scripting.QPEx

def path = "/Your/Path/Here"

def imageData = QPEx.getCurrentImageData()
def server = imageData.getServer()

def filename = server.getShortServerName()

i = 1

for (annotation in getAnnotationObjects()) {

    roi = annotation.getROI()
    
    def request = RegionRequest.createInstance(imageData.getServerPath(), 
        1, roi)
    
    tiletype = annotation.getParent().getPathClass()
    
    String tilename = String.format("%s_%s%d.jpg", filename, tiletype, i)
    
    ImageWriterTools.writeImageRegion(server, request, path + "/" + tilename);
    
    print("wrote " + tilename)
    
    i++
}

The one remaining minor issue is that, in addition to the tiles, the containing annotation region is saved.

@Svidro
Copy link

Svidro commented Apr 11, 2017

It seems like the easiest way to resolve the minor issue would be swapping the for loop/tiles to detections, if that works with the imagewritertools.

@schmolze
Copy link
Author

I modified the body of the loop as follows, which seems to work for now:

for (annotation in getAnnotationObjects()) {

    roi = annotation.getROI()
    
    def request = RegionRequest.createInstance(imageData.getServerPath(), 
        1, roi)
    
    String tiletype = annotation.getParent().getPathClass()
    
    if (!tiletype.equals("Image")) {
    
        String tilename = String.format("%s_%s%d.jpg", filename, tiletype, i)
    
        ImageWriterTools.writeImageRegion(server, request, path + "/" + tilename);
    
        print("wrote " + tilename)
    
        i++
        
    }

}

I guess you could also explicitly check the enclosing annotation type, like:

if (tiletype.equals("Tumor") || tiletype.equals("Stroma")) {
    // write tile
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants