0

I want the intensity of every point of a picture, which function should I call? Let say the pixels of my picture is 125 x 125, I want the intensity of (0,0) to (125,125), is there a function so that I give a coordinate, it will return me an intensity like this

function(0,123) --> intensity?
Jan Eglinger
  • 3,995
  • 1
  • 25
  • 51
pill45
  • 599
  • 3
  • 8
  • 23

2 Answers2

0

In ImageJ macro language:

result = getPixel(0,123);
print(result);

From other scripting languages, you can use the ImageJ Java API, e.g. the ImageProcessor#getPixel(int x, int y) method (ImageJ1) or the net.imglib2.Positionable#setPosition(int[] position) and net.imglib2.Sampler#get() methods (ImageJ2)

For example, in Python:

  • (using ImageJ1 structures)
from ij import IJ

imp = IJ.getImage()
result = imp.getProcessor().getPixel(0, 123)
print result
# @Dataset img
ra = img.randomAccess()
ra.setPosition([0, 123])
result = ra.get()
print result
Jan Eglinger
  • 3,995
  • 1
  • 25
  • 51
0

ImageJ uses the abstract class ImageProcessor, which is "roughly" an extension of the BufferedImage found into java.awt.

You can use one of the method proposed into the ImageProcessor:

public abstract int getPixel(int x, int y);
public abstract int get(int x, int y);
public abstract int get(int index);

But it gets a little bit messy when you want to access pixels encoding on multiple channels (colors images).

Here is a simple way to access pixels using the raster:

ImageProcessor myimage = ... ;
BufferedImage image = myimage.getBufferedImage() ;
int intensity = image.getRaster().getSample(0, 0, 0) ; // Get the value.
image.getRaster().setSample(0, 0, 0, NewValue) ; // Set a new value.

Thats the simplest way, but not the fastest. The fastest being the direct access to the values, which are stored into the DataBuffer, but then you have to deal with the type.

FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
  • For color images, you can use the [`ImagePlus#getPixel(x,y)`](http://javadoc.imagej.net/ImageJ1/index.html?ij/ImagePlus.html) method that returns an int[] array containing the RGBA values. – Jan Eglinger Sep 15 '15 at 11:14