# registration is too slow and changing sample rate seems not working

**URL:** https://discourse.itk.org/t/registration-is-too-slow-and-changing-sample-rate-seems-not-working/3356
**Category:** Beginner Questions
**Tags:** registration
**Created:** [August 3, 2020, 8:31am UTC](https://discourse.itk.org/t/registration-is-too-slow-and-changing-sample-rate-seems-not-working/3356 "2020-08-03T08:31:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![aweib](https://discourse.itk.org/user_avatar/discourse.itk.org/aweib/32/1655_2.png) [@aweib](https://discourse.itk.org/u/aweib)
#### Post date: [August 3, 2020, 8:31am UTC](https://discourse.itk.org/t/registration-is-too-slow-and-changing-sample-rate-seems-not-working/3356/1 "2020-08-03T08:31:25Z")

</div>

Hello everyone,  
I’m using ITK for 3D brain image registration, but the registration is too slow. I try to change the sample rate but it seems not working, reducing sample rate does not affect the registration speed. It really confused me and I’m trying to get some help.  
Here is part of the code

```
using ImageType = itk::Image<float, K_DIM>;
using TransformType = itk::VersorRigid3DTransform<double>;
using MetricType = itk::MattesMutualInformationImageToImageMetricv4<ImageType, ImageType>;
using OptimizerType = itk::RegularStepGradientDescentOptimizerv4<double>;
using RegistrationType = itk::ImageRegistrationMethodv4<ImageType, ImageType, TransformType>;

RegistrationType::Pointer registration = RegistrationType::New();
registration->SetFixedImage(fixed);
registration->SetMovingImage(moving);
MetricType::Pointer metric = MetricType::New();
registration->SetMetric(metric);
OptimizerType::Pointer optimizer = OptimizerType::New();
registration->SetOptimizer(optimizer);

metric->SetNumberOfHistogramBins(histogram_bins);
metric->SetUseMovingImageGradientFilter(false);
metric->SetUseFixedImageGradientFilter(false);

if (sample_rate < 1) {
    registration->SetMetricSamplingStrategy(RegistrationType::RANDOM);
    registration->SetMetricSamplingPercentage(sample_rate);
    registration->MetricSamplingReinitializeSeed(42);
}
else {
    metric->SetUseSampledPointSet(false);
}
```

---

<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: [August 3, 2020, 1:59pm UTC](https://discourse.itk.org/t/registration-is-too-slow-and-changing-sample-rate-seems-not-working/3356/2 "2020-08-03T13:59:51Z")

</div>

Make sure you are compiling in release mode. Debug is usually many times slower. But changing the sampling percentage should have made some difference in the speed. Have you tried setting `sample_rate` to 0.1, 0.01 or 0.001?

---

<div class="post-metadata">

### Author: ![aweib](https://discourse.itk.org/user_avatar/discourse.itk.org/aweib/32/1655_2.png) [@aweib](https://discourse.itk.org/u/aweib)
#### Post date: [August 4, 2020, 1:55am UTC](https://discourse.itk.org/t/registration-is-too-slow-and-changing-sample-rate-seems-not-working/3356/3 "2020-08-04T01:55:37Z")

</div>

Hi dzenanz, thank you for your help. I’v found the error, calling SetNumberOfLevels will reset the sampling percentage to 1, after rearrange the code, problem solved.
