Determine number of channels in groovy script

Hello,
I need to run a batch script on fluorescence images to score cells as positive for a given marker based on the mean cell signal. The problem is that in some images the marker of interest in the 4th channel while in others it is the 2nd channel. Is there a way by which I can query the number of channels in the fluorescence image so that I extract the mean signal per cell for the correct fluorescence channel?

Thanks!

Hmm, easiest would probably be to extract the channels prior to analysis, but you can check the number of channels fairly simply.
channels = getQuPath().getViewer().getImageDisplay().getAvailableChannels()
That will create an array. channels.size() would be the number of channels.

Can see it in use in a rather more complex script here:
https://gist.githubusercontent.com/Svidro/6171d6d24a85539d3af5d417bc928d50/raw/983269d24f41313d95580e76b91c948057dfb3ef/Tissue%20Detection%20with%20GUI.groovy

Note that the command changes depending on whether you are using 0.1.2 or 0.2.0m2. Always helps to include that information when posting!

You can use the following:

// If you just want the number (works v0.1.2 and v0.2.0)
def server = getCurrentImageData().getServer()
print server.nChannels()


// If you want to also be able to query channel names/colors (works v0.2.0 only)
print server.getChannels()

@Research_Associate’s approach using getImageDisplay() may well also work, but it’s best to avoid ImageDisplay when it isn’t needed. The number of ‘channels’ it provides for display can include new ‘channels’ generated through color transforms.

Therefore it’s best request the channels directly from the ImageServer, which is the main object used to access pixels and metadata.

1 Like

Oooh, didn’t know that!

Thanks for the replies. Pete’s suggestion worked.

Sorry I forgot to mention that I am using 0.2.0-m2.