summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWillem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl>2018-07-17 16:54:13 +0200
committerWillem Jan Palenstijn <Willem.Jan.Palenstijn@cwi.nl>2018-07-17 16:54:13 +0200
commit4d741fc8e6c7930f7a8e27f54c55e0ad4949ed07 (patch)
treee78ac8d69f659b7c9c59e121f7dfb9cba8e5004f
parent9bce55d46758bc79ef2504f68cda6e79c81f4cba (diff)
downloadastra-4d741fc8e6c7930f7a8e27f54c55e0ad4949ed07.tar.gz
astra-4d741fc8e6c7930f7a8e27f54c55e0ad4949ed07.tar.bz2
astra-4d741fc8e6c7930f7a8e27f54c55e0ad4949ed07.tar.xz
astra-4d741fc8e6c7930f7a8e27f54c55e0ad4949ed07.zip
Add sample scripts
-rw-r--r--samples/matlab/s023_FBP_filters.m96
-rw-r--r--samples/python/s023_FBP_filters.py116
2 files changed, 212 insertions, 0 deletions
diff --git a/samples/matlab/s023_FBP_filters.m b/samples/matlab/s023_FBP_filters.m
new file mode 100644
index 0000000..4abec7e
--- /dev/null
+++ b/samples/matlab/s023_FBP_filters.m
@@ -0,0 +1,96 @@
+% -----------------------------------------------------------------------
+% This file is part of the ASTRA Toolbox
+%
+% Copyright: 2010-2018, imec Vision Lab, University of Antwerp
+% 2014-2018, CWI, Amsterdam
+% License: Open Source under GPLv3
+% Contact: astra@astra-toolbox.com
+% Website: http://www.astra-toolbox.com/
+% -----------------------------------------------------------------------
+
+
+% This sample script illustrates three ways of passing filters to FBP.
+% They work with both the FBP (CPU) and the FBP_CUDA (GPU) algorithms.
+
+N = 256;
+
+vol_geom = astra_create_vol_geom(N, N);
+proj_geom = astra_create_proj_geom('parallel', 1.0, N, linspace2(0,pi,180));
+
+proj_id = astra_create_projector('strip', proj_geom, vol_geom);
+
+P = phantom(256);
+
+[sinogram_id, sinogram] = astra_create_sino(P, proj_id);
+
+rec_id = astra_mex_data2d('create', '-vol', vol_geom);
+
+cfg = astra_struct('FBP');
+cfg.ReconstructionDataId = rec_id;
+cfg.ProjectionDataId = sinogram_id;
+cfg.ProjectorId = proj_id;
+
+
+% 1. Use a standard Ram-Lak filter
+cfg.FilterType = 'ram-lak';
+
+alg_id = astra_mex_algorithm('create', cfg);
+astra_mex_algorithm('run', alg_id);
+rec_RL = astra_mex_data2d('get', rec_id);
+astra_mex_algorithm('delete', alg_id);
+
+
+% 2. Define a filter in Fourier space
+% This is assumed to be symmetric, and ASTRA therefore expects only half
+
+% The full filter size should be the smallest power of two that is at least
+% twice the number of detector pixels.
+fullFilterSize = 2*N;
+kernel = [linspace2(0, 1, floor(fullFilterSize / 2)) linspace2(1, 0, ceil(fullFilterSize / 2))];
+halfFilterSize = floor(fullFilterSize / 2) + 1;
+filter = kernel(1:halfFilterSize);
+
+filter_geom = astra_create_proj_geom('parallel', 1.0, halfFilterSize, [0]);
+filter_id = astra_mex_data2d('create', '-sino', filter_geom, filter);
+
+cfg.FilterType = 'projection';
+cfg.FilterSinogramId = filter_id;
+
+alg_id = astra_mex_algorithm('create', cfg);
+astra_mex_algorithm('run', alg_id);
+rec_filter = astra_mex_data2d('get', rec_id);
+astra_mex_algorithm('delete', alg_id);
+
+% 3. Define a (spatial) convolution kernel directly
+% For a kernel of odd size 2*k+1, the central component is at kernel(k+1)
+% For a kernel of even size 2*k, the central component is at kernel(k+1)
+
+kernel = zeros(1, N);
+for i = 0:floor(N/4)-1
+ f = pi * (2*i + 1);
+ val = -2.0 / (f * f);
+ kernel(floor(N/2) + 1 + (2*i+1)) = val;
+ kernel(floor(N/2) + 1 - (2*i+1)) = val;
+end
+kernel(floor(N/2)+1) = 0.5;
+
+kernel_geom = astra_create_proj_geom('parallel', 1.0, N, [0]);
+kernel_id = astra_mex_data2d('create', '-sino', kernel_geom, kernel);
+
+cfg.FilterType = 'rprojection';
+cfg.FilterSinogramId = kernel_id;
+
+alg_id = astra_mex_algorithm('create', cfg);
+astra_mex_algorithm('run', alg_id);
+rec_kernel = astra_mex_data2d('get', rec_id);
+astra_mex_algorithm('delete', alg_id);
+
+figure(1); imshow(P, []);
+figure(2); imshow(rec_RL, []);
+figure(3); imshow(rec_filter, []);
+figure(4); imshow(rec_kernel, []);
+
+
+astra_mex_data2d('delete', rec_id);
+astra_mex_data2d('delete', sinogram_id);
+astra_mex_projector('delete', proj_id);
diff --git a/samples/python/s023_FBP_filters.py b/samples/python/s023_FBP_filters.py
new file mode 100644
index 0000000..11518ac
--- /dev/null
+++ b/samples/python/s023_FBP_filters.py
@@ -0,0 +1,116 @@
+# -----------------------------------------------------------------------
+# Copyright: 2010-2018, imec Vision Lab, University of Antwerp
+# 2013-2018, CWI, Amsterdam
+#
+# Contact: astra@astra-toolbox.com
+# Website: http://www.astra-toolbox.com/
+#
+# This file is part of the ASTRA Toolbox.
+#
+#
+# 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 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 ASTRA Toolbox. If not, see <http://www.gnu.org/licenses/>.
+#
+# -----------------------------------------------------------------------
+
+import astra
+import numpy as np
+import scipy.io
+
+
+# This sample script illustrates three ways of passing filters to FBP.
+# They work with both the FBP (CPU) and the FBP_CUDA (GPU) algorithms.
+
+
+N = 256
+
+vol_geom = astra.create_vol_geom(N, N)
+proj_geom = astra.create_proj_geom('parallel', 1.0, N, np.linspace(0,np.pi,180,False))
+
+P = scipy.io.loadmat('phantom.mat')['phantom256']
+
+proj_id = astra.create_projector('strip',proj_geom,vol_geom)
+sinogram_id, sinogram = astra.create_sino(P, proj_id)
+
+rec_id = astra.data2d.create('-vol', vol_geom)
+cfg = astra.astra_dict('FBP')
+cfg['ReconstructionDataId'] = rec_id
+cfg['ProjectionDataId'] = sinogram_id
+cfg['ProjectorId'] = proj_id
+
+
+
+# 1. Use a standard Ram-Lak filter
+cfg['FilterType'] = 'ram-lak'
+
+alg_id = astra.algorithm.create(cfg)
+astra.algorithm.run(alg_id)
+rec_RL = astra.data2d.get(rec_id)
+astra.algorithm.delete(alg_id)
+
+# 2. Define a filter in Fourier space
+# This is assumed to be symmetric, and ASTRA therefore expects only half
+
+# The full filter size should be the smallest power of two that is at least
+# twice the number of detector pixels.
+fullFilterSize = 2*N
+kernel = np.append( np.linspace(0, 1, fullFilterSize//2, endpoint=False), np.linspace(1, 0, fullFilterSize//2, endpoint=False) )
+halfFilterSize = fullFilterSize // 2 + 1
+filter = np.reshape(kernel[0:halfFilterSize], (1, halfFilterSize))
+
+filter_geom = astra.create_proj_geom('parallel', 1.0, halfFilterSize, [0]);
+filter_id = astra.data2d.create('-sino', filter_geom, filter);
+
+cfg['FilterType'] = 'projection'
+cfg['FilterSinogramId'] = filter_id
+alg_id = astra.algorithm.create(cfg)
+astra.algorithm.run(alg_id)
+rec_filter = astra.data2d.get(rec_id)
+astra.algorithm.delete(alg_id)
+
+
+# 3. Define a (spatial) convolution kernel directly
+# For a kernel of odd size 2*k+1, the central component is at kernel[k]
+# For a kernel of even size 2*k, the central component is at kernel[k]
+kernel = np.zeros((1, N))
+for i in range(0,N//4):
+ f = np.pi * (2*i + 1)
+ val = -2.0 / (f * f)
+ kernel[0, N//2 + (2*i+1)] = val
+ kernel[0, N//2 - (2*i+1)] = val
+kernel[0, N//2] = 0.5
+kernel_geom = astra.create_proj_geom('parallel', 1.0, N, [0]);
+kernel_id = astra.data2d.create('-sino', kernel_geom, kernel);
+
+cfg['FilterType'] = 'rprojection'
+cfg['FilterSinogramId'] = kernel_id
+alg_id = astra.algorithm.create(cfg)
+astra.algorithm.run(alg_id)
+rec_kernel = astra.data2d.get(rec_id)
+astra.algorithm.delete(alg_id)
+
+import pylab
+pylab.figure()
+pylab.imshow(P)
+pylab.figure()
+pylab.imshow(rec_RL)
+pylab.figure()
+pylab.imshow(rec_filter)
+pylab.figure()
+pylab.imshow(rec_kernel)
+pylab.show()
+
+astra.data2d.delete(rec_id)
+astra.data2d.delete(sinogram_id)
+astra.projector.delete(proj_id)
+