Home > Mobile >  Getting all pixels from a 3d image whithin a sphere
Getting all pixels from a 3d image whithin a sphere

Time:01-30

Given the sphere center coordinates and radius and the image in 3D how to get all pixels within the sphere using numpy in python ? Assuming for the 3D image we have indicesX, indicesY, indicesZ arrays containing the pixels coordinates over X,Y and Z axis, centerX, centerY, centerZ contains the coordinates of center for the sphere and r is the radius.

CodePudding user response:

One way you could do this is to construct a boolean mask using the equation for a sphere

mask = (np.square(indicesX - centerX)   np.square(indicesY - centerY)   np.square(indicesZ - centerZ)) <= np.square(r)

indicesX_in_sphere = indicesX[mask]
indicesY_in_sphere = indicesY[mask]
indicesZ_in_sphere = indicesZ[mask]

or you could use the mask directly on the values of the 3D image depending on your needs.

  • Related