Determine number of channels in groovy script

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?


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:

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.