[5d12a0]: / src / integrateVelocityField.cxx

Download this file

114 lines (89 with data), 3.9 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
 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
#include <nanobind/nanobind.h>
#include <nanobind/stl/vector.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/tuple.h>
#include <nanobind/stl/list.h>
#include <nanobind/ndarray.h>
#include <nanobind/stl/shared_ptr.h>
#include <exception>
#include <vector>
#include <string>
#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkVector.h"
#include "itkTimeVaryingVelocityFieldIntegrationImageFilter.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "antsImage.h"
namespace nb = nanobind;
using namespace nb::literals;
template <unsigned int Dimension>
AntsImage<itk::VectorImage<float, Dimension>> integrateVelocityField( AntsImage<itk::VectorImage<float, Dimension+1>> & antsVelocityField,
float lowerBound,
float upperBound,
unsigned int numberOfIntegrationSteps )
{
using RealType = float;
using ANTsVelocityFieldType = itk::VectorImage<RealType, Dimension+1>;
using ANTsVelocityFieldPointerType = typename ANTsVelocityFieldType::Pointer;
using ANTsFieldType = itk::VectorImage<RealType, Dimension>;
using ANTsFieldPointerType = typename ANTsFieldType::Pointer;
using VectorType = itk::Vector<RealType, Dimension>;
using ITKVelocityFieldType = itk::Image<VectorType, Dimension+1>;
using ITKVelocityFieldPointerType = typename ITKVelocityFieldType::Pointer;
using ITKFieldType = itk::Image<VectorType, Dimension>;
using IteratorType = itk::ImageRegionIteratorWithIndex<ITKVelocityFieldType>;
using ConstIteratorType = itk::ImageRegionConstIteratorWithIndex<ITKFieldType>;
ANTsVelocityFieldPointerType inputVelocityField = antsVelocityField.ptr;
ITKVelocityFieldPointerType inputITKVelocityField = ITKVelocityFieldType::New();
inputITKVelocityField->CopyInformation( inputVelocityField );
inputITKVelocityField->SetRegions( inputVelocityField->GetRequestedRegion() );
inputITKVelocityField->AllocateInitialized();
IteratorType It( inputITKVelocityField,
inputITKVelocityField->GetRequestedRegion() );
for( It.GoToBegin(); !It.IsAtEnd(); ++It )
{
VectorType vector;
typename ANTsFieldType::PixelType antsVector = inputVelocityField->GetPixel( It.GetIndex() );
for( unsigned int d = 0; d < Dimension; d++ )
{
vector[d] = antsVector[d];
}
It.Set( vector );
}
using IntegratorType = itk::TimeVaryingVelocityFieldIntegrationImageFilter<ITKVelocityFieldType, ITKFieldType>;
typename IntegratorType::Pointer integrator = IntegratorType::New();
integrator->SetInput( inputITKVelocityField );
integrator->SetLowerTimeBound( lowerBound );
integrator->SetUpperTimeBound( upperBound );
integrator->SetNumberOfIntegrationSteps( numberOfIntegrationSteps );
integrator->Update();
//////////////////////////
//
// Now convert back to vector image type.
//
ANTsFieldPointerType antsField = ANTsFieldType::New();
antsField->CopyInformation( integrator->GetOutput() );
antsField->SetRegions( integrator->GetOutput()->GetRequestedRegion() );
antsField->SetVectorLength( Dimension );
antsField->AllocateInitialized();
ConstIteratorType ItI( integrator->GetOutput(),
integrator->GetOutput()->GetRequestedRegion() );
for( ItI.GoToBegin(); !ItI.IsAtEnd(); ++ItI )
{
VectorType data = ItI.Value();
typename ANTsFieldType::PixelType antsVector( Dimension );
for( unsigned int d = 0; d < Dimension; d++ )
{
antsVector[d] = data[d];
}
antsField->SetPixel( ItI.GetIndex(), antsVector );
}
AntsImage<ANTsFieldType> out_ants_image = { antsField };
return out_ants_image;
}
void local_integrateVelocityField(nb::module_ &m)
{
m.def("integrateVelocityFieldD2", &integrateVelocityField<2>);
m.def("integrateVelocityFieldD3", &integrateVelocityField<3>);
}