Get Pixel brightness value from image stack using imagej in java

Tags: #<Tag:0x00007fb87b8b4c88>


I have a tiff image of dimension 64 × 64 × 51, where 51 is my z axis or the number of image stacks. Now I want to get the brightness value for each stack corresponding to its x and y coordinates of pixel using imageJ. So if I want to get brightness value of coordinate 10,12(x,y) in image stack 5 (z) I can map it to corresponding pixels. I am unable to find any api in imageJ library which can do this.

If I work on ImagePlus object, getpixel api gives me pixel array based on x,y coordinate but I am unsure about z coordinate or the stack from which it is coming from.

ImagePlus img=new ImagePlus("some.tiff")
int[] some=img.getPixel(10, 20);

If I get the whole image stack and get the pixel values by stack number again I am unsure of the x,y coordinate. Something like this

ImageStack is=img.getImageStack();
Object som=is.getPixels(10); // Gives me pixel values array for stack 10

I want to know if I can somehow get the pixel value based on the x,y,z coordinate which will make it easier for me to map and make sense of the data. Also I am open to exploring other libraries which can help do this for an image stack in java.

Macro MeasureStack repeated results

Welcome to the forum, @Yogesh_gupta!

I’m referring to your post on StackOverflow so that others interested in this question will be able to find answers in both places.

The ImagePlus and ImageStack objects in the ImageJ 1.x API are essentially organized in slices (planes), so you’ll have to img.setSlice(z) before accessing that plane’s ImageProcessor object. Here’s an illustrative Groovy script that you can run from the script editor in ImageJ/Fiji (it should be straightforward to convert this to Java):

#@ ImagePlus imp
#@ Integer x
#@ Integer y
#@ Integer z
#@output value

value = imp.getPixel(x,y)[0]

(Note that this is using SciJava Parameter annotation to get the required inputs, i.e. the current open image and the requested coordinates.)

You should be aware that in this case, x and y are 0-indexed, while z is 1-indexed, so your example image would give valid values for x = [0, 63], y = [0, 63] and z = [1, 51].

The ImageJ2 (or more precisely ImgLib2) API allows to access pixel data in a different way:

#@ Img img
#@ Integer x
#@ Integer y
#@ Integer z
#@output value

randomAccess = img.randomAccess()
randomAccess.setPosition([x,y,z] as int[])
value = randomAccess.get().getRealDouble()

In this case, all dimensions a 0-indexed, as you might have expected in a Java-based software.


Use the ImageStack.getVoxel(x,y,z) method.