Accessing pixel values in images

HI all,
I am trying to read an image in ImageJ Framework . I want to read the pixel by pixel value. I am able to go to particular positions in the image using RandomAccess but I am not able to read the pixel values.

I tried using realSum to get the value of a pixel, but I am not able to reset the realSum value. is there another class which I can use to get the pixel by pixel value using the randomAccess ?

I didn’t find anything on the Imgllib2 examples which can help me. I tried working on image-tutorials code and am using the same to get things going.

The following is the code I am using -

public class trial1
	// within this method we define <T> to be a NumericType (depends on the type of ImagePlus)
	// you might want to define it as RealType if you know it cannot be an ImageJ RGB Color image
	public < T extends RealType< T > & NativeType< T > > trial1() throws ImgIOException
		File file = new File( "barbara.png" );
		final ImagePlus img=new Opener().openImage(file.getAbsolutePath());
		final Img<T> img2=ImagePlusAdapter.wrap(img);
		Img<FloatType> img3= (Img<FloatType>) new ImgOpener().openImg("barbara.png",new ArrayImgFactory<FloatType>());
		Img< FloatType > img4 = (Img< FloatType >) new ImgOpener().openImg( "DrosophilaWing.tif",
	            new ArrayImgFactory< FloatType >() );
		Img< FloatType > duplicate = copyImageCorrect( img4, new CellImgFactory< FloatType >( 20 ) );
	 public < T extends Type< T >&RealType<T>> Img< T > copyImageCorrect( final Img< T > input,
			    final ImgFactory< T > imgFactory )
	    Img< T > output = imgFactory.create( input, input.firstElement() );
	    Cursor< T > cursorInput = input.localizingCursor();
	    RandomAccess< T > randomAccess = output.randomAccess();
	    RandomAccess< T > r2 = output.randomAccess();
	    RealSum a1=new RealSum();
	    while ( cursorInput.hasNext())
	      randomAccess.setPosition( cursorInput );
	      randomAccess.get().set( cursorInput.get() );
	      int[] new_position=new int[2];//change 2 to the number of dimensions in image
	      //need pixel Values here !!! 
	    System.out.println(r2.getDoublePosition(0)+" "+r2.getDoublePosition(1));
	    try {
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
	     RealSum a2=new RealSum();
	    return output;
	public static void main( String[] args ) throws ImgIOException
		// open an ImageJ window
		new ImageJ();

		// run the example
		new trial1();
1 Like

I got the answer the value can be accessed by


you should try the tutorials: Everything is explained in detail there.

1 Like

Here is a sample code that opens an image, inverts it and saves it back:

import ij.ImagePlus;
import ij.process.ImageProcessor;

ImagePlus imgPlus = new ImagePlus("path-to-sample.jpg");
ImageProcessor imgProcessor = imgPlus.getProcessor();
FileSaver fs = new FileSaver(imgPlus);

And here’s a sample code that shows how to manipulate an image to make it grayscale:

BufferedImage bufferedImage = imgProcessor.getBufferedImage();
for(int y=0;y<bufferedImage.getHeight();y++)
    for(int x=0;x<bufferedImage.getWidth();x++)
        Color color = new Color(bufferedImage.getRGB(x, y));
        int grayLevel = (color.getRed() + color.getGreen() + color.getBlue()) / 3;
        int r = grayLevel;
        int g = grayLevel;
        int b = grayLevel;
        int rgb = (r<<16)  | (g<<8)  | b;
        bufferedImage.setRGB(x, y, rgb);
ImagePlus grayImg = new ImagePlus("gray", bufferedImage);
fs = new FileSaver(grayImg);

I hope it helps you get started :slight_smile:

1 Like

The code you posted is using ImageJ1-specific classes that are tightly bound to the Java AWT user interface. If I understood correctly, the original question was about using ImgLib2 data structures.

I didn’t notice that. Thank you for bringing up the point :slightly_smiling: