-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertRGBstackTo32bitHyperstack.py
33 lines (28 loc) · 1.27 KB
/
ConvertRGBstackTo32bitHyperstack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from ij import IJ, ImagePlus, ImageStack, CompositeImage
# Load a stack of images: a fly brain, in RGB
imp = IJ.openImage("https://imagej.nih.gov/ij/images/flybrain.zip")
stack = imp.getImageStack()
# A new stack to hold the data of the hyperstack
stack2 = ImageStack(imp.width, imp.height)
# Convert each color slice in the stack
# to two 32-bit FloatProcessor slices
for i in xrange(1, imp.getNSlices()+1):
# Get the ColorProcessor slice at index i
cp = stack.getProcessor(i)
# Extract the red and green channels as FloatProcessor
red = cp.toFloat(0, None)
green = cp.toFloat(1, None)
# Add both to the new stack
stack2.addSlice(None, red)
stack2.addSlice(None, green)
# Create a new ImagePlus with the new stack
imp2 = ImagePlus("32-bit 2-channel composite", stack2)
imp2.setCalibration(imp.getCalibration().copy())
# Tell the ImagePlus to represent the slices in its stack
# in hyperstack form, and open it as a CompositeImage:
nChannels = 2 # two color channels
nSlices = stack.getSize() # the number of slices of the original stack
nFrames = 1 # only one time point
imp2.setDimensions(nChannels, nSlices, nFrames)
comp = CompositeImage(imp2, CompositeImage.COLOR)
comp.show()