# PointSet to PointSet registration with random sampling

**URL:** <https://discourse.itk.org/t/pointset-to-pointset-registration-with-random-sampling/1502>\
**Category:** Engineering\
**Tags:** registration\
**Created:** [January 3, 2019, 5:54pm UTC](https://discourse.itk.org/t/pointset-to-pointset-registration-with-random-sampling/1502 "2019-01-03T17:54:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roulbac](https://discourse.itk.org/user_avatar/discourse.itk.org/roulbac/32/571_2.png) [@Roulbac](https://discourse.itk.org/u/Roulbac)\
**Post date:** [January 3, 2019, 5:54pm UTC](https://discourse.itk.org/t/pointset-to-pointset-registration-with-random-sampling/1502/1 "2019-01-03T17:54:19Z")

</div>

Hi,  
I need to register two point clouds; however my point sets have too many points, so it is quite slow to run.  
I have followed the example from the IterativeClosestPoint.  
A solution I can think of is to randomly sample my point clouds at each iteration, though I don’t think the registration method allows it…  
Any workaround for first randomly sampling my PointSets before registering? I looked around but couldn’t find any. Should I write my own filter? Are there any guidelines/best practices for that?

---

<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:** [January 8, 2019, 11:32pm UTC](https://discourse.itk.org/t/pointset-to-pointset-registration-with-random-sampling/1502/2 "2019-01-08T23:32:16Z")

</div>

Hi @Roulbac,

Yes, random sampling the points is one good approach.

The points in a `PointSet` have an integer index. In Python , you could quickly generate integer indices with `numpy.random.shuffle` and `numpy.arange`.

In C++, you could reuse the `RandomPermutation` code used by `itk:ImageRandomNonRepeatingConstIteratorWithIndex`:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/7b0e4702728e70dcb1f7c3c6a91be2c3480e1c55/Modules/Core/Common/include/itkImageRandomNonRepeatingConstIteratorWithIndex.h#L71-L140>

In either case, create a new point set and populated with points from the complete set using the first _N_ indices.

A bonus challenge is to create a class that encapsulates this operation, and contribute it to the community!

---

<div class="post-metadata">

**Author:** ![Roulbac](https://discourse.itk.org/user_avatar/discourse.itk.org/roulbac/32/571_2.png) [@Roulbac](https://discourse.itk.org/u/Roulbac)\
**Post date:** [January 9, 2019, 5:49pm UTC](https://discourse.itk.org/t/pointset-to-pointset-registration-with-random-sampling/1502/3 "2019-01-09T17:49:13Z")

</div>

Thank you!  
Excited to try and contribute that once I get a bit of a break from school stuff!

---

<div class="post-metadata">

**Author:** ![keyurs19](https://discourse.itk.org/user_avatar/discourse.itk.org/keyurs19/32/1344_2.png) [@keyurs19](https://discourse.itk.org/u/keyurs19)\
**Post date:** [April 10, 2020, 8:16pm UTC](https://discourse.itk.org/t/pointset-to-pointset-registration-with-random-sampling/1502/4 "2020-04-10T20:16:59Z")

</div>

Hello Matt,

Thank you for your post regarding the random sampling on a pointset. Could you please advise how would one uniformly sample the pointsets?

Thank you,  
Keyur Shah

---

<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:** [April 10, 2020, 9:14pm UTC](https://discourse.itk.org/t/pointset-to-pointset-registration-with-random-sampling/1502/5 "2020-04-10T21:14:28Z")

</div>

I think that two options are full sampling and random sampling. Every n-th point does not make much sense for point sets as it does for images.

---

<div class="post-metadata">

**Author:** ![keyurs19](https://discourse.itk.org/user_avatar/discourse.itk.org/keyurs19/32/1344_2.png) [@keyurs19](https://discourse.itk.org/u/keyurs19)\
**Post date:** [April 10, 2020, 9:46pm UTC](https://discourse.itk.org/t/pointset-to-pointset-registration-with-random-sampling/1502/6 "2020-04-10T21:46:16Z")

</div>

Hello Dzenan,

Thank you for your response. Another quick question, would you know how to write a pointset to a file? I could not find an example to do so.

Thank you,  
Keyur Shah

---

<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:** [April 10, 2020, 10:44pm UTC](https://discourse.itk.org/t/pointset-to-pointset-registration-with-random-sampling/1502/7 "2020-04-10T22:44:39Z")

</div>

ITK has no specialized classes for reading and writing point sets.

---

<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:** [April 13, 2020, 7:05pm UTC](https://discourse.itk.org/t/pointset-to-pointset-registration-with-random-sampling/1502/8 "2020-04-13T19:05:49Z")

</div>

For 3D points sets, it is possible to save point sets in mesh file formats like _.OBJ_. In C++, `itk::MeshFileReader` / `itk::MeshFileWriter` can be used. In Python, these are exposed with the more Pythonic `itk.meshread` / `itk.meshwrite` functions in ITK 5.1.
