Get only first channel stack from hyperstack

fiji
imagej
python

#1

Hi all,

Probably a very stupid question, but I have an imageplus which is an hyperstack with 3 channels and 5 slices, and I want to create another imageplus with only the first channel of the first one.

I couldn’t find a function on the ImagePlus API to do that but maybe I missed something…

Thanks a lot.


#2

Nevermind, I think I found something doing exactly that from the jython examples.

For those interested.

from ij import IJ, ImagePlus, ImageStack
 
def extractChannel(imp, nChannel, nFrame):
 """ Extract a stack for a specific color channel and time frame """
 stack = imp.getImageStack()
 ch = ImageStack(imp.width, imp.height)
 for i in range(1, imp.getNSlices() + 1):
   index = imp.getStackIndex(nChannel, i, nFrame)
   ch.addSlice(str(i), stack.getProcessor(index))
 return ImagePlus("Channel " + str(nChannel), ch)
 
imp = IJ.getImage()
extractChannel(imp, 1, 1).show()

#3

Glad you figured it out @lguerard,

In case others find this, there are also a couple of ways to do this through the GUI (or with macros):

Image > Stacks > Tools > Make Substack...
and select the channels you want to keep.

Image > Duplicate
also lets you choose the channels and slices you want to keep.

John