# How to register a pair of images and their corresponding masks?

**URL:** <https://discourse.itk.org/t/how-to-register-a-pair-of-images-and-their-corresponding-masks/6511>\
**Category:** Beginner Questions\
**Created:** [March 12, 2024, 2:41am UTC](https://discourse.itk.org/t/how-to-register-a-pair-of-images-and-their-corresponding-masks/6511 "2024-03-12T02:41:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vectorzwt](https://discourse.itk.org/user_avatar/discourse.itk.org/vectorzwt/32/3943_2.png) [@vectorzwt](https://discourse.itk.org/u/vectorzwt)\
**Post date:** [March 12, 2024, 2:41am UTC](https://discourse.itk.org/t/how-to-register-a-pair-of-images-and-their-corresponding-masks/6511/1 "2024-03-12T02:41:45Z")

</div>

Hello everyone.I’m new to simpleitk, and I want to register two images (pre and post surgery) and their corresponding masks. And my code is shown:

```auto
def rigid_pre_post(fixed_image_path, moving_image_path, fixed_mask_path, moving_mask_path, out_image_path, out_mask_path):
    # 读取图像
    fixed_image = sitk.ReadImage(fixed_image_path)
    fixed_image = sitk.Cast(fixed_image, sitk.sitkFloat32)
    moving_image = sitk.ReadImage(moving_image_path)
    moving_image = sitk.Cast(moving_image, sitk.sitkFloat32)
    
    # 读取mask
    fixed_mask = sitk.ReadImage(fixed_mask_path)
    moving_mask = sitk.ReadImage(moving_mask_path)

    # 初始化变换
    transform = sitk.CenteredTransformInitializer(fixed_image, moving_image, sitk.Euler3DTransform(),
                                                  sitk.CenteredTransformInitializerFilter.GEOMETRY)
    
    # 设置配准方法
    registration_method = sitk.ImageRegistrationMethod()
    registration_method.SetOptimizerAsGradientDescentLineSearch(learningRate=1.0, numberOfIterations=100,
                                                                convergenceMinimumValue=1e-6, convergenceWindowSize=10)
    registration_method.SetOptimizerScalesFromPhysicalShift()
    registration_method.SetInitialTransform(transform)
    registration_method.SetMetricAsMattesMutualInformation(numberOfHistogramBins=50)
    registration_method.SetMetricSamplingStrategy(registration_method.RANDOM)
    registration_method.SetMetricSamplingPercentage(0.01)
    registration_method.SetInterpolator(sitk.sitkLinear)

    # 执行配准
    final_transform = registration_method.Execute(fixed=fixed_image, moving=moving_image)

    # 变换移动图像
    moving_resampled = sitk.Resample(moving_image, fixed_image, final_transform, sitk.sitkLinear, 0.0, moving_image.GetPixelID())
    sitk.WriteImage(moving_resampled, out_image_path)
    
    # 变换移动mask（使用最近邻插值以保持二值性质）
    moving_mask_resampled = sitk.Resample(moving_mask, fixed_image, final_transform, sitk.sitkNearestNeighbor, 0.0, moving_mask.GetPixelID())
    sitk.WriteImage(moving_mask_resampled, out_mask_path)

```

In most cases, it works. However, some cases my registered mask is all zero! Can you give me some suggestions? Thank you!

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [March 12, 2024, 12:59pm UTC](https://discourse.itk.org/t/how-to-register-a-pair-of-images-and-their-corresponding-masks/6511/2 "2024-03-12T12:59:41Z")

</div>

Hello @vectorzwt,

If the `moving_mask_resampled` is all zeros then likely the registration failed. How does the `moving_resampled` image look? Likely that isn’t as expected either.

To allow us to help you please share the resulting transformation and reason for optimizer termination?

```auto
print(f"Final metric value: {registration_method.GetMetricValue()}")
print(
f"Optimizer's stopping condition, {registration_method.GetOptimizerStopConditionDescription()}"
)
print(f"Final transformation: {final_transform}")

```

Most often the registration failure is due to poor initialization. Try resampling the moving image and mask after the transform returned by the `CenteredTransformInitializer` if the results are far from the fixed image and mask then likely you will need to look at a more robust initialization approach (see [this notebook](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/63_Registration_Initialization.ipynb) for details).

Finally, minor comment on the code, you can combine the image reading and casting:

```auto
fixed_image = sitk.ReadImage(fixed_image_path, sitk.sitkFloat32)
moving_image = sitk.ReadImage(moving_image_path, sitk.sitkFloat32)

```

---

<div class="post-metadata">

**Author:** ![vectorzwt](https://discourse.itk.org/user_avatar/discourse.itk.org/vectorzwt/32/3943_2.png) [@vectorzwt](https://discourse.itk.org/u/vectorzwt)\
**Post date:** [March 12, 2024, 1:33pm UTC](https://discourse.itk.org/t/how-to-register-a-pair-of-images-and-their-corresponding-masks/6511/3 "2024-03-12T13:33:52Z")

</div>

Thank you for replying.

> [@zivy](#):
>
> How does the `moving_resampled` image look?

I have checked the `moving_resampled` image, it looks quite normal.

> [@zivy](#):
>
> To allow us to help you please share the resulting transformation and reason for optimizer termination?

I get the results as below:

 ![image](https://discourse.itk.org/uploads/default/original/2X/6/625bdb7776e12439c4004f040fe7ec098a1921f9.png)  
Can you give me some advice? Thank you!

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [March 12, 2024, 4:02pm UTC](https://discourse.itk.org/t/how-to-register-a-pair-of-images-and-their-corresponding-masks/6511/4 "2024-03-12T16:02:22Z")

</div>

Hello @vectorzwt,

The results seem reasonable and the optimizer did some work, 33 iterations, so doesn’t look like early termination due to really bad initialization.

Hard to guess why the `moving_mask_resampled` is not as expected (the resampling code looks correct and uses nearest neighbor interpolation). Just to be on the safe side, please confirm that the `moving_mask` had non-zero values after it was read. If the input was as expected, then it is hard to guess what is going on.

Possibly share the anonymized data?

---

<div class="post-metadata">

**Author:** ![vectorzwt](https://discourse.itk.org/user_avatar/discourse.itk.org/vectorzwt/32/3943_2.png) [@vectorzwt](https://discourse.itk.org/u/vectorzwt)\
**Post date:** [March 13, 2024, 5:06am UTC](https://discourse.itk.org/t/how-to-register-a-pair-of-images-and-their-corresponding-masks/6511/5 "2024-03-13T05:06:17Z")

</div>

> [@zivy](#):
>
> Just to be on the safe side, please confirm that the `moving_mask` had non-zero values after it was read.

I have checked it, and it seems not to be all zero. I can upload the data.  
[mri nii file](https://drive.google.com/file/d/1gwoX_pf48n5O90awlegZlQv7JOE6JJxD/view?usp=sharing)  
Thank you very much!

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [March 13, 2024, 5:27pm UTC](https://discourse.itk.org/t/how-to-register-a-pair-of-images-and-their-corresponding-masks/6511/6 "2024-03-13T17:27:12Z")

</div>

Hello @vectorzwt,

From a quick look at the provided data the fixed image and fixed mask don’t seem to occupy the same spatial region. Their size, spacing and direction cosine matrices look similar but the origins are significantly different. Any idea what is causing this?

---

<div class="post-metadata">

**Author:** ![vectorzwt](https://discourse.itk.org/user_avatar/discourse.itk.org/vectorzwt/32/3943_2.png) [@vectorzwt](https://discourse.itk.org/u/vectorzwt)\
**Post date:** [March 14, 2024, 7:15am UTC](https://discourse.itk.org/t/how-to-register-a-pair-of-images-and-their-corresponding-masks/6511/7 "2024-03-14T07:15:08Z")

</div>

> [@zivy](#):
>
> Their size, spacing and direction cosine matrices look similar but the origins are significantly different.

Oh, I think it causes my problem.I do not change the original data. I will contact the doctor for more information. Thank you very much!
