# Select nearest neighbour voxels using some voxels as reference?

**URL:** <https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175>\
**Category:** Development\
**Created:** [February 24, 2021, 8:05am UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175 "2021-02-24T08:05:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [February 24, 2021, 8:05am UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/1 "2021-02-24T08:05:23Z")

</div>

Hi,  
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.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 25, 2021, 6:47am UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/2 "2021-02-25T06:47:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [February 25, 2021, 7:01am UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/3 "2021-02-25T07:01:17Z")

</div>

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

Thank you

Regards,  
Saima Safdar

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [February 25, 2021, 7:59am UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/4 "2021-02-25T07:59:03Z")

</div>

> [@lassoan](#):
>
> rom the closed surface representation o

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.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/f/5f0ee5b773248f7b448959cc78a8d5ea0747d54a.jpeg)

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

Regards,  
Saima Safdar

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 25, 2021, 1:15pm UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/5 "2021-02-25T13:15:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [February 26, 2021, 3:17am UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/6 "2021-02-26T03:17:35Z")

</div>

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

Regards,  
Saima Safdar

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/6/26eaa623cf4666059c8c51cf3a3f57945be05884.png)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 26, 2021, 3:50am UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/7 "2021-02-26T03:50:43Z")

</div>

> [@Saima](#):
>
> When I do not include surface smoothing still the results are attached here not include some of the voxels.

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.

> [@Saima](#):
>
> How can I do the exhaustive search for closest voxels. Any relevant code example?

You can use the examples in the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository) 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.

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [February 26, 2021, 5:10am UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/8 "2021-02-26T05:10:50Z")

</div>

> [@lassoan](#):
>
> > [@Saima](#):
> >
> > voxels.
> 
> 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.

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.

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [February 26, 2021, 5:38am UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/9 "2021-02-26T05:38:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 26, 2021, 12:56pm UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/10 "2021-02-26T12:56:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [March 4, 2021, 6:16am UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/11 "2021-03-04T06:16:35Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [April 6, 2021, 1:49pm UTC](https://discourse.slicer.org/t/select-nearest-neighbour-voxels-using-some-voxels-as-reference/16175/12 "2021-04-06T13:49:37Z")

</div>

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