4 #include <itkAddImageFilter.h> 5 #include <itkMultiplyImageFilter.h> 7 #include <itkComposeDisplacementFieldsImageFilter.h> 8 #include <itkVectorLinearInterpolateNearestNeighborExtrapolateImageFunction.h> 15 template <
class ScalarType,
unsigned int NDimensions>
16 void composeSVF(itk::StationaryVelocityFieldTransform <ScalarType,NDimensions> *baseTrsf,
17 itk::StationaryVelocityFieldTransform <ScalarType,NDimensions> *addonTrsf,
18 unsigned int numThreads,
unsigned int bchOrder)
20 if ((baseTrsf->GetParametersAsVectorField() == NULL)&&(addonTrsf->GetParametersAsVectorField() == NULL))
23 if ((bchOrder > 4)||(bchOrder < 1))
24 throw itk::ExceptionObject(__FILE__,__LINE__,
"Invalid BCH order, not implemented yet",ITK_LOCATION);
26 typedef typename itk::StationaryVelocityFieldTransform <ScalarType,NDimensions>::VectorFieldType VelocityFieldType;
28 if (baseTrsf->GetParametersAsVectorField() == NULL)
30 baseTrsf->SetParametersAsVectorField(addonTrsf->GetParametersAsVectorField());
34 typedef itk::AddImageFilter <VelocityFieldType, VelocityFieldType> AddFilterType;
35 typedef itk::MultiplyImageFilter <VelocityFieldType, itk::Image <double, NDimensions>,
36 VelocityFieldType> MultiplyConstFilterType;
38 typename AddFilterType::Pointer bchAdder = AddFilterType::New();
39 bchAdder->SetInput(0,baseTrsf->GetParametersAsVectorField());
40 bchAdder->SetInput(1,addonTrsf->GetParametersAsVectorField());
43 bchAdder->SetNumberOfWorkUnits(numThreads);
47 typename VelocityFieldType::Pointer resField = bchAdder->GetOutput();
48 resField->DisconnectPipeline();
51 typename LieBracketFilterType::JacobianImagePointer baseTrsfJac, addonTrsfJac;
52 typename LieBracketFilterType::OutputImagePointer previousLieBracket;
57 typename LieBracketFilterType::Pointer lieBracketFilter = LieBracketFilterType::New();
59 lieBracketFilter->SetInput(0,baseTrsf->GetParametersAsVectorField());
60 lieBracketFilter->SetInput(1,addonTrsf->GetParametersAsVectorField());
63 lieBracketFilter->SetNumberOfWorkUnits(numThreads);
65 lieBracketFilter->Update();
66 baseTrsfJac = lieBracketFilter->GetFirstFieldJacobian();
67 baseTrsfJac->DisconnectPipeline();
68 addonTrsfJac = lieBracketFilter->GetSecondFieldJacobian();
69 addonTrsfJac->DisconnectPipeline();
70 previousLieBracket = lieBracketFilter->GetOutput();
71 previousLieBracket->DisconnectPipeline();
73 typename MultiplyConstFilterType::Pointer bracketMultiplier = MultiplyConstFilterType::New();
74 bracketMultiplier->SetInput(0,previousLieBracket);
75 bracketMultiplier->SetConstant(0.5);
78 bracketMultiplier->SetNumberOfWorkUnits(numThreads);
80 bracketMultiplier->Update();
82 typename AddFilterType::Pointer bchSecondAdder = AddFilterType::New();
83 bchSecondAdder->SetInput(0,resField);
84 bchSecondAdder->SetInput(1,bracketMultiplier->GetOutput());
87 bchSecondAdder->SetNumberOfWorkUnits(numThreads);
89 bchSecondAdder->Update();
91 resField = bchSecondAdder->GetOutput();
92 resField->DisconnectPipeline();
98 typename LieBracketFilterType::Pointer lieBracketFilter = LieBracketFilterType::New();
100 lieBracketFilter->SetInput(0,baseTrsf->GetParametersAsVectorField());
101 lieBracketFilter->SetInput(1,previousLieBracket);
102 lieBracketFilter->SetFirstFieldJacobian(baseTrsfJac);
105 lieBracketFilter->SetNumberOfWorkUnits(numThreads);
107 lieBracketFilter->Update();
109 typename MultiplyConstFilterType::Pointer bracketMultiplier = MultiplyConstFilterType::New();
110 bracketMultiplier->SetInput(0,lieBracketFilter->GetOutput());
111 bracketMultiplier->SetConstant(1.0 / 12);
114 bracketMultiplier->SetNumberOfWorkUnits(numThreads);
116 bracketMultiplier->Update();
118 typename AddFilterType::Pointer bchSecondAdder = AddFilterType::New();
119 bchSecondAdder->SetInput(0,resField);
120 bchSecondAdder->SetInput(1,bracketMultiplier->GetOutput());
123 bchSecondAdder->SetNumberOfWorkUnits(numThreads);
125 bchSecondAdder->Update();
127 resField = bchSecondAdder->GetOutput();
128 resField->DisconnectPipeline();
131 typename LieBracketFilterType::Pointer reverseLieBracketFilter = LieBracketFilterType::New();
133 reverseLieBracketFilter->SetInput(0,previousLieBracket);
134 reverseLieBracketFilter->SetInput(1,addonTrsf->GetParametersAsVectorField());
135 reverseLieBracketFilter->SetFirstFieldJacobian(lieBracketFilter->GetSecondFieldJacobian());
136 reverseLieBracketFilter->SetSecondFieldJacobian(addonTrsfJac);
139 reverseLieBracketFilter->SetNumberOfWorkUnits(numThreads);
141 reverseLieBracketFilter->Update();
143 bracketMultiplier = MultiplyConstFilterType::New();
144 bracketMultiplier->SetInput(0,reverseLieBracketFilter->GetOutput());
145 bracketMultiplier->SetConstant(1.0 / 12);
148 bracketMultiplier->SetNumberOfWorkUnits(numThreads);
150 bracketMultiplier->Update();
152 bchSecondAdder = AddFilterType::New();
153 bchSecondAdder->SetInput(0,resField);
154 bchSecondAdder->SetInput(1,bracketMultiplier->GetOutput());
157 bchSecondAdder->SetNumberOfWorkUnits(numThreads);
159 bchSecondAdder->Update();
161 resField = bchSecondAdder->GetOutput();
162 resField->DisconnectPipeline();
164 previousLieBracket = lieBracketFilter->GetOutput();
165 previousLieBracket->DisconnectPipeline();
171 typename LieBracketFilterType::Pointer lieBracketFilter = LieBracketFilterType::New();
173 lieBracketFilter->SetInput(0,previousLieBracket);
174 lieBracketFilter->SetInput(1,addonTrsf->GetParametersAsVectorField());
175 lieBracketFilter->SetSecondFieldJacobian(addonTrsfJac);
178 lieBracketFilter->SetNumberOfWorkUnits(numThreads);
180 lieBracketFilter->Update();
182 typename MultiplyConstFilterType::Pointer bracketMultiplier = MultiplyConstFilterType::New();
183 bracketMultiplier->SetInput(0,lieBracketFilter->GetOutput());
184 bracketMultiplier->SetConstant(1.0 / 24);
187 bracketMultiplier->SetNumberOfWorkUnits(numThreads);
189 bracketMultiplier->Update();
191 typename AddFilterType::Pointer bchSecondAdder = AddFilterType::New();
192 bchSecondAdder->SetInput(0,resField);
193 bchSecondAdder->SetInput(1,bracketMultiplier->GetOutput());
196 bchSecondAdder->SetNumberOfWorkUnits(numThreads);
198 bchSecondAdder->Update();
200 resField = bchSecondAdder->GetOutput();
201 resField->DisconnectPipeline();
204 baseTrsf->SetParametersAsVectorField(resField.GetPointer());
207 template <
class ScalarType,
unsigned int NDimensions>
208 void GetSVFExponential(itk::StationaryVelocityFieldTransform <ScalarType,NDimensions> *baseTrsf,
209 rpi::DisplacementFieldTransform <ScalarType,NDimensions> *resultTransform,
210 unsigned int exponentiationOrder,
unsigned int numThreads,
bool invert)
212 if (baseTrsf->GetParametersAsVectorField() == NULL)
215 typedef itk::StationaryVelocityFieldTransform <ScalarType,NDimensions> SVFType;
216 typedef typename SVFType::VectorFieldType FieldType;
217 typedef typename FieldType::Pointer FieldPointer;
221 FieldPointer tmpPtr = const_cast <FieldType *> (baseTrsf->GetParametersAsVectorField());
224 typedef itk::MultiplyImageFilter <FieldType,itk::Image <double, NDimensions>, FieldType> MultiplyFilterType;
225 typename MultiplyFilterType::Pointer multiplier = MultiplyFilterType::New();
226 multiplier->SetInput(tmpPtr);
227 multiplier->SetConstant(-1.0);
229 multiplier->SetNumberOfWorkUnits(numThreads);
230 multiplier->Update();
232 tmpPtr = multiplier->GetOutput();
233 tmpPtr->DisconnectPipeline();
236 typename ExponentialFilterType::Pointer expFilter = ExponentialFilterType::New();
237 expFilter->SetInput(tmpPtr);
238 expFilter->SetExponentiationOrder(exponentiationOrder);
239 expFilter->SetNumberOfWorkUnits(numThreads);
240 expFilter->SetMaximalDisplacementAmplitude(0.25);
244 FieldPointer resField = expFilter->GetOutput();
245 resField->DisconnectPipeline();
247 resultTransform->SetParametersAsVectorField(resField.GetPointer());
250 template <
class ScalarType,
unsigned int NDimensions>
252 typename rpi::DisplacementFieldTransform <ScalarType,NDimensions>::Pointer &positiveAddOn,
253 typename rpi::DisplacementFieldTransform <ScalarType,NDimensions>::Pointer &negativeAddOn,
254 unsigned int numThreads)
256 typedef rpi::DisplacementFieldTransform <ScalarType,NDimensions> DisplacementFieldTransformType;
257 typedef typename DisplacementFieldTransformType::VectorFieldType VectorFieldType;
258 typedef itk::ComposeDisplacementFieldsImageFilter <VectorFieldType,VectorFieldType> ComposeFilterType;
259 typedef itk::MultiplyImageFilter <VectorFieldType,itk::Image <double, NDimensions>, VectorFieldType> MultiplyFilterType;
260 typedef typename itk::ImageRegionIterator <VectorFieldType> VectorFieldIterator;
261 typedef typename VectorFieldType::PixelType VectorType;
263 typedef itk::VectorLinearInterpolateNearestNeighborExtrapolateImageFunction <VectorFieldType,
264 typename VectorType::ValueType> VectorInterpolateFunctionType;
266 typename ComposeFilterType::Pointer composePositiveFilter = ComposeFilterType::New();
267 composePositiveFilter->SetWarpingField(positiveAddOn->GetParametersAsVectorField());
268 composePositiveFilter->SetDisplacementField(baseTrsf->GetParametersAsVectorField());
269 composePositiveFilter->SetNumberOfWorkUnits(numThreads);
271 typename VectorInterpolateFunctionType::Pointer interpolator = VectorInterpolateFunctionType::New();
273 composePositiveFilter->SetInterpolator(interpolator);
274 composePositiveFilter->Update();
275 positiveAddOn->SetParametersAsVectorField(composePositiveFilter->GetOutput());
277 typename MultiplyFilterType::Pointer multiplyFilter = MultiplyFilterType::New();
278 multiplyFilter->SetInput(baseTrsf->GetParametersAsVectorField());
279 multiplyFilter->SetConstant(-1.0);
282 multiplyFilter->SetNumberOfWorkUnits(numThreads);
284 multiplyFilter->InPlaceOn();
286 multiplyFilter->Update();
288 typename ComposeFilterType::Pointer composeNegativeFilter = ComposeFilterType::New();
289 composeNegativeFilter->SetWarpingField(negativeAddOn->GetParametersAsVectorField());
290 composeNegativeFilter->SetDisplacementField(multiplyFilter->GetOutput());
293 composeNegativeFilter->SetNumberOfWorkUnits(numThreads);
295 interpolator = VectorInterpolateFunctionType::New();
297 composeNegativeFilter->SetInterpolator(interpolator);
298 composeNegativeFilter->Update();
299 negativeAddOn->SetParametersAsVectorField(composeNegativeFilter->GetOutput());
301 VectorFieldIterator positiveItr(const_cast <VectorFieldType *> (positiveAddOn->GetParametersAsVectorField()),
302 positiveAddOn->GetParametersAsVectorField()->GetLargestPossibleRegion());
304 VectorFieldIterator negativeItr(const_cast <VectorFieldType *> (negativeAddOn->GetParametersAsVectorField()),
305 negativeAddOn->GetParametersAsVectorField()->GetLargestPossibleRegion());
309 while (!positiveItr.IsAtEnd())
311 tmpVec = 0.5 * (positiveItr.Get() - negativeItr.Get());
312 positiveItr.Set(tmpVec);
313 negativeItr.Set(- tmpVec);
319 baseTrsf = positiveAddOn;
Computes the Lie bracket between two fields u and v as expressed by Bossa et al.
Computes the exponentiation of a stationary velocity field using sclaing and squaring and approximate...
void GetSVFExponential(itk::StationaryVelocityFieldTransform< ScalarType, NDimensions > *baseTrsf, rpi::DisplacementFieldTransform< ScalarType, NDimensions > *resultTransform, unsigned int exponentiationOrder, unsigned int numThreads, bool invert)
void composeSVF(itk::StationaryVelocityFieldTransform< ScalarType, NDimensions > *baseTrsf, itk::StationaryVelocityFieldTransform< ScalarType, NDimensions > *addonTrsf, unsigned int numThreads, unsigned int bchOrder)
void composeDistortionCorrections(typename rpi::DisplacementFieldTransform< ScalarType, NDimensions >::Pointer &baseTrsf, typename rpi::DisplacementFieldTransform< ScalarType, NDimensions >::Pointer &positiveAddOn, typename rpi::DisplacementFieldTransform< ScalarType, NDimensions >::Pointer &negativeAddOn, unsigned int numThreads)