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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/*=========================================================================
*
* 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 "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkResampleImageFilter.h"
#include "itkBSplineTransform.h"
#include "itkTestingMacros.h"
#include <fstream>
// The following section of code implements a Command observer
// used to monitor the evolution of the registration process.
//
class CommandProgressUpdate : public itk::Command
{
public:
using Self = CommandProgressUpdate;
using Superclass = itk::Command;
using Pointer = itk::SmartPointer<Self>;
itkNewMacro(Self);
protected:
CommandProgressUpdate() = default;
public:
void
Execute(itk::Object * caller, const itk::EventObject & event) override
{
Execute((const itk::Object *)caller, event);
}
void
Execute(const itk::Object * object, const itk::EventObject & event) override
{
const auto * filter = dynamic_cast<const itk::ProcessObject *>(object);
if (!itk::ProgressEvent().CheckEvent(&event))
{
return;
}
std::cout << filter->GetProgress() << std::endl;
}
};
template <unsigned int VSplineOrder>
class BSplineTransformTest3Helper
{
public:
static int
RunTest(int argc, char * argv[])
{
constexpr unsigned int ImageDimension = 2;
using PixelType = unsigned char;
using FixedImageType = itk::Image<PixelType, ImageDimension>;
using MovingImageType = itk::Image<PixelType, ImageDimension>;
using FixedReaderType = itk::ImageFileReader<FixedImageType>;
using MovingReaderType = itk::ImageFileReader<MovingImageType>;
auto fixedReader = FixedReaderType::New();
fixedReader->SetFileName(argv[2]);
ITK_TRY_EXPECT_NO_EXCEPTION(fixedReader->Update());
auto movingReader = MovingReaderType::New();
movingReader->SetFileName(argv[3]);
typename FixedImageType::ConstPointer fixedImage = fixedReader->GetOutput();
using FilterType = itk::ResampleImageFilter<MovingImageType, FixedImageType>;
auto resampler = FilterType::New();
using InterpolatorType = itk::LinearInterpolateImageFunction<MovingImageType, double>;
auto interpolator = InterpolatorType::New();
resampler->SetInterpolator(interpolator);
typename FixedImageType::SpacingType fixedSpacing = fixedImage->GetSpacing();
typename FixedImageType::PointType fixedOrigin = fixedImage->GetOrigin();
typename FixedImageType::DirectionType fixedDirection = fixedImage->GetDirection();
resampler->SetOutputSpacing(fixedSpacing);
resampler->SetOutputOrigin(fixedOrigin);
resampler->SetOutputDirection(fixedDirection);
typename FixedImageType::RegionType fixedRegion = fixedImage->GetBufferedRegion();
typename FixedImageType::SizeType fixedSize = fixedRegion.GetSize();
resampler->SetSize(fixedSize);
resampler->SetOutputStartIndex(fixedRegion.GetIndex());
resampler->SetInput(movingReader->GetOutput());
const unsigned int SpaceDimension = ImageDimension;
using CoordinateRepType = double;
using TransformType = itk::BSplineTransform<CoordinateRepType, SpaceDimension, VSplineOrder>;
auto bsplineTransform = TransformType::New();
using RegionType = typename TransformType::RegionType;
RegionType bsplineRegion;
typename RegionType::SizeType size;
const unsigned int numberOfGridNodesOutsideTheImageSupport = VSplineOrder;
constexpr unsigned int numberOfGridNodesInsideTheImageSupport = 5;
const unsigned int numberOfGridNodes =
numberOfGridNodesInsideTheImageSupport + numberOfGridNodesOutsideTheImageSupport;
const unsigned int numberOfGridCells = numberOfGridNodesInsideTheImageSupport - 1;
size.Fill(numberOfGridNodes);
bsplineRegion.SetSize(size);
using MeshSizeType = typename TransformType::MeshSizeType;
MeshSizeType meshSize;
meshSize.Fill(numberOfGridCells);
using PhysicalDimensionsType = typename TransformType::PhysicalDimensionsType;
PhysicalDimensionsType fixedDimensions;
for (unsigned int d = 0; d < ImageDimension; ++d)
{
fixedDimensions[d] = fixedSpacing[d] * (fixedSize[d] - 1.0);
}
bsplineTransform->SetTransformDomainOrigin(fixedOrigin);
bsplineTransform->SetTransformDomainDirection(fixedDirection);
bsplineTransform->SetTransformDomainPhysicalDimensions(fixedDimensions);
bsplineTransform->SetTransformDomainMeshSize(meshSize);
using ParametersType = typename TransformType::ParametersType;
const unsigned int numberOfParameters = bsplineTransform->GetNumberOfParameters();
const unsigned int numberOfNodes = numberOfParameters / SpaceDimension;
ParametersType parameters(numberOfParameters);
std::ifstream infile;
infile.open(argv[1]);
for (unsigned int n = 0; n < numberOfNodes; ++n)
{
infile >> parameters[n];
infile >> parameters[n + numberOfNodes];
}
infile.close();
bsplineTransform->SetParameters(parameters);
auto observer = CommandProgressUpdate::New();
resampler->AddObserver(itk::ProgressEvent(), observer);
resampler->SetTransform(bsplineTransform);
ITK_TRY_EXPECT_NO_EXCEPTION(itk::WriteImage(resampler->GetOutput(), argv[4]));
using VectorType = itk::Vector<float, ImageDimension>;
using DeformationFieldType = itk::Image<VectorType, ImageDimension>;
auto field = DeformationFieldType::New();
field->SetRegions(fixedRegion);
field->SetOrigin(fixedOrigin);
field->SetSpacing(fixedSpacing);
field->Allocate();
using FieldIterator = itk::ImageRegionIterator<DeformationFieldType>;
FieldIterator fi(field, fixedRegion);
fi.GoToBegin();
typename TransformType::InputPointType fixedPoint;
typename TransformType::OutputPointType movingPoint;
typename DeformationFieldType::IndexType index;
VectorType displacement;
while (!fi.IsAtEnd())
{
index = fi.GetIndex();
field->TransformIndexToPhysicalPoint(index, fixedPoint);
movingPoint = bsplineTransform->TransformPoint(fixedPoint);
displacement[0] = movingPoint[0] - fixedPoint[0];
displacement[1] = movingPoint[1] - fixedPoint[1];
fi.Set(displacement);
++fi;
}
if (argc >= 6)
{
ITK_TRY_EXPECT_NO_EXCEPTION(itk::WriteImage(field, argv[5]));
}
return EXIT_SUCCESS;
}
};
int
itkBSplineTransformTest3(int argc, char * argv[])
{
if (argc < 7)
{
std::cerr << "Missing parameters." << std::endl;
std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv);
std::cerr << " coefficientsFile fixedImage ";
std::cerr << "movingImage deformedMovingImage" << std::endl;
std::cerr << "[deformationField][multithreader use #threads]" << std::endl;
return EXIT_FAILURE;
}
const int numberOfThreads = std::stoi(argv[6]);
int status = 0;
switch (numberOfThreads)
{
case 0:
{
// Don't invoke MultiThreader at all.
status |= BSplineTransformTest3Helper<3>::RunTest(argc, argv);
break;
}
default:
{
// Use MultiThreader with argv[6] threads
itk::MultiThreaderBase::SetGlobalDefaultNumberOfThreads(numberOfThreads);
itk::MultiThreaderBase::SetGlobalMaximumNumberOfThreads(numberOfThreads);
status |= BSplineTransformTest3Helper<3>::RunTest(argc, argv);
break;
}
}
if (status)
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|