Export from QuPath with contrast adjustments

Hello Qupathers,

Is there a way to produce downsampled images with contrast adjustments for multichannel images? (the ability to split and save channels as well would be great!)

I started with the script from Exporting single-composite images , but I couldn’t figure out how to deal with the missing ImageWriterTools. I am using 0.2.0-m9.

The below script is currently working quite well for what I’d like, except that I have not figured out how to implement auto-brightness/contrast (or even better specific brightness/contrast parameters)

Thanks,
George

//Modified from this post: Macro Image displaying annotations

double downsample = 10.0

def server = getCurrentServer()
def request = RegionRequest.createInstance(server, downsample)
def img = server.readBufferedImage(request)

def name = getProjectEntry().getImageName()
def path = buildFilePath(PROJECT_BASE_DIR,‘downsample’, name+’-downsample.tif’)
writeImage(img, path)
print ‘Done!’

Hmm, the adjustments to brightness and contrast can be handled as shown here for the viewer:


Generally if I want to maintain those, I export through ImageJ where the resulting image keeps the current brightness and contrast settings:
image

You may also be able to use the last of Pete’s scripts to export the image as per the current viewer.
https://qupath.readthedocs.io/en/latest/docs/advanced/exporting_images.html

1 Like

Thanks so much for the reply and advice.

I can make the brightness/contrast adjustments and export from the viewer (thanks for the link), but I do not know how to then create a downsampled image from the viewer (Pete’s scripts also do not appear to cover this scenario in the example… I tried to mix-and-match parts of the scripts, but server and viewer commands do not play nice together.)

The second part of the suggestion sounds great. Do you have a script (or example components of scripts) for sending an image to ImageJ and saving via ImageJ (or passing back to QuPath and saving the downsampled image)?

(I am trying to learn how to script in QuPath, but I am not quite sure how to learn what I need to know! The workflow tab does not appear to reveal a lot of the actions that I am trying to script. I am trying to investigate available options using IntelliJ rather than the script editor, but the process has been quite slow.)

Thanks,
George

1 Like

You might be able to get some of the scripts down near the bottom of this to work, but they are also from older versions, and I am certain the ImageJ export has changed recently since the options shown in that window did not used to exist!
https://gist.github.com/Svidro/5e4c29630e8d2ef36988184987d1028f

I don’t know the specifics though, maybe someone else will have a better idea.

1 Like

Ah, maybe not at this time…
https://qupath.readthedocs.io/en/latest/docs/advanced/imagej.html

Note

Unfortunately, it is not currently possible to send color-transformed pixels from QuPath to ImageJ through the macro runner.
1 Like

What will the exported image be used for next?

For multichannel images saves as 16-bit (for example) the brightness/contrast is rather dependent on the viewing software (e.g. you could set it in an ImageJ tiff, but only ImageJ would interpret it properly). But if you want to convert to RGB for display only and apply the brightness and contrast at this point, then the task is quite different.

1 Like

Context: our facility runs a Zeiss Axioscan as a slide scanning service, producing so many pyramidal .czi files. Since Fiji/ImageJ doesn’t play very nicely with pyamidal files (I did have some luck exporting them as pyramidal ome-tiff) and the Zeiss software is Windows-only (and otherwise missing some batchable features), I am learning QuPath for myself and to teach the researchers with Axioscan data who would benefit.

For the particular user that I am helping, she has many whole slide images that she wants to summarize for display purposes. So, I believe that a downsampled RGB would be appropriate for this task. For her transmitted light images (on a 8-bit RGB detector), the script that I have works wonderfully. For her fluorescence images (16-bit monochrome detector), my current script does not work as well since the image histograms are not filling the 16-bit range. So, I would want to (1) change the B/C of the 16-bit images, (2) downsample, and (3) save as RGB.

(she is also using QuPath on her full images for color deconvolution and subsequent H-DAB cell detection, which has been fantastic!)

2 Likes

You could start with this:

double downsample = 10.0

def server = getCurrentServer()
def request = RegionRequest.createInstance(server, downsample)
def imp = IJTools.convertToImagePlus(server, request).getImage()

At that point imp is a standard ImagePlus object, with methods such as setDisplayRange and the ability to flatten or save. I think this is the easiest way to have full control over the export.

See https://qupath.readthedocs.io/en/latest/docs/scripting/overview.html#working-with-imagej for a bit more information.

If you don’t mind having to have the image open in the viewer, you can create a RenderedImageServer that reflects the current viewer settings - and also adjust these settings in the script if needed. For example:

import qupath.lib.gui.images.servers.RenderedImageServer

double downsample = 10.0

// Specify contrast in the current viewer for a specified channel
setChannelDisplayRange('DAPI', 0, 100)

def viewer = getCurrentViewer()
def server = RenderedImageServer.createRenderedServer(viewer)

def request = RegionRequest.createInstance(server, downsample)
def img = server.readBufferedImage(request)

def name = getProjectEntry().getImageName()
def path = buildFilePath(PROJECT_BASE_DIR,'downsample', name+'-downsample.tif')
writeImage(img, path)
print 'Done!'

I’m glad! :smiley:

3 Likes

Thanks Pete! I think these example scripts and links will set me in the right direction to continue making changes when needed (time to learn how to talk to ImageJ!)

2 Likes