如何保存ndarray或显示动画



我正在使用TIGRE(CERN提供的一个用于重建的开源工具箱(。最初的代码是为python 3.7编写的,目前正在努力在3.9上正确运行。

这是原始代码及其产生的错误。

#%% Initialize
import tigre
import numpy as np
from tigre.utilities import sample_loader
from tigre.utilities import CTnoise
import tigre.algorithms as algs
#%% Geometry
geo = tigre.geometry_default(high_resolution=False)
#%% Load data and generate projections
# define angles
angles = np.linspace(0, 2 * np.pi, 100)
# Load thorax phatom data
head = sample_loader.load_head_phantom(geo.nVoxel)
# generate projections
projections = tigre.Ax(head, geo, angles)
# add noise
noise_projections = CTnoise.add(projections, Poisson=1e5, Gaussian=np.array([0, 10]))
#%% Reconstruct image using OS-SART and FDK
# FDK
imgFDK = algs.fdk(noise_projections, geo, angles)
# OS-SART
niter = 50
imgOSSART = algs.ossart(noise_projections, geo, angles, niter)
imgOSSART.save
#%% Show the results
tigre.plotimg(np.concatenate([imgFDK, imgOSSART], axis=1), dim="z")

错误:

File ~TIGREPythondemosd04_SimpleReconstruction.py:54 in <module>
imgOSSART.save
AttributeError: 'numpy.ndarray' object has no attribute 'save'

当我删除imgOSSART.save时,程序运行,但返回一个空的数字:

<Figure size 432x288 with 0 Axes>
C:ProgramDataAnaconda3libsite-packagesmatplotlibanimation.py:889: UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g. `anim`, that exists until you have outputted the Animation using `plt.show()` or `anim.save()`.
warnings.warn(

有人知道解决这个问题或获得python 3.7的方法吗?

在numpy中,ndarrays使用numpy.save函数保存。所以,试试这样的东西,而不是

np.save('imgOSSART.npy', imgOSSART)

然而,我认为这并不能解决问题。


代码片段似乎来自这里,其中不包括np.save

要将动画保存为gif,可以尝试添加savegif参数

tigre.plotimg(
np.concatenate([imgFDK, imgOSSART], axis=1), 
dim="z", 
savegif='filename.gif'
)

最新更新