diff options
Diffstat (limited to 'samples')
| -rw-r--r-- | samples/python/s018_experimental_multires.py | 84 | ||||
| -rw-r--r-- | samples/python/s018_plugin.py | 132 | 
2 files changed, 216 insertions, 0 deletions
| diff --git a/samples/python/s018_experimental_multires.py b/samples/python/s018_experimental_multires.py new file mode 100644 index 0000000..cf38e53 --- /dev/null +++ b/samples/python/s018_experimental_multires.py @@ -0,0 +1,84 @@ +#----------------------------------------------------------------------- +#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- + +import astra +import numpy as np +from astra.experimental import do_composite_FP + +astra.log.setOutputScreen(astra.log.STDERR, astra.log.DEBUG) + +# low res part (voxels of 4x4x4) +vol_geom1 = astra.create_vol_geom(32, 16, 32, -64, 0, -64, 64, -64, 64) + +# high res part (voxels of 1x1x1) +vol_geom2 = astra.create_vol_geom(128, 64, 128, 0, 64, -64, 64, -64, 64) + + +# Split the output in two parts as well, for demonstration purposes +angles1 = np.linspace(0, np.pi/2, 90, False) +angles2 = np.linspace(np.pi/2, np.pi, 90, False) +proj_geom1 = astra.create_proj_geom('parallel3d', 1.0, 1.0, 128, 192, angles1) +proj_geom2 = astra.create_proj_geom('parallel3d', 1.0, 1.0, 128, 192, angles2) + +# Create a simple hollow cube phantom +cube1 = np.zeros((32,32,16)) +cube1[4:28,4:28,4:16] = 1 + +cube2 = np.zeros((128,128,64)) +cube2[16:112,16:112,0:112] = 1 +cube2[33:97,33:97,4:28] = 0 + +vol1 = astra.data3d.create('-vol', vol_geom1, cube1) +vol2 = astra.data3d.create('-vol', vol_geom2, cube2) + +proj1 = astra.data3d.create('-proj3d', proj_geom1, 0) +proj2 = astra.data3d.create('-proj3d', proj_geom2, 0) + +# The actual geometries don't matter for this composite FP/BP case +projector = astra.create_projector('cuda3d', proj_geom1, vol_geom1) + +do_composite_FP(projector, [vol1, vol2], [proj1, proj2]) + +proj_data1 = astra.data3d.get(proj1) +proj_data2 = astra.data3d.get(proj2) + +# Display a single projection image +import pylab +pylab.gray() +pylab.figure(1) +pylab.imshow(proj_data1[:,0,:]) +pylab.figure(2) +pylab.imshow(proj_data2[:,0,:]) +pylab.show() + + +# Clean up. Note that GPU memory is tied up in the algorithm object, +# and main RAM in the data objects. +astra.data3d.delete(vol1) +astra.data3d.delete(vol2) +astra.data3d.delete(proj1) +astra.data3d.delete(proj2) +astra.projector3d.delete(projector) diff --git a/samples/python/s018_plugin.py b/samples/python/s018_plugin.py new file mode 100644 index 0000000..31cca95 --- /dev/null +++ b/samples/python/s018_plugin.py @@ -0,0 +1,132 @@ +#----------------------------------------------------------------------- +#Copyright 2015 Centrum Wiskunde & Informatica, Amsterdam +# +#Author: Daniel M. Pelt +#Contact: D.M.Pelt@cwi.nl +#Website: http://dmpelt.github.io/pyastratoolbox/ +# +# +#This file is part of the Python interface to the +#All Scale Tomographic Reconstruction Antwerp Toolbox ("ASTRA Toolbox"). +# +#The Python interface to the ASTRA Toolbox is free software: you can redistribute it and/or modify +#it under the terms of the GNU General Public License as published by +#the Free Software Foundation, either version 3 of the License, or +#(at your option) any later version. +# +#The Python interface to the ASTRA Toolbox is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU General Public License for more details. +# +#You should have received a copy of the GNU General Public License +#along with the Python interface to the ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>. +# +#----------------------------------------------------------------------- + +import astra +import numpy as np +import six + +# Define the plugin class (has to subclass astra.plugin.base) +# Note that usually, these will be defined in a separate package/module +class SIRTPlugin(astra.plugin.base): +    """Example of an ASTRA plugin class, implementing a simple 2D SIRT algorithm. + +    Options: + +    'rel_factor': relaxation factor (optional) +    """ + +    # The astra_name variable defines the name to use to +    # call the plugin from ASTRA +    astra_name = "SIRT-PLUGIN" + +    def initialize(self,cfg, rel_factor = 1): +        self.W = astra.OpTomo(cfg['ProjectorId']) +        self.vid = cfg['ReconstructionDataId'] +        self.sid = cfg['ProjectionDataId'] +        self.rel = rel_factor + +    def run(self, its): +        v = astra.data2d.get_shared(self.vid) +        s = astra.data2d.get_shared(self.sid) +        W = self.W +        for i in range(its): +            v[:] += self.rel*(W.T*(s - (W*v).reshape(s.shape))).reshape(v.shape)/s.size + +if __name__=='__main__': + +    vol_geom = astra.create_vol_geom(256, 256) +    proj_geom = astra.create_proj_geom('parallel', 1.0, 384, np.linspace(0,np.pi,180,False)) + +    # As before, create a sinogram from a phantom +    import scipy.io +    P = scipy.io.loadmat('phantom.mat')['phantom256'] +    proj_id = astra.create_projector('cuda',proj_geom,vol_geom) + +    # construct the OpTomo object +    W = astra.OpTomo(proj_id) + +    sinogram = W * P +    sinogram = sinogram.reshape([180, 384]) + +    # Register the plugin with ASTRA +    # First we import the package that contains the plugin +    import s018_plugin +    # Then, we register the plugin class with ASTRA +    astra.plugin.register(s018_plugin.SIRTPlugin) + +    # Get a list of registered plugins +    six.print_(astra.plugin.get_registered()) + +    # To get help on a registered plugin, use get_help +    six.print_(astra.plugin.get_help('SIRT-PLUGIN')) + +    # Create data structures +    sid = astra.data2d.create('-sino', proj_geom, sinogram) +    vid = astra.data2d.create('-vol', vol_geom) + +    # Create config using plugin name +    cfg = astra.astra_dict('SIRT-PLUGIN') +    cfg['ProjectorId'] = proj_id +    cfg['ProjectionDataId'] = sid +    cfg['ReconstructionDataId'] = vid + +    # Create algorithm object +    alg_id = astra.algorithm.create(cfg) + +    # Run algorithm for 100 iterations +    astra.algorithm.run(alg_id, 100) + +    # Get reconstruction +    rec = astra.data2d.get(vid) + +    # Options for the plugin go in cfg['option'] +    cfg = astra.astra_dict('SIRT-PLUGIN') +    cfg['ProjectorId'] = proj_id +    cfg['ProjectionDataId'] = sid +    cfg['ReconstructionDataId'] = vid +    cfg['option'] = {} +    cfg['option']['rel_factor'] = 1.5 +    alg_id_rel = astra.algorithm.create(cfg) +    astra.algorithm.run(alg_id_rel, 100) +    rec_rel = astra.data2d.get(vid) + +    # We can also use OpTomo to call the plugin +    rec_op = W.reconstruct('SIRT-PLUGIN', sinogram, 100, extraOptions={'rel_factor':1.5}) + +    import pylab as pl +    pl.gray() +    pl.figure(1) +    pl.imshow(rec,vmin=0,vmax=1) +    pl.figure(2) +    pl.imshow(rec_rel,vmin=0,vmax=1) +    pl.figure(3) +    pl.imshow(rec_op,vmin=0,vmax=1) +    pl.show() + +    # Clean up. +    astra.projector.delete(proj_id) +    astra.algorithm.delete([alg_id, alg_id_rel]) +    astra.data2d.delete([vid, sid]) | 
