Skip to content

Commit

Permalink
COMP: Support VectorImage input to DisplacementFieldTransform
Browse files Browse the repository at this point in the history
And in Python.

Co-authored-by: Matt McCormick <matt.mccormick@kitware.com>
Co-authored-by: Tom Birdsong <tom.birdsong@kitware.com>
  • Loading branch information
3 people committed Apr 1, 2022
1 parent 9f00f9d commit 447b82b
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkTransform.h"

#include "itkImage.h"
#include "itkVectorImage.h"
#include "itkMatrixOffsetTransformBase.h"
#include "itkImageVectorOptimizerParametersHelper.h"
#include "itkVectorInterpolateImageFunction.h"
Expand Down Expand Up @@ -157,6 +158,7 @@ class ITK_TEMPLATE_EXPORT DisplacementFieldTransform : public Transform<TParamet

/** Define the displacement field type and corresponding interpolator type. */
using DisplacementFieldType = Image<OutputVectorType, Dimension>;
using VectorImageDisplacementFieldType = VectorImage<TParametersValueType, Dimension>;
using DisplacementFieldPointer = typename DisplacementFieldType::Pointer;
using DisplacementFieldConstPointer = typename DisplacementFieldType::ConstPointer;

Expand All @@ -181,6 +183,9 @@ class ITK_TEMPLATE_EXPORT DisplacementFieldTransform : public Transform<TParamet
* container. */
virtual void
SetDisplacementField(DisplacementFieldType * field);
virtual void
SetDisplacementField(VectorImageDisplacementFieldType * field);
virtual void SetDisplacementField(std::nullptr_t) = delete;
itkGetModifiableObjectMacro(DisplacementField, DisplacementFieldType);

/** Get/Set the inverse displacement field. This must be supplied by the user for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "itkImageRegionIteratorWithIndex.h"
#include "vnl/algo/vnl_symmetric_eigensystem.h"
#include "vnl/algo/vnl_matrix_inverse.h"
#include "itkCastImageFilter.h"

namespace itk
{
Expand Down Expand Up @@ -342,6 +343,18 @@ DisplacementFieldTransform<TParametersValueType, VDimension>::SetDisplacementFie
this->SetFixedParametersFromDisplacementField();
}

template <typename TParametersValueType, unsigned int VDimension>
void
DisplacementFieldTransform<TParametersValueType, VDimension>::SetDisplacementField(
VectorImageDisplacementFieldType * field)
{
using CasterType = CastImageFilter<VectorImageDisplacementFieldType, DisplacementFieldType>;
auto caster = CasterType::New();
caster->SetInput(field);
caster->Update();
this->SetDisplacementField(caster->GetOutput());
}

template <typename TParametersValueType, unsigned int VDimension>
void
DisplacementFieldTransform<TParametersValueType, VDimension>::SetInverseDisplacementField(
Expand Down Expand Up @@ -477,7 +490,7 @@ DisplacementFieldTransform<TParametersValueType, VDimension>::SetFixedParameters
}
if (nullState)
{
this->SetDisplacementField(nullptr);
this->SetDisplacementField(static_cast<DisplacementFieldType *>(nullptr));
this->SetInverseDisplacementField(nullptr);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class ITK_TEMPLATE_EXPORT VelocityFieldTransform : public DisplacementFieldTrans
using typename Superclass::DisplacementFieldType;
using DisplacementFieldPointer = typename DisplacementFieldType::Pointer;

/** Define alternate displacement field interface. */
using typename Superclass::VectorImageDisplacementFieldType;

/** Define the displacement field type and corresponding interpolator type. */
using VelocityFieldType = Image<OutputVectorType, VelocityFieldDimension>;
using VelocityFieldPointer = typename VelocityFieldType::Pointer;
Expand All @@ -112,6 +115,8 @@ class ITK_TEMPLATE_EXPORT VelocityFieldTransform : public DisplacementFieldTrans
using OptimizerParametersHelperType =
ImageVectorOptimizerParametersHelper<ScalarType, Dimension, VelocityFieldDimension>;

using Superclass::SetDisplacementField;

/** Get/Set the velocity field.
* Set the displacement field. Create special set accessor to update
* interpolator and assign displacement field to transform parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ itkDisplacementFieldTransformTest(int argc, char * argv[])
displacementTransform->SetDisplacementField(displacementField);
ITK_TEST_SET_GET_VALUE(displacementField, displacementTransform->GetDisplacementField());

DisplacementTransformType::VectorImageDisplacementFieldType::Pointer vectorImageDisplacementField =
DisplacementTransformType::VectorImageDisplacementFieldType::New();
displacementTransform->SetDisplacementField(vectorImageDisplacementField);

DisplacementTransformType::DisplacementFieldType::Pointer inverseDisplacementField =
DisplacementTransformType::DisplacementFieldType::New();
displacementTransform->SetInverseDisplacementField(inverseDisplacementField);
Expand Down Expand Up @@ -627,7 +631,7 @@ itkDisplacementFieldTransformTest(int argc, char * argv[])

displacementTransform->SetIdentity();

displacementTransform->SetDisplacementField(nullptr);
displacementTransform->SetDisplacementField(static_cast<DisplacementFieldType *>(nullptr));
displacementTransform->SetInverseDisplacementField(nullptr);

// Check setting all zero for fixed parameters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if(ITK_WRAP_PYTHON)
itk_python_add_test(NAME itkDisplacementFieldTransformPythonTest
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/itkDisplacementFieldTransformTest.py)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# ==========================================================================
#
# Copyright NumFOCUS
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ==========================================================================*/
import itk
itk.auto_progress(2)

ScalarType = itk.F
VectorDimension = 2
VectorType = itk.Vector[ScalarType,VectorDimension]

ImageDimension = 2
ImageType = itk.Image[VectorType, ImageDimension]

image_size = [10, 10]

transform = itk.DisplacementFieldTransform[ScalarType, ImageDimension].New()

# Test setting image of vectors
pixel_value = 5
image = ImageType.New()
image.SetRegions(image_size)
image.Allocate()
image.FillBuffer([pixel_value] * VectorDimension)

transform.SetDisplacementField(image)

# Verify all parameters match expected value
for value in list(transform.GetParameters()):
assert value == pixel_value

# Test setting vector image
pixel_value = 20
vector_image = itk.VectorImage[ScalarType, ImageDimension].New()
vector_image.SetRegions(image_size)
vector_image.SetVectorLength(VectorDimension)
vector_image.Allocate()
pixel_default = itk.VariableLengthVector[ScalarType]()
pixel_default.SetSize(VectorDimension)
pixel_default.Fill(pixel_value)
vector_image.FillBuffer(pixel_default)

transform.SetDisplacementField(vector_image)

# Verify all parameters match expected value
for value in list(transform.GetParameters()):
assert value == pixel_value
2 changes: 1 addition & 1 deletion Wrapping/WrapITKTypes.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ set(itk_Wrap_Image ${WRAPPER_TEMPLATES})
WRAP_TYPE("itk::VectorImage" "VI" "itkVectorImage.h")
# Make a list of all of the selected image pixel types and also uchar
# (for 8-bit image output)
UNIQUE(wrap_image_types "${WRAP_ITK_COMPLEX_REAL};${WRAP_ITK_SCALAR};UC")
UNIQUE(wrap_image_types "${WRAP_ITK_COMPLEX_REAL};${WRAP_ITK_SCALAR};UC;D")

foreach(d ${ITK_WRAP_IMAGE_DIMS})
foreach(type ${wrap_image_types})
Expand Down

0 comments on commit 447b82b

Please sign in to comment.