Skip to content

Instantly share code, notes, and snippets.

@kmader
Created March 5, 2014 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmader/9369975 to your computer and use it in GitHub Desktop.
Save kmader/9369975 to your computer and use it in GitHub Desktop.
ImageJ plugin which calculates the average value in each slice
import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.*;
import ij.plugin.frame.*;
public class AverageSliceValue implements PlugIn {
public void run(String arg) {
ImagePlus imp = IJ.getImage();
ImageStack is = imp.getStack();
int n_slices = is.getSize();
for(int sliceIndex=1;sliceIndex<=n_slices;sliceIndex++) {
double sumVal=0;
long countPixel=0;
ImageProcessor curSlice=is.getProcessor(sliceIndex);
float[][] sliceArray=curSlice.getFloatArray();
for(int x=0;x<sliceArray.length;x++) {
for(int y=0;y<sliceArray[x].length;y++) {
sumVal+=sliceArray[x][y];
countPixel+=1;
}
}
IJ.log("Slice:"+sliceIndex+", Mean:"+sumVal/countPixel);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment