I’ve installed from sources the SimpleITK package on Python3. When I perform the provided registration example :
#!/usr/bin/env python #========================================================================= # # Copyright NumFOCUS # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0.txt # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # #========================================================================= from __future__ import print_function import SimpleITK as sitk import sys import os def command_iteration(method) : print("{0:3} = {1:10.5f} : {2}".format(method.GetOptimizerIteration(), method.GetMetricValue(), method.GetOptimizerPosition())) if len ( sys.argv ) < 4: print( "Usage: {0} <fixedImageFilter> <movingImageFile> <outputTransformFile>".format(sys.argv[0])) sys.exit ( 1 ) fixed = sitk.ReadImage(sys.argv[1], sitk.sitkFloat32) moving = sitk.ReadImage(sys.argv[2], sitk.sitkFloat32) R = sitk.ImageRegistrationMethod() R.SetMetricAsMeanSquares() R.SetOptimizerAsRegularStepGradientDescent(4.0, .01, 200 ) R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension())) R.SetInterpolator(sitk.sitkLinear) R.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(R) ) outTx = R.Execute(fixed, moving) print("-------") print(outTx) print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription())) print(" Iteration: {0}".format(R.GetOptimizerIteration())) print(" Metric value: {0}".format(R.GetMetricValue())) sitk.WriteTransform(outTx, sys.argv[3]) if ( not "SITK_NOSHOW" in os.environ ): resampler = sitk.ResampleImageFilter() resampler.SetReferenceImage(fixed); resampler.SetInterpolator(sitk.sitkLinear) resampler.SetDefaultPixelValue(100) resampler.SetTransform(outTx) out = resampler.Execute(moving) simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8) simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8) cimg = sitk.Compose(simg1, simg2, simg1//2.+simg2//2.) sitk.Show( cimg, "ImageRegistration1 Composition" )
The execution with Python ImageRegistrationMethod1.py image_ref.tif
image_moving.tif res.tif
seems to work well until it comes to write the res.tif
image and triggers the following error :
TIFFReadDirectory: Warning, Unknown field with tag 50838 (0xc696) encountered. TIFFReadDirectory: Warning, Unknown field with tag 50839 (0xc697) encountered. TIFFReadDirectory: Warning, Unknown field with tag 50838 (0xc696) encountered. TIFFReadDirectory: Warning, Unknown field with tag 50839 (0xc697) encountered. ------- itk::simple::Transform TranslationTransform (0x7fb2a54ec0f0) RTTI typeinfo: itk::TranslationTransform<double, 3u> Reference Count: 2 Modified Time: 2196 Debug: Off Object Name: Observers: none Offset: [14.774, 10.57, 18.0612] Optimizer stop condition: RegularStepGradientDescentOptimizerv4: Step too small after 22 iterations. Current step (0.0078125) is less than minimum step (0.01). Iteration: 23 Metric value: 2631.5128202930223 Traceback (most recent call last): File "ImageRegistrationMethod1.py", line 57, in <module> sitk.WriteTransform(outTx, sys.argv[3]) File "/usr/local/lib/python3.7/site- packages/SimpleITK/SimpleITK.py", line 5298, in WriteTransform return _SimpleITK.WriteTransform(transform, filename) RuntimeError: Exception thrown in SimpleITK WriteTransform: itk::ERROR: TransformFileWriterTemplate(0x7faa4fcfce70): Could not create Transform IO object for writing file /Users/anass/Desktop/res.tif Tried to create one of the following: HDF5TransformIOTemplate HDF5TransformIOTemplate MatlabTransformIOTemplate MatlabTransformIOTemplate TxtTransformIOTemplate TxtTransformIOTemplate You probably failed to set a file suffix, or set the suffix to an unsupported type.
I really don’t know why I’m getting this error since the code was built from source. Any help please?
Answer
The result of running the ImageRegistrationMethod1 example is a transform. SimpleITK supports a number of file formats for transformations, including a text file (.txt), a Matlab file (.mat) and a HDF5Tranform (.hdf5). That does not include a .tif file, which is an image file, not a transform.
You can read more about it on the Transform section of the SimpleITK IO page:
https://simpleitk.readthedocs.io/en/master/IO.html#transformations
In the case of this example, the transform produced is a 3-d translation. So if you’ve chosen a .txt file output, you can see the X, Y and Z translations on the Parameters line.