We want to connect the people who have knowledge to the people who need it, to bring together people with different perspectives so they can understand each other better, and to empower everyone to share their knowledge.

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?

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.

## 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)