-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathMatlabSparseMatrixExample.py
More file actions
77 lines (64 loc) · 2.44 KB
/
Copy pathMatlabSparseMatrixExample.py
File metadata and controls
77 lines (64 loc) · 2.44 KB
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
#!/usr/bin/env python
import itk
import numpy as np
# Define types
OutputPixelType = itk.F
Dimension = 3
OutputImageType = itk.Image[OutputPixelType, Dimension]
# Parameters
numberOfProjections = 10
sid = 600 # source to isocenter distance
sdd = 1200 # source to detector distance
# Set up the geometry
GeometryType = itk.ThreeDCircularProjectionGeometry
geometry = GeometryType.New()
for i in range(numberOfProjections):
angle = (360.0 * i) / numberOfProjections
geometry.AddProjectionInRadians(0, 0, angle * np.pi / 180.0, sid, sdd, 0, 0)
# Create projection images
projectionsSource = itk.ConstantImageSource[OutputImageType].New()
projectionsSource.SetSize([512, 1, numberOfProjections])
projectionsSource.SetSpacing([1.0, 1.0, 1.0])
projectionsSource.SetOrigin([-256.0, 0.0, 0.0])
projectionsSource.SetConstant(1.0)
projectionsSource.Update()
# Create volume
volumeSource = itk.ConstantImageSource[OutputImageType].New()
volumeSource.SetSize([32, 32, 32])
volumeSource.SetSpacing([2.0, 2.0, 2.0])
volumeSource.SetOrigin([-32.0, -32.0, -32.0])
volumeSource.SetConstant(0.0)
volumeSource.Update()
# Back-project with matrix capture
IWM = itk.InterpolationWeightMultiplicationBackProjection[
OutputPixelType, OutputPixelType
]
SSM = itk.StoreSparseMatrixSplatWeightMultiplication[
OutputPixelType, itk.D, OutputPixelType
]
backProjection = itk.JosephBackProjectionImageFilter[
OutputImageType, OutputImageType, IWM, SSM
].New()
backProjection.SetInput(volumeSource.GetOutput())
backProjection.SetInput(1, projectionsSource.GetOutput())
backProjection.SetGeometry(geometry)
# Initialize and configure matrix capture
backProjection.GetSplatWeightMultiplication().Resize(
projectionsSource.GetOutput().GetLargestPossibleRegion().GetNumberOfPixels(),
volumeSource.GetOutput().GetLargestPossibleRegion().GetNumberOfPixels(),
)
backProjection.GetSplatWeightMultiplication().SetProjectionsBuffer(
projectionsSource.GetOutput().GetBufferPointer()
)
backProjection.GetSplatWeightMultiplication().SetVolumeBuffer(
volumeSource.GetOutput().GetBufferPointer()
)
backProjection.Update()
# Export to Matlab format
matlabMatrix = itk.MatlabSparseMatrix[OutputImageType].New()
matlabMatrix.SetMatrixFromFunctor(backProjection.GetSplatWeightMultiplication())
matlabMatrix.SetOutput(volumeSource.GetOutput())
matlabMatrix.Save("backprojection_matrix.mat")
# Print matrix information
matlabMatrix.Print()
print("Matrix successfully saved to backprojection_matrix.mat")