forked from dmpelt/pyastratoolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths015_fp_bp.py
86 lines (70 loc) · 3.22 KB
/
s015_fp_bp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#-----------------------------------------------------------------------
#Copyright 2013 Centrum Wiskunde & Informatica, Amsterdam
#
#Author: Daniel M. Pelt
#Contact: [email protected]
#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/>.
#
#-----------------------------------------------------------------------
# This example demonstrates using the FP and BP primitives with Matlab's lsqr
# solver. Calls to FP (astra_create_sino_cuda) and
# BP (astra_create_backprojection_cuda) are wrapped in a function astra_wrap,
# and a handle to this function is passed to lsqr.
# Because in this case the inputs/outputs of FP and BP have to be vectors
# instead of images (matrices), the calls require reshaping to and from vectors.
import astra
import numpy as np
# FP/BP wrapper class
class astra_wrap(object):
def __init__(self,proj_geom,vol_geom):
self.proj_id = astra.create_projector('line',proj_geom,vol_geom)
self.shape = (proj_geom['DetectorCount']*len(proj_geom['ProjectionAngles']),vol_geom['GridColCount']*vol_geom['GridRowCount'])
self.dtype = np.float
def matvec(self,v):
sid, s = astra.create_sino(np.reshape(v,(vol_geom['GridRowCount'],vol_geom['GridColCount'])),self.proj_id,useCUDA=True)
astra.data2d.delete(sid)
return s.flatten()
def rmatvec(self,v):
bid, b = astra.create_backprojection(np.reshape(v,(len(proj_geom['ProjectionAngles']),proj_geom['DetectorCount'],)),self.proj_id,useCUDA=True)
astra.data2d.delete(bid)
return b.flatten()
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))
# Create a 256x256 phantom image
import scipy.io
P = scipy.io.loadmat('phantom.mat')['phantom256']
# Create a sinogram using the GPU.
proj_id = astra.create_projector('line',proj_geom,vol_geom)
sinogram_id, sinogram = astra.create_sino(P, proj_id,useCUDA=True)
# Reshape the sinogram into a vector
b = sinogram.flatten()
# Call lsqr with ASTRA FP and BP
import scipy.sparse.linalg
wrapper = astra_wrap(proj_geom,vol_geom)
result = scipy.sparse.linalg.lsqr(wrapper,b,atol=1e-4,btol=1e-4,iter_lim=25)
# Reshape the result into an image
Y = np.reshape(result[0],(vol_geom['GridRowCount'], vol_geom['GridColCount']));
import pylab
pylab.gray()
pylab.imshow(Y)
pylab.show()
astra.data2d.delete(sinogram_id)
astra.projector.delete(proj_id)
astra.projector.delete(wrapper.proj_id)