Obtain voxels coordinate of an image


I used 3d slicer to segment a part of a brain image (from a *.nrrd file) and I need to obtain the the voxel coordinates of the segmentation. Actually I need the coordinates of all the inner and boundary voxels as a list like {[x1,y1,z1],…,[xn,yn,zn]} and the order of the coordinates is not important.
I appreciate if any one could help me.

You can export the segmentation as labelmap volume and then voxels as a numpy array using slicer.util.arrayFromVolume. If you need voxel IJK coordinates then you can use this example.

Note that storing a segmentation by listing coordinates of all its points is an extremely inefficient representation to read, write, and process. Are you sure this is what you need? What do you plan to do with the coordinate list?

Thanks for the quick reply,
As a matter of fact I need voxelized visualization and in addition I need to use the norm distance between different voxels for some data analysis purpose, for these reasons I considered to have a list of coordinate so I can find for example the mean point of the voxels and etc.

Unless you have very sparse data (e.g., few ten points in a grid containing millions of points), computations can be implemented much more efficiently if points are represented in a rectilinear grid rather than as a list of random coordinates. Computing center of gravity, distance map, Hausdorff distance, etc. are all available for labelmap representation and you don’t even need to think about implementing, testing, and maintaining them.

Let me be more precise, imagine I have a segmentation and I want to select for example 5 specific points (they are not on the boundary) from the segmentation and then find the mean value of them (call it M), after that I want to measure the distance of all the segmentation’s voxels to the point M
the questions are

  1. How to find the coordinates of the 5 points
  2. How to find the coordinates of the segmentation’s voxels

You can specify points using markups and use this script to get IJK coordinates.

You can export all segments to a merged labelmap and use the script referenced above. Or, slightly modify the script to use segmentation node instead of volume node (the difference is that you need to retrieve VTK image data from the segmentation object stored in the segmentation node; instead of getting it from a volume node).

Thank you Andras that was really Helpful :pray: