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
|
/*=========================================================================
*
* 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
*
* https://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.
*
*=========================================================================*/
#include "itkPointSet.h"
#include "itkBSplineScatteredDataPointSetToImageFilter.h"
#include "itkBSplineTransform.h"
#include "itkVectorIndexSelectionCastImageFilter.h"
#include "itkVectorLinearInterpolateImageFunction.h"
#include "itkTestingMacros.h"
/**
* In this test, we approximate a 2-D scalar field.
* The scattered data is derived from a segmented
* image. We write the output to an image for
* comparison.
*/
int
itkBSplineScatteredDataPointSetToImageFilterTest4(int, char *[])
{
constexpr unsigned int ParametricDimension = 3;
constexpr unsigned int DataDimension = 3;
constexpr unsigned int SplineOrder = 3;
using RealType = float;
using VectorType = itk::Vector<RealType, DataDimension>;
using VectorImageType = itk::Image<VectorType, ParametricDimension>;
using PointSetType = itk::PointSet<VectorImageType::PixelType, ParametricDimension>;
using PointType = PointSetType::PointType;
// Instantiate the B-spline filter
using FilterType = itk::BSplineScatteredDataPointSetToImageFilter<PointSetType, VectorImageType>;
VectorImageType::SizeType size;
size.Fill(100);
VectorImageType::PointType origin;
origin.Fill(0);
VectorImageType::SpacingType spacing;
spacing.Fill(1);
VectorImageType::DirectionType direction;
direction.SetIdentity();
// Instantiate example corresponding points with relative weighting
auto pointSet = PointSetType::New();
pointSet->Initialize();
using WeightsContainerType = FilterType::WeightsContainerType;
auto weights = WeightsContainerType::New();
weights->Initialize();
// Create first landmark pair and weights
PointType landmarkInFirstImage1;
PointType landmarkInSecondImage1;
landmarkInFirstImage1[0] = 4.0;
landmarkInFirstImage1[1] = 4.0;
landmarkInFirstImage1[2] = 4.0;
landmarkInSecondImage1[0] = 5.0;
landmarkInSecondImage1[1] = 5.0;
landmarkInSecondImage1[2] = 5.0;
RealType weight1 = 1.0;
weights->InsertElement(0, weight1);
VectorType vector1;
for (unsigned int d = 0; d < DataDimension; ++d)
{
vector1[d] = landmarkInSecondImage1[d] - landmarkInFirstImage1[d];
}
pointSet->SetPoint(0, landmarkInFirstImage1);
pointSet->SetPointData(0, vector1);
// Create second landmark pair
PointType landmarkInFirstImage2;
PointType landmarkInSecondImage2;
landmarkInFirstImage2[0] = 25.0;
landmarkInFirstImage2[1] = 25.0;
landmarkInFirstImage2[2] = 25.0;
landmarkInSecondImage2[0] = 30.0;
landmarkInSecondImage2[1] = 35.0;
landmarkInSecondImage2[2] = 45.0;
RealType weight2 = 3.0;
weights->InsertElement(1, weight2);
VectorType vector2;
for (unsigned int d = 0; d < DataDimension; ++d)
{
vector2[d] = landmarkInSecondImage2[d] - landmarkInFirstImage2[d];
}
pointSet->SetPoint(1, landmarkInFirstImage2);
pointSet->SetPointData(1, vector2);
// Create third landmark pair
PointType landmarkInFirstImage3;
PointType landmarkInSecondImage3;
landmarkInFirstImage3[0] = 25.0;
landmarkInFirstImage3[1] = 25.0;
landmarkInFirstImage3[2] = 25.0;
landmarkInSecondImage3[0] = 30.0;
landmarkInSecondImage3[1] = 35.0;
landmarkInSecondImage3[2] = 45.0;
RealType weight3 = 0.5;
weights->InsertElement(2, weight3);
VectorType vector3;
for (unsigned int d = 0; d < DataDimension; ++d)
{
vector3[d] = landmarkInSecondImage3[d] - landmarkInFirstImage3[d];
}
pointSet->SetPoint(2, landmarkInFirstImage3);
pointSet->SetPointData(2, vector3);
// Now fit the displacement
auto filter = FilterType::New();
ITK_EXERCISE_BASIC_OBJECT_METHODS(filter, BSplineScatteredDataPointSetToImageFilter, PointSetToImageFilter);
// Define the parametric domain.
filter->SetOrigin(origin);
filter->SetSpacing(spacing);
filter->SetSize(size);
filter->SetDirection(direction);
filter->SetInput(pointSet);
filter->SetPointWeights(weights);
// we normally don't need the sampled b-spline object
// so we should turn this off but since we want to
// compare, we need to have it on.
// we only need the control point lattice to
// set as input in the bspline transform.
filter->SetGenerateOutputImage(true);
// Define the rest of the B-spline parameters.
// Note that some are transferred to the B-spline
// transform. Specifically, this includes the final
// number of control points and the spline order.
filter->SetSplineOrder(SplineOrder);
FilterType::ArrayType ncps;
ncps.Fill(4);
filter->SetNumberOfControlPoints(ncps);
filter->SetNumberOfLevels(3);
FilterType::ArrayType close;
close.Fill(0);
filter->SetCloseDimension(close);
ITK_TRY_EXPECT_NO_EXCEPTION(filter->Update());
// Instantiate the BSpline transform
using TransformType = itk::BSplineTransform<float, DataDimension, SplineOrder>;
auto transform = TransformType::New();
using CoefficientImageType = TransformType::ImageType;
TransformType::CoefficientImageArray coefficientImages;
for (unsigned int j = 0; j < DataDimension; ++j)
{
using SelectorType = itk::VectorIndexSelectionCastImageFilter<VectorImageType, CoefficientImageType>;
auto selector = SelectorType::New();
selector->SetInput(filter->GetPhiLattice());
selector->SetIndex(j);
coefficientImages[j] = selector->GetOutput();
coefficientImages[j]->Update();
coefficientImages[j]->DisconnectPipeline();
}
transform->SetCoefficientImages(coefficientImages);
using InputPointType = TransformType::InputPointType;
using OutputPointType = TransformType::OutputPointType;
InputPointType inputPoint;
inputPoint.Fill(50.0);
OutputPointType outputPoint = transform->TransformPoint(inputPoint);
// Now instantiate an interpolator to get an approximation of what
// the transform should produce
using InterpolatorType = itk::VectorLinearInterpolateImageFunction<VectorImageType>;
auto interpolator = InterpolatorType::New();
interpolator->SetInputImage(filter->GetOutput());
VectorImageType::PointType testPoint;
testPoint.CastFrom(inputPoint);
VectorType vector = interpolator->Evaluate(testPoint);
RealType testDistance = vector.GetNorm();
RealType approximateDistance = inputPoint.EuclideanDistanceTo(outputPoint);
VectorImageType::PointType approximateOutputPoint;
for (unsigned int d = 0; d < DataDimension; ++d)
{
approximateOutputPoint[d] = testPoint[d] + vector[d];
}
std::cout << "Input point " << inputPoint << std::endl;
std::cout << "True output point " << outputPoint << std::endl;
std::cout << "Approximate output point " << approximateOutputPoint << std::endl;
if (itk::Math::abs(testDistance - approximateDistance) > 0.0001)
{
std::cerr << "Didn't return correct distance." << std::endl;
std::cerr << "true distance: " << testDistance << std::endl;
std::cerr << "Approximate distance: " << approximateDistance << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|