# ITK octree

**URL:** https://discourse.itk.org/t/itk-octree/3185
**Category:** Beginner Questions
**Created:** [June 14, 2020, 12:27am UTC](https://discourse.itk.org/t/itk-octree/3185 "2020-06-14T00:27:17Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![rolof](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@rolof](https://discourse.itk.org/u/rolof)
#### Post date: [June 14, 2020, 12:27am UTC](https://discourse.itk.org/t/itk-octree/3185/1 "2020-06-14T00:27:17Z")

</div>

Hi, I am trying to use Octrees on ITK, I saw that ITK have an Octree object. However, I can not find the methods of Octree to get the closest points.

My problem is that I have a region of interest composed of several voxels with x,y,z coordinates in a .nrrd image. I want to put several voxels of this image into an octree structure to query the closest distance to my region of interest. I thought to use Octrees similar to VTK object, but I couldn’t find methods to encode my idea. I saw KD-tress but I am not sure if that structure is the same concept that octree.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [June 17, 2020, 10:40am UTC](https://discourse.itk.org/t/itk-octree/3185/2 "2020-06-17T10:40:47Z")

</div>

Hi @rolof,

Yes, the KD-tree can likely provide the desired functionality. Here is an example:

[https://itk.org/ITKExamples/src/Numerics/Statistics/SpatialSearch/Documentation.html](https://itk.org/ITKExamples/src/Numerics/Statistics/SpatialSearch/Documentation.html)

More information can be found in Section 5.1.7 of the [ITK Software Guide](https://itk.org/ItkSoftwareGuide.pdf).

---

<div class="post-metadata">

### Author: ![rolof](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@rolof](https://discourse.itk.org/u/rolof)
#### Post date: [June 17, 2020, 3:29pm UTC](https://discourse.itk.org/t/itk-octree/3185/3 "2020-06-17T15:29:43Z")

</div>

Ok thank you @matt.mccormick, but in that case, do you know what is the purpose of Octree object in Itk?

And another question, do you know where could I find alternatives to distance search in ITK library? I thought in Octree or Brute force, but I do not know if ITK has a specific section or set of objects to solve this problem.

Thank you again!

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [June 17, 2020, 6:21pm UTC](https://discourse.itk.org/t/itk-octree/3185/4 "2020-06-17T18:21:39Z")

</div>

The Octree class was likely brought in from VTK, but it has not matured.

Yes, you are on the right track with Kd-tree for efficient distance searches. There are examples of application in @ntustison 's ITK-v4 point-set metrics.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [June 17, 2020, 7:21pm UTC](https://discourse.itk.org/t/itk-octree/3185/5 "2020-06-17T19:21:45Z")

</div>

Have you considered distance transform? [Maurer filter](https://itk.org/Doxygen/html/classitk_1_1SignedMaurerDistanceMapImageFilter.html) is the fastest one in ITK proper.

---

<div class="post-metadata">

### Author: ![rolof](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@rolof](https://discourse.itk.org/u/rolof)
#### Post date: [July 6, 2020, 12:44am UTC](https://discourse.itk.org/t/itk-octree/3185/6 "2020-07-06T00:44:39Z")

</div>

Thank you @dzenanz, @matt.mccormick, I am going to take a look to both KD-tree and Maurer filter, I will leave this post opened because probably there are some filters that was not mentioned and I want to test all of them.

Thank you again!

---

<div class="post-metadata">

### Author: ![rolof](https://discourse.itk.org/letter_avatar_proxy/v4/letter/r/41988e/32.png) [@rolof](https://discourse.itk.org/u/rolof)
#### Post date: [July 9, 2020, 12:35am UTC](https://discourse.itk.org/t/itk-octree/3185/7 "2020-07-09T00:35:41Z")

</div>

I tested the Maurer filter, I saw that it is only suitable for a binary image, do you know if there is another filter to multi labeled image. I mean imagine the case that you have three types of voxels in an image, 0: Background, 1:object\_1, 2:object\_2, and you want to compute the euclidean distance for all the points of object\_1 to object\_2 ignoring the computations of the background. Currently, with the Maurer filter, I do not know how to achieve that.

Thanks in advance!

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [July 9, 2020, 1:16am UTC](https://discourse.itk.org/t/itk-octree/3185/8 "2020-07-09T01:16:42Z")

</div>

Binarize your image using threshold filter keeping only object 2. Run Maurer on that. It will compute distances to object 2 for all the pixels of the image, including the ones which belong to object 1. You could then mask the distance image with object 1 label.
