Select nearest neighbour voxels using some voxels as reference?

Is there a possibility to select nearest neighbor voxels in an MRI based on the label-map. For-example I have the voxels based on some label. then I need to find nearest neighbour voxels that dont belong to voxels identified through label.

I found this
import numpy
volume = array(‘Volume’)
label = array(‘Volume-label’)
points = numpy.where( label == 1 ) # or use another label number depending on what you segmented
values = volume[points] # this will be a list of the label values
values.mean() # should match the mean value of LabelStatistics calculation as a double-check
numpy.savetxt(‘values.txt’, values)

with above I can have voxel values defined by the label-map. how can I get voxel values in the nearest neighbor using these voxel values as reference.

It is simpler to compute nearest neighbor from the closed surface representation of the segmentation.

Hi Andras,
Any direction to help. Is there any thing available in slicer already.

Thank you

Saima Safdar

Hi Andras,
I investigated a little more into this. When I get closed surface some of the voxels in the segmentation are not included in the closed surface.

Then I read this script online

Measure distance of points from surface

But will this affect the accuracy of selection of nearest neighbor cells.

Is there a way to work at the voxels level.

Thanks you

Saima Safdar

You can turn off surface smoothing or do an exhaustive search for closest voxel in a small neighborhood of the closest mesh point.

Hi Andras,
When I do not include surface smoothing still the results are attached here not include some of the voxels. How can I do the exhaustive search for closest voxels. Any relevant code example?

thank you

Saima Safdar


The single voxel difference that you see is most likely not a real difference in 3D, just because a mesh is sliced differently than a labelmap. If a subvoxel error matters for you then it means that the binary labelmap’s voxel size is too large. In general, a voxel size should be 5-10x smaller than your error tolerance.

You can use the examples in the script repository for IJK voxel position from RAS position then you can use standard numpy indexing for retrieving a small neighborhood of voxels around that IJK position, and walk through all the voxels and fine the one that is non-zero and closest to the selected position.

Is there a way to change the voxel size in 3D Slicer? how can I change voxel size and remove any error that is happening.

Hi Andras,
Any relevant code example to compute nearest neighbor from closed surface.

I don’t think I can give further help with this. You should be able to find examples for iterating through elements of a 3D array, computing distance between two points, and finding minimum value in an iteration.

Hi Andras,
Is there any code example to start with for computing nearest neighbor from closed surface representation.

" It is simpler to compute nearest neighbor from the closed surface representation of the segmentation"

You can get the closed surface representation as a vtkPolyData and then find the closest point the same way as for vtkPolyData retrieved from model nodes (using VTK locator classes).

1 Like