Exporting labelled images after tiling a specific annotations

Hi to all,

I’m try to tiling annotated area within a whole slide sections. Using the script find here:

I’m able to tile the whole slide however I need to limit tiling within a specific annotation (eg only tumor).

How I can modify the script to do this?

Thx

If you are able to post any screenshots that would be very helpful, as currently I don’t understand exactly what you need to export and therefore where exactly the current script would need to be changed.

For example:

  • Do you have a single tumor annotation, or multiple annotations? And, if multiple, do they overlap…?
  • Do you have further annotations within the tumor, and do these have different classifications?
  • Is your tumor annotation too big to export in one go? If not, a script like this may help: https://petebankhead.github.io/qupath/scripting/2018/03/13/script-export-import-binary-masks.html
  • Do you want individual tiles, or would you ideally export to a single large (possibly pyramidal) TIFF image? If so, OME-TIFF now gives a way to do that, although it’s not easily accessible within QuPath (yet).

Pretty much any kind of export should be possible* somehow, if it can be defined precisely enough.

Note also that the script has some variables you can set to control what is exported, e.g.

// NOTE: The following parameters only matter if exportAnnotationLabelledImage == true
// Ignore annotations that don't have a classification set
boolean skipUnclassifiedAnnotations = true
// Skip tiles without annotations (only applies to label exports - all image tiles will be written)
boolean skipUnannotatedTiles = true
  • Possible, but not necessarily entirely straightforward…

Hi Pete,

thx for your answers, here some screenshots of my work:

  1. I start from E&O slides as this:

  1. In each slide I draw an annotation including cancer

  1. After that I have to divide tha annotation area in many small tiles like this:

4)Each tile should then be saved in a folder

For each case will be drawn a single annotation including cancer.

I don’t really do this kind of thing, so I’m not sure, but have you tried making those tiles annotations? It should be a checkbox or something when you create the tiles. Then each annotation it cycles through to make an image should be… well, the tile.

Thanks, the screenshot is very helpful although I am still a little confused. If you export only tiles from within the tumor annotation, why would you also need to draw/export a labelled image of the annotation?

In fact I don’t need a lebelled image of anotations my only need is to create tiles from tumor annotation and export them as single images. Using tiling function available in qupath I can do this as I want (eg screenshots 3) however I had no idea how to export the tiles, so I searched for some scripts that once adapted could be useful to me.

What do you want to do with tiles that overlap the boundary of your annotation?

They should be exported like others

Have you looked at something like what David wrote here?

1 Like

Dear mike

thx for your suggestion, it looks just what I need…
However I try to import the script

, but without success, I think it’s because I have to introduce some input into the script eg for path definition what is the correct syntax?
here: ```
def path = “/Your/Path/Here”
`` I try to set a path to a folder like this C:\Users\Asus\Desktop\Melanoma_Qupath\exported_tiles
the output after run was:

Any suggestion to fix this problem?
thx
Lorenzo

Paths to directories should look something like in this post for Windows.
See the very last comment.

Another way to handle it is to create the path within the project similar to the code in the original post you linked. Replace cores with tiles or whatever you want the folder to be called.

// Create output directory inside the project
def dirOutput = buildFilePath(PROJECT_BASE_DIR, 'cores')
mkdirs(dirOutput)

great!!! Now all run perfectely

thx!

1 Like

HELP! I am using Schmolzes script for exporting tiles from annotations extensively. But somehow in the new QuPath version it starts exporting tiles and then for some reason it will stop in the middle of the export. It does not give any error message. I just know it stops as I can hit “Run” again and no further tiles are generated. Any idea @petebankhead?

setImageType('BRIGHTFIELD_H_E');
setColorDeconvolutionStains('{"Name" : "H&E default", "Stain 1" : "Hematoxylin", "Values 1" : "0.65111 0.70119 0.29049 ", "Stain 2" : "Eosin", "Values 2" : "0.2159 0.8012 0.5581 ", "Background" : " 255 255 255 "}');
selectAnnotations();
runPlugin('qupath.lib.algorithms.TilerPlugin', '{"tileSizeMicrons": 233.4,  "trimToROI": false,  "makeAnnotations": true,  "removeParentAnnotation": false}');

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


def path = "some/path"

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)
   
   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++
       
   }

}
print("Done!")

You say no error, but the log should indicate something in terms of print messages indicating where something happened. What does the log show? *View menu

Also that was definitely made for 1.2/1.3, just from a quick glance. A few things there worj at least slightly differently in 0.2.0. You might want to stick to using 1.2 to export. No restrictions on having both around at the same time, and any fixes might break for the next release as well :slight_smile:

1 Like

Hmm, now that I have a computer, it seems that the changes don’t prevent the script from running at all. I was able to export ~2700 tiles without problems, so the difficulty probably lies elsewhere.

1 Like

@Research_Associate Mike. Thank you so much for your fast response! It was certainly made for 1.2/1.3, it’s from the github issue you posted earlier. We have been using this bit to export tiles from annotations for quite some time now. We have switched to 0.2.0 and annotated a large number of whole slides with the new version. So I was really anxious to get this up and running, since I did not want to do all the annotations again in 1.2/1.3 (or is there a way to move annotations from one version to another?).

The curious thing was that the log did not show any error message. The script just stopped with tiles left to be exported and without having printed “Done!”. I think the problem was that the source files (whole slides), the qupath-project and the path I wrote the tiles to, were all in different directories / on different servers. Once I put everything on one machine it worked smoothly. Probably some connection-time-out-kerfuffle - I guess.

Either way, thank you sooo much again for looking into this. Have a good weekend.

1 Like

Thanks for letting us know, might prove useful to someone else troubleshooting the same thing in the future.

1 Like

Hi to all again,

I’m trying to solve other two problems in this context:

  1. I need to export tiles at a fixed magnification eg. 40X but I heve no idea about the script command to do it.

  2. Finally I can create a workflow for a single image, how I can perform a worflow that runs on all the images of the projects?

Any idea?

The script has a downsample factor, which is currently written in as 1 in the line below. There is no 40x, the downsample of 1 means it will give you the base pixels. Any attempt to go under 1 (say you were at 20x and put in 0.5) would result in weird blocky textures, if it even worked. If your images are 80x, you could make it 2, and they would come out as 40x.

def request = RegionRequest.createInstance(imageData.getServerPath(), 
       1, roi)

The script saves all of the files using the image’s file name, so I am not sure why it wouldn’t work for multiple images. If you needed to, you could put the filename into the path variable so that you are creating multiple folders, but you would also need to add in a mkdirs() command, which you can see used in a few of Pete’s scripts, like this one. It creates the directory if none exists.

1 Like