Volume calculation of 3D numpy array in python?
I have a 3D array (a is a numpy array of the shape of (512, 512, 133)) which contains non zero values in a certain area. I would like to calculate the volume of non zero area in this 3D numpy array. If I know the pixel spacing (0.7609, 0.7609, 0.5132), how the actual volume can be found in python?
Ms Ark
import numpy as np
units = np.count_nonzero([[[ 0., 0.],
[ 2., 0.],
[ 0., 3.]],
[[ 0., 0.],
[ 0., 5.],
[ 7., 0.]]])
If you know the spacing s between two pixels the volume of a pixel is calculated as the volume of a square (pixel volume) times the amount of pixels you previously determined.
volume = units * pow(s, 3)