Zero-valued spacing is not supported and may result in undefined behavior.

Hi,
I am trying to run a python registration package and I get an error
This is the code that I run:

irm.SetMetricAsMattesMutualInformation(numberOfHistogramBins=256)
irm.SetMetricFixedMask(fix_mask)
irm.SetMetricMovingMask(mov_mask)

print('images spacing')
print(fix.GetSpacing())
print(mov.GetSpacing())
print(fix_mask.GetSpacing())
print(mov_mask.GetSpacing())
print('images dims')
print(fix.GetSize())
print(mov.GetSize())
print(fix_mask.GetSize())
print(mov_mask.GetSize())

initial_metric_value = irm.MetricEvaluate(fix, mov)
print('A')
irm.Execute(fix, mov)
print('B')
final_metric_value = irm.MetricEvaluate(fix, mov)
print('B')

and this is the output/error that I get:

images spacing
(9.0, 1.8181851239729527, 1.8181851239729527)
(9.0, 1.8181851239729527, 1.8181851239729527)
(9.0, 1.8181851239729527, 1.8181851239729527)
(9.0, 1.8181851239729527, 1.8181851239729527)
images dims
(15, 149, 129)
(15, 149, 129)
(15, 149, 129)
(15, 149, 129)
A
 Exception thrown in SimpleITK ImageRegistrationMethod_MetricEvaluate: /tmp/SimpleITK-build/ITK-prefix/include/ITK-5.3/itkImageBase.hxx:77:
ITK ERROR: Image(0x7fdb404b7fb0): Zero-valued spacing is not supported and may result in undefined behavior.
Refusing to change spacing from [1, 1] to [0, 0.797619]

The spacing is not zero, so I don’t get why I am getting this error… I tried to change the numberOfHistogramBins, and it did change the spacing, but only for the 2nd value, the first stayed 0.
Also, the spacing is 3D why do I get an error of 2D spacing?
I probably missing something any help will be useful.
Thanks,
Oren

Hello @orena1,

The numberOfHistogramBins parameter is not the cause of the problem. I suspect there is something going on with the data, images or masks, which is causing the problem and possibly leading to an erroneous error message. From the information you provided the only thing that seems a bit unusual is that the 3D image samples are sparse along the x-axis and dense along the y and z axes, though that should not cause any problem in ITK/SimpleITK.

Was unable to reproduce the issue using the following code and this data training_001_ct.mha, training_001_mr_T1.mha.

Please provide a minimal working example and data so that we can reproduce the problem and help you resolve it.

import SimpleITK as sitk

fix = sitk.ReadImage("training_001_ct.mha", sitk.sitkFloat32)
mov = sitk.ReadImage("training_001_mr_T1.mha", sitk.sitkFloat32)
fix_mask = fix>0
mov_mask = mov>0

irm = sitk.ImageRegistrationMethod()

irm.SetMetricAsMattesMutualInformation(numberOfHistogramBins=256)
irm.SetMetricFixedMask(fix_mask)
irm.SetMetricMovingMask(mov_mask)

print('images spacing')
print(fix.GetSpacing())
print(mov.GetSpacing())
print(fix_mask.GetSpacing())
print(mov_mask.GetSpacing())
print('images dims')
print(fix.GetSize())
print(mov.GetSize())
print(fix_mask.GetSize())
print(mov_mask.GetSize())

initial_metric_value = irm.MetricEvaluate(fix, mov)
print('A')
irm.Execute(fix, mov)
print('B')
final_metric_value = irm.MetricEvaluate(fix, mov)
print('B')
2 Likes

Thanks a lot @zivy ,
Here are the fails, the code fails also when using your mask.
e.g.

fix = sitk.ReadImage("debug_fix.mha", sitk.sitkFloat32)
mov = sitk.ReadImage("debug_mov.mha", sitk.sitkFloat32)

fix_mask = fix>0
mov_mask = mov>0

debug_fix.mha (1.1 MB)
debug_fix_mask.mha (281.9 KB)
debug_mov.mha (1.1 MB)
debug_mov_mask.mha (281.9 KB)

Thanks a lot!

Hello @orena1,

  1. There is a problem with your fixed image, it is empty (all zeros) so there is likely some bug in your code processing that image prior to registration:
import SimpleITK as sitk

fixed_image = sitk.ReadImage("debug_fix.mha")
arr_view = sitk.GetArrayViewFromImage(fixed_image)
print(f"[min,max] = [{arr_view.min()}, {arr_view.max()}]")
  1. The error message is misleading, opened an issue on GitHub.

Thanks for your reply, @zivy

I know the image is all zeros, it is a top left subtile of a larger image for registration, it is all zeros as it is out of the FoV of the moving image:

I would expect that in this case it would just return a zero of inf or something like that? no?
I assume if I just add some very small noise to these ares it will be ok?
Any thoughts?

Thanks

This is a bug in ITK, issue created on GitHub.

Great, thank you very much