Get LabelMap voxel values

how can we extract points from binary volume (labelMap). I confuse between the vertices of each voxel and points. So I need extract points from labelMap.

Thank’s in advance

You can get voxel values of a labelmap volume as a numpy array by calling slicer.util.arrayFromVolume.

Is there other filters to do that in vtk and C++?

In C++ you can get access to the vtkImageData by calling volumeNode->GetImageData(). To map between voxel and physical space, you can use the matrix returned by GetIJKToRAS().

I am trying to get the coordinates of 3D points from vtkImageData(); For example, given a vtkImageData object, called image , I know I can get the voxel values like this:

image = vtk.vtkImageData()
voxels = image.GetPointData().GetScalars().

but what I want is a 3D point.

You can use GetScalarComponentAsDouble(int x, int y, int z, int component) to get value of a voxel. You can get voxel coordinates from physical coordinates using the matrix returned by volumeNode->GetIJKToRAS().

Note that GetScalarComponentAsDouble is extremely slow - it is mainly for testing and debugging. You can sample millions of points at once using vtkProbeFilter.

Thank you so much for the response . as the voxel has 8 vertices, I guess obtain 8 points for each voxel?

If nearest neighbor interpolation is good enough then you could use GetScalarComponentAsDouble. For higher-order interpolation, you could use any of the vtkAbstractImageInterpolator child classes. However, you should use vtkProbeFilter, which is simpler and faster, as it samples all the points that you are interested in at once.

What about this;

image=vtk.vtkImageData();

for each voxel of the volume , to recover the corresponding 3 point, we can do:

double p[3];
int coords={i,j,k} // i, j , k are the coordinates of each vertex of the voxel
image->GetPoint(image->ComputePointId(coords), p);

image->GetPoints(image->ComputePointId(…)) is just a more complicated version of image->GetScalarComponentAsDouble or image->GetScalarPointer and it is suitable if nearest neighbor interpolation is acceptable.

IJK coordinates are voxel coordinates, vertex coordinates are in physical coordinates, so the coordinate values you use are incorrect. I already wrote above 2x that you need to convert using IJK to RAS matrix.

1 Like

Thank you very much for your help , and i know that I must convert the IJK To RAS coordinates. I developped a non-optimized marching cubes algorithm as vtk Filter. I did all computations in ijk coordinates and once the algorithm generates the list of triangles and points , I converted the output from IJK To RAS.

1 Like

Hi Mr

I have just another question. A have Applied IJKTORAS transformation to get the IJK voxel coordinates in word space, but what about normal vectors which I have computed from volume data. Normals vectors need to be transformed into word space or not. If right, what must I use?

Thank’s in advance for your response

Yes, you need to transform vectors by multiplying with the same homogeneous transformation matrix. Note that point positions must be represented as (x, y, z, 1) while vector directions as (x, y, z, 0).

1 Like