用于图像配准的 SimpleITK。写作问题
SimpleITK for image registration. Writing issue
我已经在 Python3 上从源代码安装了 SimpleITK 包。当我执行提供的注册示例时:
#!/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" )
使用 Python ImageRegistrationMethod1.py image_ref.tif
image_moving.tif res.tif
执行似乎运行良好,直到写入 res.tif
图像并触发以下错误:
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.
我真的不知道为什么我会收到这个错误,因为代码是从源代码构建的。有什么帮助吗?
ImageRegistrationMethod1 示例 运行 的结果是一个转换。 SimpleITK 支持多种文件格式进行转换,包括文本文件 (.txt)、Matlab 文件 (.mat) 和 HDF5Tranform (.hdf5)。这不包括 .tif 文件,它是图像文件,而不是转换文件。
您可以在 SimpleITK IO 页面的转换部分阅读更多相关信息:
https://simpleitk.readthedocs.io/en/master/IO.html#transformations
在此示例中,生成的变换是 3 维平移。因此,如果您选择了 .txt 文件输出,您可以在参数行中看到 X、Y 和 Z 翻译。
如果你想把dicom写成文件输出,请试试这个。
writer.SetFileName(os.path.join('transformed.dcm'))
writer.Execute(cimg)
我已经在 Python3 上从源代码安装了 SimpleITK 包。当我执行提供的注册示例时:
#!/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" )
使用 Python ImageRegistrationMethod1.py image_ref.tif
image_moving.tif res.tif
执行似乎运行良好,直到写入 res.tif
图像并触发以下错误:
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.
我真的不知道为什么我会收到这个错误,因为代码是从源代码构建的。有什么帮助吗?
ImageRegistrationMethod1 示例 运行 的结果是一个转换。 SimpleITK 支持多种文件格式进行转换,包括文本文件 (.txt)、Matlab 文件 (.mat) 和 HDF5Tranform (.hdf5)。这不包括 .tif 文件,它是图像文件,而不是转换文件。
您可以在 SimpleITK IO 页面的转换部分阅读更多相关信息:
https://simpleitk.readthedocs.io/en/master/IO.html#transformations
在此示例中,生成的变换是 3 维平移。因此,如果您选择了 .txt 文件输出,您可以在参数行中看到 X、Y 和 Z 翻译。
如果你想把dicom写成文件输出,请试试这个。
writer.SetFileName(os.path.join('transformed.dcm'))
writer.Execute(cimg)