-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpctFDKDDBackProjectionImageFilter.txx
222 lines (192 loc) · 8.65 KB
/
pctFDKDDBackProjectionImageFilter.txx
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#ifndef __pctFDKDDBackProjectionImageFilter_txx
#define __pctFDKDDBackProjectionImageFilter_txx
#include <itkImageRegionIteratorWithIndex.h>
#include <itkLinearInterpolateImageFunction.h>
#include <rtkThreeDCircularProjectionGeometry.h>
namespace pct
{
/**
* GenerateData performs the accumulation
*/
template <class TInputImage, class TOutputImage>
void
FDKDDBackProjectionImageFilter<TInputImage,TOutputImage>
::DynamicThreadedGenerateData(const OutputImageRegionType& outputRegionForThread)
{
const unsigned int Dimension = TInputImage::ImageDimension;
const unsigned int nProj = m_ProjectionStack->GetLargestPossibleRegion().GetSize(Dimension);
const unsigned int iFirstProj = m_ProjectionStack->GetLargestPossibleRegion().GetIndex(Dimension);
const rtk::ThreeDCircularProjectionGeometry * geometry;
geometry = this->GetGeometry();
// Create interpolator, could be any interpolation
typedef itk::LinearInterpolateImageFunction< ProjectionImageType, double > InterpolatorType;
typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
// Iterators on volume input and output
typedef itk::ImageRegionConstIterator<TInputImage> InputRegionIterator;
InputRegionIterator itIn(this->GetInput(), outputRegionForThread);
typedef itk::ImageRegionIteratorWithIndex<TOutputImage> OutputRegionIterator;
OutputRegionIterator itOut(this->GetOutput(), outputRegionForThread);
// Initialize output region with input region in case the filter is not in
// place
if(this->GetInput() != this->GetOutput() )
{
itIn.GoToBegin();
while(!itIn.IsAtEnd() )
{
itOut.Set(itIn.Get() );
++itIn;
++itOut;
}
}
// Rotation center (assumed to be at 0 yet)
typename TInputImage::PointType rotCenterPoint;
rotCenterPoint.Fill(0.0);
itk::ContinuousIndex<double, Dimension> rotCenterIndex;
this->GetInput(0)->TransformPhysicalPointToContinuousIndex(rotCenterPoint, rotCenterIndex);
// Continuous index at which we interpolate
itk::ContinuousIndex<double, Dimension> pointProj;
// Go over each projection
for(unsigned int iProj=iFirstProj; iProj<iFirstProj+nProj; iProj++)
{
const double sid = geometry->GetSourceToIsocenterDistances()[iProj];
const double sdd = geometry->GetSourceToDetectorDistances()[iProj];
// Extract the current slice
ProjectionImagePointer projection = this->GetDDProjection(iProj);
interpolator->SetInputImage(projection);
// Index to index matrix normalized to have a correct backprojection weight
// (1 at the isocenter)
ProjectionMatrixType matrix;
if(sdd==0.)
{
// Create a 3D translation matrix for the projection instead of the 2D because
// we want to keep the z coordinate
itk::Matrix<double, Dimension+1, Dimension+1> matProjTrans;
double sourceOffsetX = geometry->GetSourceOffsetsX()[iProj];
double sourceOffsetY = geometry->GetSourceOffsetsY()[iProj];
double projOffsetX = geometry->GetProjectionOffsetsX()[iProj];
double projOffsetY = geometry->GetProjectionOffsetsY()[iProj];
matProjTrans = rtk::ThreeDCircularProjectionGeometry::ComputeTranslationHomogeneousMatrix(sourceOffsetX-projOffsetX, sourceOffsetY-projOffsetY, 0.);
// Flip because disance-driven binning puts the source on the negative side and the backprojection on the positive side
matProjTrans[2][2] = -1.;
itk::Matrix<double, Dimension+1, Dimension+1> matrixVol =
rtk::GetIndexToPhysicalPointMatrix< TOutputImage >( this->GetOutput() );
itk::Matrix<double, Dimension+1, Dimension+1> matrixStackProj =
rtk::GetPhysicalPointToIndexMatrix< TOutputImage >( projection );
matrixVol = matrixStackProj.GetVnlMatrix() *
matProjTrans.GetVnlMatrix() *
geometry->GetSourceTranslationMatrices()[iProj].GetVnlMatrix()*
geometry->GetRotationMatrices()[iProj].GetVnlMatrix() *
matrixVol.GetVnlMatrix();
// Remove last line that is not used in the following
for(unsigned int i=0; i<Dimension; i++)
for(unsigned int j=0; j<Dimension+1; j++)
matrix[i][j] = matrixVol[i][j];
}
else
{
matrix = GetIndexToIndexProjectionMatrix(iProj, projection);
double perspFactor = matrix[Dimension-1][Dimension];
for(unsigned int j=0; j<Dimension; j++)
perspFactor += matrix[Dimension-1][j] * rotCenterIndex[j];
matrix /= perspFactor;
}
// Go over each voxel
itOut.GoToBegin();
while(!itOut.IsAtEnd() )
{
if(sdd==0.) // Parallel
{
// Compute projection index
for(unsigned int i=0; i<Dimension; i++)
{
pointProj[i] = matrix[i][Dimension];
for(unsigned int j=0; j<Dimension; j++)
pointProj[i] += matrix[i][j] * itOut.GetIndex()[j];
}
// Interpolate if in projection
if( interpolator->IsInsideBuffer(pointProj) )
{
itOut.Set( itOut.Get() + interpolator->EvaluateAtContinuousIndex(pointProj) );
}
}
else // Cone-beam
{
// Compute projection index
for(unsigned int i=0; i<Dimension-1; i++)
{
pointProj[i] = matrix[i][Dimension];
for(unsigned int j=0; j<Dimension; j++)
pointProj[i] += matrix[i][j] * itOut.GetIndex()[j];
}
// Apply perspective
double perspFactor = matrix[Dimension-1][Dimension];
for(unsigned int j=0; j<Dimension; j++)
perspFactor += matrix[Dimension-1][j] * itOut.GetIndex()[j];
perspFactor = 1/perspFactor;
for(unsigned int i=0; i<Dimension-1; i++)
pointProj[i] = pointProj[i]*perspFactor;
// Distance driven
pointProj[Dimension-1] = (sid / perspFactor - sid - m_ProjectionStack->GetOrigin()[Dimension-1]) / m_ProjectionStack->GetSpacing()[Dimension-1];
// Interpolate if in projection
if( interpolator->IsInsideBuffer(pointProj) )
{
itOut.Set( itOut.Get() + perspFactor*perspFactor*interpolator->EvaluateAtContinuousIndex(pointProj) );
}
}
++itOut;
}
}
}
template <class TInputImage, class TOutputImage>
typename FDKDDBackProjectionImageFilter<TInputImage,TOutputImage>::ProjectionImagePointer
FDKDDBackProjectionImageFilter<TInputImage,TOutputImage>
::GetDDProjection(const unsigned int iProj)
{
const int iProjBuff = m_ProjectionStack->GetBufferedRegion().GetIndex(ProjectionImageType::ImageDimension);
ProjectionImagePointer projection = ProjectionImageType::New();
typename ProjectionImageType::RegionType region;
typename ProjectionImageType::SpacingType spacing;
typename ProjectionImageType::PointType origin;
for(unsigned int i=0; i<ProjectionImageType::ImageDimension; i++)
{
origin[i] = m_ProjectionStack->GetOrigin()[i];
spacing[i] = m_ProjectionStack->GetSpacing()[i];
region.SetSize(i, m_ProjectionStack->GetLargestPossibleRegion().GetSize()[i]);
region.SetIndex(i, m_ProjectionStack->GetLargestPossibleRegion().GetIndex()[i]);
}
projection->SetSpacing(spacing);
projection->SetOrigin(origin);
projection->SetRegions(region);
projection->Allocate();
const unsigned int npixels = projection->GetLargestPossibleRegion().GetNumberOfPixels();
const ProjectionPixelType *pi = m_ProjectionStack->GetBufferPointer() + (iProj-iProjBuff)*npixels;
ProjectionPixelType * po = projection->GetBufferPointer();
// Transpose projection for optimization
for(unsigned int i=0; i<npixels; i++)
*po++ = *pi++;
return projection;
}
template <class TInputImage, class TOutputImage>
typename rtk::BackProjectionImageFilter<TInputImage,TOutputImage>::ProjectionMatrixType
FDKDDBackProjectionImageFilter<TInputImage,TOutputImage>
::GetIndexToIndexProjectionMatrix(const unsigned int iProj, const ProjectionImageType *proj)
{
const unsigned int Dimension = TInputImage::ImageDimension;
itk::Matrix<double, Dimension+1, Dimension+1> matrixVol;
matrixVol = rtk::GetIndexToPhysicalPointMatrix< TOutputImage >( this->GetOutput() );
itk::Matrix<double, Dimension+1, Dimension+1> matrixProjFull;
matrixProjFull = rtk::GetPhysicalPointToIndexMatrix< ProjectionImageType >(proj);
itk::Matrix<double, Dimension, Dimension> matrixProj;
matrixProj.SetIdentity();
for(unsigned int j=0; j<Dimension-1; j++)
{
for(unsigned int i=0; i<Dimension-1; i++)
matrixProj[i][j] = matrixProjFull[i][j];
matrixProj[j][Dimension-1] = matrixProjFull[j][Dimension];
}
return ProjectionMatrixType(matrixProj.GetVnlMatrix() *
this->GetGeometry()->GetMatrices()[iProj].GetVnlMatrix() *
matrixVol.GetVnlMatrix() );
}
} // end namespace pct
#endif