Converting script from IJM to Java: getPixel and setPixel

Hi,
I am converting a script that I did not write from IJM to Java and I am stuck with the methods getPixel and more importantly setPixel.

I found that there is ImagePlus.getPixel(x, y) and ImageProcessor.setPixel(Object) but I don’t know how to make the translation.
Indeed, in the code I am translating, the result of getPixel is a simple int (in my case 255), not an array like in ImagePlus.getPixel(x, y).

Similarly, the setPixel invocation is as simple as setPixel(x, y, value) with value being 255 in my case (I guess, just a white pixel).

Can you help?

Bonus question: is there a documentation or a “readable” code where I could dig up how IJ interprets ijm code and converts it to Java equivalents ?

Thanks.

Hi @diyoyo,

The javadoc is a good place to find stuff like this.
In your case, check out ImagePlus and ImageProcessor in particular.

This is because ImagePlus has to handle more general cases, from here ImagePlus.getPixel:

Returns the pixel value at (x,y) as a 4 element array. Grayscale values are retuned in the first element. RGB values are returned in the first 3 elements. For indexed color images, the RGB values are returned in the first 3 three elements and the index (0-255) is returned in the last.

I don’t believe this is worth your time, but if you were to do it, you’d get the most complete answer by looking at the code directly.

ImageJ2 evangelism

Much of these “annoying” details (like ImagePlus getPixel returning an array) is handled much more cleanly and straightforwardly in ImageJ2 (using imglib2). If you’re already moving macro scripts to java, it may be worth your time. Yes, there’s a learning curve, but there are lots of examples, and tutorials, to help you learn. (Also this forum for asking questions).

John

Much of these “annoying” details (like ImagePlus getPixel returning an array) is handled much more cleanly and straightforwardly in ImageJ2 (using imglib2). If you’re already moving macro scripts to java, it may be worth your time. Yes, there’s a learning curve, but there are lots of examples , and tutorials, to help you learn. (Also this forum for asking questions).

That was the plan. Unfortunately, I got misled by the ImageJ interface, where what you record in Java is actually IJ1 and not IJ2. I am confused by the overpresence of IJ1 in Java examples (and also the fact that all the examples for IJ2 are in Python rather than Java). I’ll have a look at your links.

1 Like

This directory is full of Java examples.
I know… it’s always a matter of knowing where to look… :-/ (part of why the forum is a great resource).

John

Thanks
(I was referring to the built-in examples in FIJI’s script editor)
From what I can see in the link you provided, there is still a lot of imports related to ij.xx instead of imglib2.xx. So I guess the hard part will be to decide where in my code I will benefit from imglib2.