-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathitkSliceBySliceImageFilter.txx
252 lines (213 loc) · 8.67 KB
/
itkSliceBySliceImageFilter.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#ifndef __itkSliceBySliceImageFilter_txx
#define __itkSliceBySliceImageFilter_txx
#include "itkSliceBySliceImageFilter.h"
#include "itkProgressReporter.h"
namespace itk {
template <class TInputImage, class TOutputImage, class TInputFilter, class TOutputFilter, class TInternalInputImageType, class TInternalOutputImageType>
SliceBySliceImageFilter<TInputImage, TOutputImage, TInputFilter, TOutputFilter, TInternalInputImageType, TInternalOutputImageType>
::SliceBySliceImageFilter()
{
m_InputFilter = NULL;
m_OutputFilter = NULL;
m_Dimension = ImageDimension - 1;
}
template <class TInputImage, class TOutputImage, class TInputFilter, class TOutputFilter, class TInternalInputImageType, class TInternalOutputImageType>
void
SliceBySliceImageFilter<TInputImage, TOutputImage, TInputFilter, TOutputFilter, TInternalInputImageType, TInternalOutputImageType>
::GenerateInputRequestedRegion()
{
// call the superclass' implementation of this method
Superclass::GenerateInputRequestedRegion();
for( int i=0; i<this->GetNumberOfInputs(); i++)
{
typename InputImageType::Pointer inputPtr =
const_cast< InputImageType * >( this->GetInput( i ) );
if ( !inputPtr )
{ return; }
inputPtr->SetRequestedRegion(inputPtr->GetLargestPossibleRegion());
}
}
template <class TInputImage, class TOutputImage, class TInputFilter, class TOutputFilter, class TInternalInputImageType, class TInternalOutputImageType>
void
SliceBySliceImageFilter<TInputImage, TOutputImage, TInputFilter, TOutputFilter, TInternalInputImageType, TInternalOutputImageType>
::EnlargeOutputRequestedRegion(DataObject *)
{
for( int i=0; i<this->GetNumberOfOutputs(); i++)
{
this->GetOutput( i )->SetRequestedRegion( this->GetOutput( i )->GetLargestPossibleRegion() );
}
}
template <class TInputImage, class TOutputImage, class TInputFilter, class TOutputFilter, class TInternalInputImageType, class TInternalOutputImageType>
void
SliceBySliceImageFilter<TInputImage, TOutputImage, TInputFilter, TOutputFilter, TInternalInputImageType, TInternalOutputImageType>
::SetFilter( InputFilterType * filter )
{
OutputFilterType * outputFilter = dynamic_cast< OutputFilterType * >( filter );
if( outputFilter == NULL && filter != NULL )
{
// TODO: can it be replaced by a concept check ?
itkExceptionMacro("Wrong output filter type. Use SetOutputFilter() and SetInputFilter() instead of SetFilter() when input and output filter types are different.");
}
this->SetInputFilter( filter );
this->SetOutputFilter( outputFilter );
}
template <class TInputImage, class TOutputImage, class TInputFilter, class TOutputFilter, class TInternalInputImageType, class TInternalOutputImageType>
void
SliceBySliceImageFilter<TInputImage, TOutputImage, TInputFilter, TOutputFilter, TInternalInputImageType, TInternalOutputImageType>
::SetInputFilter( InputFilterType * filter )
{
if( m_InputFilter.GetPointer() != filter )
{
this->Modified();
m_InputFilter = filter;
// adapt the number of inputs and outputs
this->SetNumberOfRequiredInputs( filter->GetNumberOfValidRequiredInputs() );
}
}
template <class TInputImage, class TOutputImage, class TInputFilter, class TOutputFilter, class TInternalInputImageType, class TInternalOutputImageType>
void
SliceBySliceImageFilter<TInputImage, TOutputImage, TInputFilter, TOutputFilter, TInternalInputImageType, TInternalOutputImageType>
::SetOutputFilter( OutputFilterType * filter )
{
if( m_OutputFilter.GetPointer() != filter )
{
this->Modified();
m_OutputFilter = filter;
// adapt the number of inputs and outputs
this->SetNumberOfRequiredOutputs( filter->GetNumberOfOutputs() );
}
}
template <class TInputImage, class TOutputImage, class TInputFilter, class TOutputFilter, class TInternalInputImageType, class TInternalOutputImageType>
void
SliceBySliceImageFilter<TInputImage, TOutputImage, TInputFilter, TOutputFilter, TInternalInputImageType, TInternalOutputImageType>
::GenerateData()
{
if( !m_InputFilter || !m_OutputFilter )
{
itkExceptionMacro("Filters must be set.");
}
for( int i=1; i<this->GetNumberOfInputs(); i++)
{
if ( this->GetInput()->GetRequestedRegion().GetSize() != this->GetInput( i )->GetRequestedRegion().GetSize() )
{
itkExceptionMacro( << "Inputs must have the same size." );
}
}
this->AllocateOutputs();
RegionType requestedRegion = this->GetOutput()->GetRequestedRegion();
IndexType requestedIndex = requestedRegion.GetIndex();
SizeType requestedSize = requestedRegion.GetSize();
InternalRegionType internalRegion;
InternalSizeType internalSize;
InternalIndexType internalIndex;
for( int i=0; i<InternalImageDimension; i++ )
{
if(i!=m_Dimension)
{
internalSize[i] = requestedSize[i];
internalIndex[i] = requestedIndex[i];
}
else
{
internalSize[i] = requestedSize[ ImageDimension - 1 ];
internalIndex[i] = requestedIndex[ ImageDimension - 1 ];
}
}
internalRegion.SetSize( internalSize );
internalRegion.SetIndex( internalIndex );
ProgressReporter progress(this, 0, requestedSize[m_Dimension]);
// std::cout << "start: " << requestedIndex[m_Dimension] << " end: " << requestedSize[m_Dimension] - requestedIndex[m_Dimension] << std::endl;
for( int slice=requestedIndex[m_Dimension]; slice < requestedSize[m_Dimension] - requestedIndex[m_Dimension]; slice++ )
{
// std::cout << "slice: " << slice << std::endl;
// reallocate the internal input at each slice, so the slice by slice filter can work
// even if the pipeline is run in place
typename std::vector<typename InternalInputImageType::Pointer> internalInputs;
internalInputs.resize( this->GetNumberOfInputs() );
for( int i=0; i<this->GetNumberOfInputs(); i++)
{
internalInputs[i] = InternalInputImageType::New();
internalInputs[i]->SetRegions( internalRegion );
internalInputs[i]->Allocate();
m_InputFilter->SetInput( i, internalInputs[i] );
}
// copy the current slice to the input image
typedef ImageRegionIterator< InternalInputImageType > InputIteratorType;
typename std::vector< InputIteratorType > inputIterators;
inputIterators.resize( this->GetNumberOfInputs() );
for( int i=0; i<this->GetNumberOfInputs(); i++)
{
inputIterators[i] = InputIteratorType( internalInputs[i], internalRegion );
inputIterators[i].GoToBegin();
}
while( !inputIterators[0].IsAtEnd() )
{
IndexType idx;
const InternalIndexType & iidx = inputIterators[0].GetIndex();
for( int i=0; i<InternalImageDimension; i++ )
{
if( i >= m_Dimension )
{
idx[i+1] = iidx[i];
}
else
{
idx[i] = iidx[i];
}
}
idx[ m_Dimension ] = slice;
for( int i=0; i<this->GetNumberOfInputs(); i++)
{
inputIterators[i].Set( this->GetInput( i )->GetPixel( idx ) );
++(inputIterators[i]);
}
}
// run the filter on the current slice
m_InputFilter->Modified();
m_OutputFilter->Modified(); // should not be needed, but may help in some cases
m_OutputFilter->UpdateLargestPossibleRegion();
progress.CompletedPixel();
// and copy the output slice to the output image
typedef ImageRegionConstIterator< InternalOutputImageType > OutputIteratorType;
typename std::vector< OutputIteratorType > outputIterators;
outputIterators.resize( this->GetNumberOfOutputs() );
for( int i=0; i<this->GetNumberOfOutputs(); i++)
{
outputIterators[i] = OutputIteratorType( m_OutputFilter->GetOutput( i ), internalRegion );
outputIterators[i].GoToBegin();
}
while( !outputIterators[0].IsAtEnd() )
{
IndexType idx;
const InternalIndexType & iidx = outputIterators[0].GetIndex();
for( int i=0; i<InternalImageDimension; i++ )
{
if( i >= m_Dimension )
{
idx[i+1] = iidx[i];
}
else
{
idx[i] = iidx[i];
}
}
idx[ m_Dimension ] = slice;
for( int i=0; i<this->GetNumberOfOutputs(); i++)
{
this->GetOutput( i )->SetPixel( idx, outputIterators[i].Get() );
++(outputIterators[i]);
}
}
this->InvokeEvent( IterationEvent() );
}
}
template <class TInputImage, class TOutputImage, class TInputFilter, class TOutputFilter, class TInternalInputImageType, class TInternalOutputImageType>
void
SliceBySliceImageFilter<TInputImage, TOutputImage, TInputFilter, TOutputFilter, TInternalInputImageType, TInternalOutputImageType>
::PrintSelf(std::ostream &os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "Dimension: " << m_Dimension << std::endl;
}
}
#endif