-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from ANTsX/InvertField
Invert field
- Loading branch information
Showing
6 changed files
with
159 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include <exception> | ||
#include <vector> | ||
#include <string> | ||
|
||
#include "itkImage.h" | ||
#include "itkVector.h" | ||
#include "itkInvertDisplacementFieldImageFilter.h" | ||
|
||
#include "LOCAL_antsImage.h" | ||
|
||
namespace py = pybind11; | ||
|
||
template <unsigned int Dimension> | ||
py::capsule invertDisplacementField( py::capsule & antsDisplacementField, | ||
py::capsule & antsInverseFieldInitialEstimate, | ||
unsigned int maximumNumberOfIterations, | ||
float meanErrorToleranceThreshold, | ||
float maxErrorToleranceThreshold, | ||
bool enforceBoundaryCondition ) | ||
{ | ||
using RealType = float; | ||
|
||
using ANTsFieldType = itk::VectorImage<RealType, Dimension>; | ||
using ANTsFieldPointerType = typename ANTsFieldType::Pointer; | ||
|
||
using VectorType = itk::Vector<RealType, Dimension>; | ||
|
||
using ITKFieldType = itk::Image<VectorType, Dimension>; | ||
using ITKFieldPointerType = typename ITKFieldType::Pointer; | ||
|
||
using IteratorType = itk::ImageRegionConstIteratorWithIndex<ITKFieldType>; | ||
|
||
ITKFieldPointerType itkDisplacementField = as< ITKFieldType >( antsDisplacementField ); | ||
ITKFieldPointerType itkInverseFieldInitialEstimate = as< ITKFieldType >( antsInverseFieldInitialEstimate ); | ||
|
||
typedef itk::InvertDisplacementFieldImageFilter<ITKFieldType> InverterType; | ||
typename InverterType::Pointer inverter = InverterType::New(); | ||
|
||
inverter->SetInput( itkDisplacementField ); | ||
inverter->SetInverseFieldInitialEstimate( itkInverseFieldInitialEstimate ); | ||
inverter->SetMaximumNumberOfIterations( maximumNumberOfIterations ); | ||
inverter->SetMeanErrorToleranceThreshold( meanErrorToleranceThreshold ); | ||
inverter->SetMaxErrorToleranceThreshold( maxErrorToleranceThreshold ); | ||
inverter->SetEnforceBoundaryCondition( enforceBoundaryCondition ); | ||
|
||
////////////////////////// | ||
// | ||
// Now convert back to vector image type. | ||
// | ||
|
||
ANTsFieldPointerType antsField = ANTsFieldType::New(); | ||
antsField->CopyInformation( inverter->GetOutput() ); | ||
antsField->SetRegions( inverter->GetOutput()->GetRequestedRegion() ); | ||
antsField->SetVectorLength( Dimension ); | ||
antsField->Allocate(); | ||
|
||
IteratorType ItI( inverter->GetOutput(), | ||
inverter->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 ); | ||
} | ||
|
||
return wrap< ANTsFieldType >( antsField ); | ||
} | ||
|
||
PYBIND11_MODULE(invertDisplacementField, m) | ||
{ | ||
m.def("invertDisplacementFieldF2", &invertDisplacementField<2>); | ||
m.def("invertDisplacementFieldF3", &invertDisplacementField<3>); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
__all__ = ['invert_displacement_field'] | ||
|
||
from ..core import ants_image as iio | ||
from .. import utils | ||
|
||
|
||
def invert_displacement_field(displacement_field, | ||
inverse_field_initial_estimate, | ||
maximum_number_of_iterations=20, | ||
mean_error_tolerance_threshold=0.001, | ||
max_error_tolerance_threshold=0.1, | ||
enforce_boundary_condition=True): | ||
""" | ||
Invert displacement field. | ||
Arguments | ||
--------- | ||
displacement_field : ANTsImage displacement field | ||
displacement field | ||
inverse_field_initial_estimate : ANTsImage displacement field | ||
initial guess | ||
maximum_number_of_iterations : integer | ||
number of iterations | ||
mean_error_tolerance_threshold : float | ||
mean error tolerance threshold | ||
max_error_tolerance_threshold : float | ||
max error tolerance threshold | ||
enforce_boundary_condition : bool | ||
enforce stationary boundary condition | ||
Example | ||
------- | ||
>>> import ants | ||
""" | ||
|
||
libfn = utils.get_lib_fn('invertDisplacementFieldF%i' % displacement_field.dimension) | ||
inverse_field = libfn(displacement_field.pointer, inverse_field_initial_estimate.pointer, | ||
maximum_number_of_iterations, mean_error_tolerance_threshold, | ||
max_error_tolerance_threshold, enforce_boundary_condition) | ||
|
||
new_image = iio.ANTsImage(pixeltype='float', dimension=displacement_field.dimension, | ||
components=displacement_field.dimension, pointer=inverse_field).clone('float') | ||
return new_image | ||
|
||
|