getVoxel returns zero after makeSubHyperStack


I am trying to do some image analysis using ImageJ libraries in a Java application. I have TIF files with Z stack and 3 channels. To do some analysis/quantification I wanted to use getVoxel method, to access a voxel with the given x, y and z coordinates. This works… However, my images have 3 channels… I wanted to separate them in 3 Hyperstacks.
For this, I used SubHyperstackMaker.makeSubhyperstack:

ImagePlus ijNuclei = SubHyperstackMaker.makeSubhyperstack(ij, "1", "1-" + dimensions[3], "1");

I can also display with and everything is fine. But after makeSubhyperstack subHyperstack returns always 0 (for every voxel), which is not correct. I can access same with ij.getImageStack().getVoxel(x, y, z) and it returns intensity. I can continue with this, but z also includes different channels (rgbrgbrgb…) and that is the reason for generating a substack in the first place. It also looks fine in the window.

Is my approach false? I need a fast and easy way to access voxels with x, y, z coordinates.


You need to convert the C,Z and T hyperstack position to a stack z index using the ImagePlus.getStackIndex() method. The following JavaScript example creates a 5D hyperstack and then reads the value at (150,70) of channel 2, slice 3 and frame 4. Note that C, Z and T are one-based indexes and x, y and z are zero-based.

img = IJ.createImage(“HS”,“16-bit label”,400,300,3,4,5);
z = img.getStackIndex(2,3,4) - 1;
stack = img.getStack();
value = stack.getVoxel(150, 70, z);
print("Value at C=2, Z=3, T=4, x=150, y=70 is "+value);;


Great, thanks a lot! This also helped me to understand the libraries better.