Skip to content

Instantly share code, notes, and snippets.

@kmader
Created March 5, 2014 15:46
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/9369798 to your computer and use it in GitHub Desktop.
Save kmader/9369798 to your computer and use it in GitHub Desktop.
Calculate the average value in each slice
image = WindowManager.getCurrentImage()
stack = image.getStack() # get the stack within the ImagePlus
n_slices = stack.getSize() # get the number of slices
# iterate over every slice
for sliceIndex in range(1,n_slices+1):
curSlice=stack.getProcessor(sliceIndex)
sumVal=0;countPixel=0
# iterate over every pixel in the slice
for cPixel in curSlice.toFloatArrays()[0]:
sumVal+=cPixel
countPixel+=1
IJ.log("Slice:"+str(sliceIndex)+", Mean:"+str(sumVal/countPixel))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment