Z project ignoring NaNs

imagej

#1

Hello everyone,
I have a stack of 32-bit images which contain NaNs at the different locations. I want to produce an average image in a way, that NaN is ignored while I produce the average image.

I found on the web an old plugin by Rasband, Wayne that make it (plugin code below).
I can run successfully the plugin on my Win7 machine but if fails on my mac machine

I get an following error message.

Z_Project_Ignore_NaNs not up-to-date because 1 source files are not up-to-date (/var/folders/kn/k6s1jwgd1kd5n6p1h7dq3jg40000gp/T/java3097215241423589642/src/main/java/Z_Project_Ignore_NaNs.java)

and a lot more that does not fit in the message

Any idea how to solve this?
Thanks a lot in advance for your feedback.
Claudio

Plugin code:

import ij.;
import ij.process.
;
import ij.plugin.*;

public class Z_Project_Ignore_NaNs implements PlugIn {
private Object stack;

public void run(String arg) {
	ImagePlus imp = IJ.getImage();
	ImagePlus projection = zProject(imp);
	projection.show();
}

public ImagePlus zProject(ImagePlus imp) {
	ImageStack stack = imp.getStack();
	int w = stack.getWidth();
	int h = stack.getHeight();
	int d = stack.getSize();
	ImagePlus projection = IJ.createImage("Projection", "32-bit Black", w, h, 1);
	ImageProcessor ip = projection.getProcessor();
	double value;
	for (int x=0; x<w; x++) {
		for (int y=0; y<h; y++) {
			double sum = 0.0;
			int count = 0;
			for (int z=0; z<d; z++) {
				value = stack.getVoxel(x, y, z);
				if (!Double.isNaN(value)) {
					sum += value;
					count++;
				}
			}
			ip.setf(x, y, (float)(sum/count));
		}
	}
	ip.resetMinAndMax();
	return projection;
}

}


#2

Good day Claudio,

the plugin-code of Wayne
http://imagej.nih.narkive.com/tKH91mp1/images-with-nans
works perfectly here.

With the posted code I realized a missing asterix in the first line:

import ij.;

Of course the line should read:
import ij.*;

Here is a macro that performs both kinds of mean z-projection of a test stack consisting of two slices:

newImage("Img-1", "32-bit black", 128, 128, 1);
run("Select All");
run("Set...", "value=1");
newImage("Img-2", "32-bit black", 128, 128, 1);
run("Specify...", "width=64 height=128 x=0 y=0");
run("Set...", "value=1");
run("Make Inverse");
run("Set...", "value=NaN");
run("Images to Stack", "name=Test-Stack title=[] use");
run("Z Project Ignore NaNs");
selectWindow("Test-Stack");
run("Z Project...", "projection=[Average Intensity]");

Regards

Herbie


#3

Thank you Herbie for your reply.
the code I run indeed has the asterisk missing. Indeed the code, as I mentioned in my initial message, works on my Win7 machine. The problem is that the same code gives me the error on my mac machine. I guess this must be a problem of my Java installation but somehow I cannot solve. I tried re-installing Java 8 and Java 6, updating FIJI but all of that did not help.
Any idea? The error message I get is very long and I posted just the first line (cannot post more as the # of characters exceeds what it’s allowed).
Thanks for helping.


#4

Claudio,

please re-compile the Java-code:

import ij.*;
import ij.process.*;
import ij.plugin.*;

public class Z_Project_Ignore_NaNs implements PlugIn {
private Object[] stack;

public void run(String arg) {
ImagePlus imp = IJ.getImage();
ImagePlus projection = zProject(imp);
projection.show();
}

public ImagePlus zProject(ImagePlus imp) {
ImageStack stack = imp.getStack();
int w = stack.getWidth();
int h = stack.getHeight();
int d = stack.getSize();
ImagePlus projection = IJ.createImage("Projection", "32-bit Black", w, h, 1);
ImageProcessor ip = projection.getProcessor();
double value;
for (int x=0; x<w; x++) {
for (int y=0; y<h; y++) {
double sum = 0.0;
int count = 0;
for (int z=0; z<d; z++) {
value = stack.getVoxel(x, y, z);
if (!Double.isNaN(value)) {
sum += value;
count++;
}
}
ip.setf(x, y, (float)(sum/count));
}
}
ip.resetMinAndMax();
return projection;
}
}

It runs here on my Mac under macOS 10.11.6, plain ImageJ 1.51v2 and Java 1.6.0_65 (64-bit) and I can’t imagine why it shouldn’t work with Java 8 but I can’t test the latter.

Have success

Herbie


#5

Hi Herbie, thanks again. I recompiled using the code you posted but I still get the message and no result image.


this is a picture of the message I get. I’m running macOS 10.13.3 and IJ 1.51v18. Any other idea as to where to look to solve the issue?


#6

Claudio,

it appears to be a Java problem.

Can’t help you with this but maybe you try with a new download of plain ImageJ from
https://imagej.nih.gov/ij/download.html

Regards

Herbie


#7

Thanks Herbie,
I tested in plain imageJ (was using FIJI before) and it works no pb. I went back to and tried to install the plugin (making plugin>install) and then it told me that the java compiler was not up to date and missing files, asking me to download them. then restarted and it worked on FIJI as well.
Thanks for your help.
Claudio