首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SimpleITK用于图像配准。写作问题

SimpleITK用于图像配准。写作问题
EN

Stack Overflow用户
提问于 2020-06-01 22:56:49
回答 2查看 1.1K关注 0票数 1

我已经从源代码中安装了SimpleITK包在Python3上。当我执行提供的注册示例时:

代码语言:javascript
复制
#!/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的执行似乎运行良好:

代码语言:javascript
复制
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.

我真的不知道为什么我会收到这个错误,因为代码是从源代码构建的。有什么帮助吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-04 15:38:54

运行ImageRegistrationMethod1示例的结果是一个转换。SimpleITK支持许多用于转换的文件格式,包括文本文件(.txt)、Matlab文件(.mat)和HDF5Tranform (.hdf5)。这不包括.tif文件,它是图像文件,而不是转换文件。

您可以在SimpleITK IO页面的Transform部分阅读更多有关它的信息:

https://simpleitk.readthedocs.io/en/master/IO.html#transformations

在这个例子中,产生的转换是一个三维翻译.因此,如果您选择了一个.txt文件输出,您可以看到参数行上的X、Y和Z翻译。

票数 1
EN

Stack Overflow用户

发布于 2021-03-18 01:30:20

如果你想把dicom写成文件输出,请试试这个。

代码语言:javascript
复制
writer.SetFileName(os.path.join('transformed.dcm'))
writer.Execute(cimg)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62142223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档