ImageRegistration2.cxx 20.3 KB
Newer Older
Jordi Inglada's avatar
Jordi Inglada committed
1 2
/*=========================================================================

3
  Program:   ORFEO Toolbox
Jordi Inglada's avatar
Jordi Inglada committed
4
  Language:  C++
5 6 7 8 9 10
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.
Jordi Inglada's avatar
Jordi Inglada committed
11 12


13 14
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Jordi Inglada's avatar
Jordi Inglada committed
15 16 17
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
18

Jordi Inglada's avatar
Jordi Inglada committed
19 20

//  Software Guide : BeginCommandLineArgs
21 22
//    INPUTS: {RamsesROISmall.png}, {ADS40RoiSmall.png}
//    OUTPUTS: {ImageRegistration2Output.png}, {ImageRegistration2CheckerboardBefore.png}, {ImageRegistration2CheckerboardAfter.png}
Jordi Inglada's avatar
Jordi Inglada committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
//  Software Guide : EndCommandLineArgs

// Software Guide : BeginLatex
//
// The following simple example illustrates how multiple imaging modalities can
// be registered using the ITK registration framework. The first difference
// between this and previous examples is the use of the
// \doxygen{itk}{MutualInformationImageToImageMetric} as the cost-function to be
// optimized. The second difference is the use of the
// \doxygen{itk}{GradientDescentOptimizer}. Due to the stochastic nature of the
// metric computation, the values are too noisy to work successfully with the
// \doxygen{itk}{RegularStepGradientDescentOptimizer}.  Therefore, we will use the
// simpler GradientDescentOptimizer with a user defined learning rate.  The
// following headers declare the basic components of this registration method.
//
38
// Software Guide : EndLatex
39
#include "itkUnaryFunctorImageFilter.h"
Jordi Inglada's avatar
Jordi Inglada committed
40 41 42 43 44 45 46 47 48
// Software Guide : BeginCodeSnippet
#include "itkImageRegistrationMethod.h"
#include "itkTranslationTransform.h"
#include "itkMutualInformationImageToImageMetric.h"
#include "itkGradientDescentOptimizer.h"
#include "otbImage.h"
// Software Guide : EndCodeSnippet

//  Software Guide : BeginLatex
49
//
Jordi Inglada's avatar
Jordi Inglada committed
50 51 52 53 54 55
//  One way to simplify the computation of the mutual information is
//  to normalize the statistical distribution of the two input images. The
//  \doxygen{itk}{NormalizeImageFilter} is the perfect tool for this task.
//  It rescales the intensities of the input images in order to produce an
//  output image with zero mean and unit variance.
//
56
//  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
57 58 59 60 61 62

// Software Guide : BeginCodeSnippet
#include "itkNormalizeImageFilter.h"
// Software Guide : EndCodeSnippet

//  Software Guide : BeginLatex
63
//
Jordi Inglada's avatar
Jordi Inglada committed
64 65 66 67 68 69
//  Additionally, low-pass filtering of the images to be registered will also
//  increase robustness against noise. In this example, we will use the
//  \doxygen{itk}{DiscreteGaussianImageFilter} for that purpose. The
//  characteristics of this filter have been discussed in Section
//  \ref{sec:BlurringFilters}.
//
70
//  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

// Software Guide : BeginCodeSnippet
#include "itkDiscreteGaussianImageFilter.h"
// Software Guide : EndCodeSnippet

#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"

#include "itkResampleImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkCheckerBoardImageFilter.h"

//  The following section of code implements a Command observer
//  that will monitor the evolution of the registration process.
//
#include "itkCommand.h"
87
class CommandIterationUpdate : public itk::Command
Jordi Inglada's avatar
Jordi Inglada committed
88 89
{
public:
OTB Bot's avatar
STYLE  
OTB Bot committed
90 91 92 93
  typedef  CommandIterationUpdate  Self;
  typedef  itk::Command            Superclass;
  typedef  itk::SmartPointer<Self> Pointer;
  itkNewMacro(Self);
Jordi Inglada's avatar
Jordi Inglada committed
94
protected:
OTB Bot's avatar
STYLE  
OTB Bot committed
95
  CommandIterationUpdate() {}
Jordi Inglada's avatar
Jordi Inglada committed
96
public:
OTB Bot's avatar
STYLE  
OTB Bot committed
97 98
  typedef   itk::GradientDescentOptimizer OptimizerType;
  typedef   const OptimizerType *         OptimizerPointer;
Jordi Inglada's avatar
Jordi Inglada committed
99

100
  void Execute(itk::Object *caller, const itk::EventObject& event) override
101
  {
OTB Bot's avatar
STYLE  
OTB Bot committed
102
    Execute((const itk::Object *) caller, event);
103
  }
Jordi Inglada's avatar
Jordi Inglada committed
104

105
  void Execute(const itk::Object * object, const itk::EventObject& event) override
106 107
  {
    OptimizerPointer optimizer =
OTB Bot's avatar
STYLE  
OTB Bot committed
108 109 110
      dynamic_cast<OptimizerPointer>(object);
    if (!itk::IterationEvent().CheckEvent(&event))
      {
111
      return;
OTB Bot's avatar
STYLE  
OTB Bot committed
112
      }
113 114 115 116
    std::cout << optimizer->GetCurrentIteration() << "   ";
    std::cout << optimizer->GetValue() << "   ";
    std::cout << optimizer->GetCurrentPosition() << std::endl;
  }
Jordi Inglada's avatar
Jordi Inglada committed
117 118
};

OTB Bot's avatar
STYLE  
OTB Bot committed
119
int main(int argc, char *argv[])
Jordi Inglada's avatar
Jordi Inglada committed
120
{
OTB Bot's avatar
STYLE  
OTB Bot committed
121 122
  if (argc < 4)
    {
Jordi Inglada's avatar
Jordi Inglada committed
123 124 125 126 127 128
    std::cerr << "Missing Parameters " << std::endl;
    std::cerr << "Usage: " << argv[0];
    std::cerr << " fixedImageFile  movingImageFile ";
    std::cerr << "outputImagefile ";
    std::cerr << "[checkerBoardBefore] [checkerBoardAfter]" << std::endl;
    return 1;
OTB Bot's avatar
STYLE  
OTB Bot committed
129
    }
130

Jordi Inglada's avatar
Jordi Inglada committed
131 132
  // Software Guide : BeginLatex
  //
133 134 135
  // The moving and fixed images types should be instantiated first.
  //
  // Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
136
  //
137
  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
138 139
  const unsigned int Dimension = 2;
  typedef  unsigned short PixelType;
140

OTB Bot's avatar
STYLE  
OTB Bot committed
141 142
  typedef otb::Image<PixelType, Dimension> FixedImageType;
  typedef otb::Image<PixelType, Dimension> MovingImageType;
Jordi Inglada's avatar
Jordi Inglada committed
143 144 145
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
146
  //
Jordi Inglada's avatar
Jordi Inglada committed
147 148 149 150 151
  //  It is convenient to work with an internal image type because mutual
  //  information will perform better on images with a normalized statistical
  //  distribution. The fixed and moving images will be normalized and
  //  converted to this internal type.
  //
152 153
  //  Software Guide : EndLatex

Jordi Inglada's avatar
Jordi Inglada committed
154
  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
155 156
  typedef float                                    InternalPixelType;
  typedef otb::Image<InternalPixelType, Dimension> InternalImageType;
Jordi Inglada's avatar
Jordi Inglada committed
157 158 159
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
160
  //
Jordi Inglada's avatar
Jordi Inglada committed
161 162 163 164
  //  The rest of the image registration components are instantiated as
  //  illustrated in Section \ref{sec:IntroductionImageRegistration} with
  //  the use of the \code{InternalImageType}.
  //
165
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
166 167

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
168 169
  typedef itk::TranslationTransform<double, Dimension> TransformType;
  typedef itk::GradientDescentOptimizer                OptimizerType;
Jordi Inglada's avatar
Jordi Inglada committed
170

OTB Bot's avatar
STYLE  
OTB Bot committed
171
  typedef itk::LinearInterpolateImageFunction<InternalImageType,
OTB Bot's avatar
STYLE  
OTB Bot committed
172
      double> InterpolatorType;
OTB Bot's avatar
STYLE  
OTB Bot committed
173 174

  typedef itk::ImageRegistrationMethod<InternalImageType,
OTB Bot's avatar
STYLE  
OTB Bot committed
175
      InternalImageType>  RegistrationType;
OTB Bot's avatar
STYLE  
OTB Bot committed
176
  // Software Guide : EndCodeSnippet
Jordi Inglada's avatar
Jordi Inglada committed
177 178

  //  Software Guide : BeginLatex
179
  //
Jordi Inglada's avatar
Jordi Inglada committed
180 181 182
  //  The mutual information metric type is instantiated using the image
  //  types.
  //
183
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
184 185

  // Software Guide : BeginCodeSnippet
186
  typedef itk::MutualInformationImageToImageMetric<
OTB Bot's avatar
STYLE  
OTB Bot committed
187 188
      InternalImageType,
      InternalImageType>    MetricType;
Jordi Inglada's avatar
Jordi Inglada committed
189 190
  // Software Guide : EndCodeSnippet

OTB Bot's avatar
STYLE  
OTB Bot committed
191 192 193 194
  TransformType::Pointer    transform     = TransformType::New();
  OptimizerType::Pointer    optimizer     = OptimizerType::New();
  InterpolatorType::Pointer interpolator  = InterpolatorType::New();
  RegistrationType::Pointer registration  = RegistrationType::New();
Jordi Inglada's avatar
Jordi Inglada committed
195

OTB Bot's avatar
STYLE  
OTB Bot committed
196 197 198
  registration->SetOptimizer(optimizer);
  registration->SetTransform(transform);
  registration->SetInterpolator(interpolator);
Jordi Inglada's avatar
Jordi Inglada committed
199 200

  //  Software Guide : BeginLatex
201
  //
Jordi Inglada's avatar
Jordi Inglada committed
202 203 204
  //  The metric is created using the \code{New()} method and then
  //  connected to the registration object.
  //
205
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
206 207

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
208 209
  MetricType::Pointer metric        = MetricType::New();
  registration->SetMetric(metric);
Jordi Inglada's avatar
Jordi Inglada committed
210 211 212
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
213
  //
Jordi Inglada's avatar
Jordi Inglada committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227
  //  The metric requires a number of parameters to be selected, including
  //  the standard deviation of the Gaussian kernel for the fixed image
  //  density estimate, the standard deviation of the kernel for the moving
  //  image density and the number of samples use to compute the densities
  //  and entropy values. Details on the concepts behind the computation of
  //  the metric can be found in Section
  //  \ref{sec:MutualInformationMetric}.  Experience has
  //  shown that a kernel standard deviation of $0.4$ works well for images
  //  which have been normalized to a mean of zero and unit variance.  We
  //  will follow this empirical rule in this example.
  //
  //  \index{itk::Mutual\-Information\-Image\-To\-Image\-Metric!SetFixedImageStandardDeviation()}
  //  \index{itk::Mutual\-Information\-Image\-To\-Image\-Metric!SetMovingImageStandardDeviation()}
  //
228
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
229 230

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
231 232
  metric->SetFixedImageStandardDeviation(0.4);
  metric->SetMovingImageStandardDeviation(0.4);
Jordi Inglada's avatar
Jordi Inglada committed
233 234
  // Software Guide : EndCodeSnippet

OTB Bot's avatar
STYLE  
OTB Bot committed
235 236
  typedef otb::ImageFileReader<FixedImageType>  FixedImageReaderType;
  typedef otb::ImageFileReader<MovingImageType> MovingImageReaderType;
Jordi Inglada's avatar
Jordi Inglada committed
237 238 239 240

  FixedImageReaderType::Pointer  fixedImageReader  = FixedImageReaderType::New();
  MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();

OTB Bot's avatar
STYLE  
OTB Bot committed
241 242
  fixedImageReader->SetFileName(argv[1]);
  movingImageReader->SetFileName(argv[2]);
Jordi Inglada's avatar
Jordi Inglada committed
243 244

  //  Software Guide : BeginLatex
245
  //
Jordi Inglada's avatar
Jordi Inglada committed
246 247 248
  //  The normalization filters are instantiated using the fixed and moving
  //  image types as input and the internal image type as output.
  //
249
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
250 251

  // Software Guide : BeginCodeSnippet
252
  typedef itk::NormalizeImageFilter<
OTB Bot's avatar
STYLE  
OTB Bot committed
253 254 255
      FixedImageType,
      InternalImageType
      > FixedNormalizeFilterType;
Jordi Inglada's avatar
Jordi Inglada committed
256

257
  typedef itk::NormalizeImageFilter<
OTB Bot's avatar
STYLE  
OTB Bot committed
258 259 260
      MovingImageType,
      InternalImageType
      > MovingNormalizeFilterType;
Jordi Inglada's avatar
Jordi Inglada committed
261

262
  FixedNormalizeFilterType::Pointer fixedNormalizer =
263
    FixedNormalizeFilterType::New();
Jordi Inglada's avatar
Jordi Inglada committed
264 265

  MovingNormalizeFilterType::Pointer movingNormalizer =
266
    MovingNormalizeFilterType::New();
Jordi Inglada's avatar
Jordi Inglada committed
267 268 269
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
270
  //
Jordi Inglada's avatar
Jordi Inglada committed
271 272 273 274
  //  The blurring filters are declared using the internal image type as both
  //  the input and output types. In this example, we will set the variance
  //  for both blurring filters to $2.0$.
  //
275
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
276 277 278

  // Software Guide : BeginCodeSnippet
  typedef itk::DiscreteGaussianImageFilter<
OTB Bot's avatar
STYLE  
OTB Bot committed
279 280 281
      InternalImageType,
      InternalImageType
      > GaussianFilterType;
282

Jordi Inglada's avatar
Jordi Inglada committed
283 284 285
  GaussianFilterType::Pointer fixedSmoother  = GaussianFilterType::New();
  GaussianFilterType::Pointer movingSmoother = GaussianFilterType::New();

OTB Bot's avatar
STYLE  
OTB Bot committed
286 287
  fixedSmoother->SetVariance(4.0);
  movingSmoother->SetVariance(4.0);
Jordi Inglada's avatar
Jordi Inglada committed
288 289 290
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
291
  //
Jordi Inglada's avatar
Jordi Inglada committed
292 293 294 295 296
  //  The output of the readers becomes the input to the normalization
  //  filters. The output of the normalization filters is connected as
  //  input to the blurring filters. The input to the registration method
  //  is taken from the blurring filters.
  //
297
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
298 299

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
300 301
  fixedNormalizer->SetInput(fixedImageReader->GetOutput());
  movingNormalizer->SetInput(movingImageReader->GetOutput());
Jordi Inglada's avatar
Jordi Inglada committed
302

OTB Bot's avatar
STYLE  
OTB Bot committed
303 304
  fixedSmoother->SetInput(fixedNormalizer->GetOutput());
  movingSmoother->SetInput(movingNormalizer->GetOutput());
Jordi Inglada's avatar
Jordi Inglada committed
305

OTB Bot's avatar
STYLE  
OTB Bot committed
306 307
  registration->SetFixedImage(fixedSmoother->GetOutput());
  registration->SetMovingImage(movingSmoother->GetOutput());
Jordi Inglada's avatar
Jordi Inglada committed
308 309 310 311
  // Software Guide : EndCodeSnippet

  fixedNormalizer->Update();
  FixedImageType::RegionType fixedImageRegion =
312
    fixedNormalizer->GetOutput()->GetBufferedRegion();
OTB Bot's avatar
STYLE  
OTB Bot committed
313
  registration->SetFixedImageRegion(fixedImageRegion);
Jordi Inglada's avatar
Jordi Inglada committed
314 315

  typedef RegistrationType::ParametersType ParametersType;
OTB Bot's avatar
STYLE  
OTB Bot committed
316
  ParametersType initialParameters(transform->GetNumberOfParameters());
Jordi Inglada's avatar
Jordi Inglada committed
317 318 319

  initialParameters[0] = 0.0;  // Initial offset in mm along X
  initialParameters[1] = 0.0;  // Initial offset in mm along Y
320

OTB Bot's avatar
STYLE  
OTB Bot committed
321
  registration->SetInitialTransformParameters(initialParameters);
Jordi Inglada's avatar
Jordi Inglada committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

  //  Software Guide : BeginLatex
  //
  //  We should now define the number of spatial samples to be considered in
  //  the metric computation. Note that we were forced to postpone this setting
  //  until we had done the preprocessing of the images because the number of
  //  samples is usually defined as a fraction of the total number of pixels in
  //  the fixed image.
  //
  //  The number of spatial samples can usually be as low as $1\%$ of the total
  //  number of pixels in the fixed image. Increasing the number of samples
  //  improves the smoothness of the metric from one iteration to another and
  //  therefore helps when this metric is used in conjunction with optimizers
  //  that rely of the continuity of the metric values. The trade-off, of
  //  course, is that a larger number of samples result in longer computation
337
  //  times per every evaluation of the metric.
Jordi Inglada's avatar
Jordi Inglada committed
338 339 340 341 342 343 344 345 346 347
  //
  //  It has been demonstrated empirically that the number of samples is not a
  //  critical parameter for the registration process. When you start fine
  //  tuning your own registration process, you should start using high values
  //  of number of samples, for example in the range of $20\%$ to $50\%$ of the
  //  number of pixels in the fixed image. Once you have succeeded to register
  //  your images you can then reduce the number of samples progressively until
  //  you find a good compromise on the time it takes to compute one evaluation
  //  of the Metric. Note that it is not useful to have very fast evaluations
  //  of the Metric if the noise in their values results in more iterations
348
  //  being required by the optimizer to converge.
Jordi Inglada's avatar
Jordi Inglada committed
349 350 351 352
  //
  //  \index{itk::Mutual\-Information\-Image\-To\-Image\-Metric!SetNumberOfSpatialSamples()}
  //  \index{itk::Mutual\-Information\-Image\-To\-Image\-Metric!Trade-offs}
  //
353
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
354 355 356

  // Software Guide : BeginCodeSnippet
  const unsigned int numberOfPixels = fixedImageRegion.GetNumberOfPixels();
357 358

  const unsigned int numberOfSamples =
OTB Bot's avatar
STYLE  
OTB Bot committed
359
    static_cast<unsigned int>(numberOfPixels * 0.01);
Jordi Inglada's avatar
Jordi Inglada committed
360

OTB Bot's avatar
STYLE  
OTB Bot committed
361
  metric->SetNumberOfSpatialSamples(numberOfSamples);
Jordi Inglada's avatar
Jordi Inglada committed
362 363 364
  // Software Guide : EndCodeSnippet

  //  Software Guide : BeginLatex
365
  //
Jordi Inglada's avatar
Jordi Inglada committed
366 367 368 369 370 371 372 373 374 375 376
  //  Since larger values of mutual information indicate better matches than
  //  smaller values, we need to maximize the cost function in this example.
  //  By default the GradientDescentOptimizer class is set to minimize the
  //  value of the cost-function. It is therefore necessary to modify its
  //  default behavior by invoking the \code{MaximizeOn()} method.
  //  Additionally, we need to define the optimizer's step size using the
  //  \code{SetLearningRate()} method.
  //
  //  \index{itk::Gradient\-Descent\-Optimizer!MaximizeOn()}
  //  \index{itk::Image\-Registration\-Method!Maximize vs Minimize}
  //
377
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
378 379

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
380 381
  optimizer->SetLearningRate(150.0);
  optimizer->SetNumberOfIterations(300);
Jordi Inglada's avatar
Jordi Inglada committed
382 383 384 385
  optimizer->MaximizeOn();
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
386
  //
Jordi Inglada's avatar
Jordi Inglada committed
387 388 389 390
  // Note that large values of the learning rate will make the optimizer
  // unstable. Small values, on the other hand, may result in the optimizer
  // needing too many iterations in order to walk to the extrema of the cost
  // function. The easy way of fine tuning this parameter is to start with
OTB Bot's avatar
STYLE  
OTB Bot committed
391
  // small values, probably in the range of $\{5.0, 10.0\}$. Once the other
Jordi Inglada's avatar
Jordi Inglada committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  // registration parameters have been tuned for producing convergence, you
  // may want to revisit the learning rate and start increasing its value until
  // you observe that the optimization becomes unstable.  The ideal value for
  // this parameter is the one that results in a minimum number of iterations
  // while still keeping a stable path on the parametric space of the
  // optimization. Keep in mind that this parameter is a multiplicative factor
  // applied on the gradient of the Metric. Therefore, its effect on the
  // optimizer step length is proportional to the Metric values themselves.
  // Metrics with large values will require you to use smaller values for the
  // learning rate in order to maintain a similar optimizer behavior.
  //
  // Software Guide : EndLatex

  // Create the Command observer and register it with the optimizer.
  //
  CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
OTB Bot's avatar
STYLE  
OTB Bot committed
408
  optimizer->AddObserver(itk::IterationEvent(), observer);
Jordi Inglada's avatar
Jordi Inglada committed
409

410
  try
OTB Bot's avatar
STYLE  
OTB Bot committed
411
    {
412
    registration->Update();
OTB Bot's avatar
STYLE  
OTB Bot committed
413 414 415
    }
  catch (itk::ExceptionObject& err)
    {
416 417
    std::cout << "ExceptionObject caught !" << std::endl;
    std::cout << err << std::endl;
Jordi Inglada's avatar
Jordi Inglada committed
418
    return -1;
OTB Bot's avatar
STYLE  
OTB Bot committed
419
    }
Jordi Inglada's avatar
Jordi Inglada committed
420 421

  ParametersType finalParameters = registration->GetLastTransformParameters();
422

Jordi Inglada's avatar
Jordi Inglada committed
423 424
  double TranslationAlongX = finalParameters[0];
  double TranslationAlongY = finalParameters[1];
425

Jordi Inglada's avatar
Jordi Inglada committed
426
  unsigned int numberOfIterations = optimizer->GetCurrentIteration();
427

Jordi Inglada's avatar
Jordi Inglada committed
428 429 430 431 432 433 434 435 436 437 438 439 440
  double bestValue = optimizer->GetValue();

  // Print out results
  //
  std::cout << std::endl;
  std::cout << "Result = " << std::endl;
  std::cout << " Translation X = " << TranslationAlongX  << std::endl;
  std::cout << " Translation Y = " << TranslationAlongY  << std::endl;
  std::cout << " Iterations    = " << numberOfIterations << std::endl;
  std::cout << " Metric value  = " << bestValue          << std::endl;
  std::cout << " Numb. Samples = " << numberOfSamples    << std::endl;

  //  Software Guide : BeginLatex
441
  //
Jordi Inglada's avatar
Jordi Inglada committed
442 443
  //  Let's execute this example over two of the images provided in
  //  \code{Examples/Data}:
444
  //
Jordi Inglada's avatar
Jordi Inglada committed
445
  //  \begin{itemize}
446
  //  \item \code{RamsesROISmall.png}
Jordi Inglada's avatar
Jordi Inglada committed
447 448 449 450 451
  //  \item \code{ADS40RoiSmall.png}
  //  \end{itemize}
  //
  //  \begin{figure}
  //  \center
452
  //  \includegraphics[width=0.44\textwidth]{RamsesROISmall.eps}
Jordi Inglada's avatar
Jordi Inglada committed
453 454 455 456 457 458 459 460
  //  \includegraphics[width=0.44\textwidth]{ADS40RoiSmall.eps}
  //  \itkcaption[Multi-Modality Registration Inputs]{A SAR image
  //  (fixed image) and an aerial
  //  photograph
  //  (moving image) are provided as input to the registration method.}
  //  \label{fig:FixedMovingImageRegistration2}
  //  \end{figure}
  //
461 462
  //
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
463

464
  typedef itk::ResampleImageFilter<
OTB Bot's avatar
STYLE  
OTB Bot committed
465 466
      MovingImageType,
      FixedImageType>    ResampleFilterType;
Jordi Inglada's avatar
Jordi Inglada committed
467 468 469

  TransformType::Pointer finalTransform = TransformType::New();

OTB Bot's avatar
STYLE  
OTB Bot committed
470
  finalTransform->SetParameters(finalParameters);
Jordi Inglada's avatar
Jordi Inglada committed
471 472 473

  ResampleFilterType::Pointer resample = ResampleFilterType::New();

OTB Bot's avatar
STYLE  
OTB Bot committed
474 475
  resample->SetTransform(finalTransform);
  resample->SetInput(movingImageReader->GetOutput());
476

Jordi Inglada's avatar
Jordi Inglada committed
477 478
  FixedImageType::Pointer fixedImage = fixedImageReader->GetOutput();

OTB Bot's avatar
STYLE  
OTB Bot committed
479 480 481 482
  resample->SetSize(fixedImage->GetLargestPossibleRegion().GetSize());
  resample->SetOutputOrigin(fixedImage->GetOrigin());
  resample->SetOutputSpacing(fixedImage->GetSpacing());
  resample->SetDefaultPixelValue(100);
Jordi Inglada's avatar
Jordi Inglada committed
483

OTB Bot's avatar
STYLE  
OTB Bot committed
484
  typedef  unsigned char OutputPixelType;
Jordi Inglada's avatar
Jordi Inglada committed
485

OTB Bot's avatar
STYLE  
OTB Bot committed
486
  typedef otb::Image<OutputPixelType, Dimension> OutputImageType;
487 488

  typedef itk::CastImageFilter<
OTB Bot's avatar
STYLE  
OTB Bot committed
489 490
      FixedImageType,
      OutputImageType> CastFilterType;
Jordi Inglada's avatar
Jordi Inglada committed
491

OTB Bot's avatar
STYLE  
OTB Bot committed
492
  typedef otb::ImageFileWriter<OutputImageType> WriterType;
Jordi Inglada's avatar
Jordi Inglada committed
493

OTB Bot's avatar
STYLE  
OTB Bot committed
494 495
  WriterType::Pointer     writer =  WriterType::New();
  CastFilterType::Pointer caster =  CastFilterType::New();
496

OTB Bot's avatar
STYLE  
OTB Bot committed
497
  writer->SetFileName(argv[3]);
Jordi Inglada's avatar
Jordi Inglada committed
498

OTB Bot's avatar
STYLE  
OTB Bot committed
499 500
  caster->SetInput(resample->GetOutput());
  writer->SetInput(caster->GetOutput());
Jordi Inglada's avatar
Jordi Inglada committed
501 502 503 504
  writer->Update();

  // Generate checkerboards before and after registration
  //
OTB Bot's avatar
STYLE  
OTB Bot committed
505
  typedef itk::CheckerBoardImageFilter<FixedImageType> CheckerBoardFilterType;
Jordi Inglada's avatar
Jordi Inglada committed
506 507 508

  CheckerBoardFilterType::Pointer checker = CheckerBoardFilterType::New();

OTB Bot's avatar
STYLE  
OTB Bot committed
509 510
  checker->SetInput1(fixedImage);
  checker->SetInput2(resample->GetOutput());
Jordi Inglada's avatar
Jordi Inglada committed
511

OTB Bot's avatar
STYLE  
OTB Bot committed
512 513
  caster->SetInput(checker->GetOutput());
  writer->SetInput(caster->GetOutput());
514

Jordi Inglada's avatar
Jordi Inglada committed
515 516 517
  // Before registration
  TransformType::Pointer identityTransform = TransformType::New();
  identityTransform->SetIdentity();
OTB Bot's avatar
STYLE  
OTB Bot committed
518
  resample->SetTransform(identityTransform);
Jordi Inglada's avatar
Jordi Inglada committed
519

OTB Bot's avatar
STYLE  
OTB Bot committed
520 521 522
  if (argc > 4)
    {
    writer->SetFileName(argv[4]);
Jordi Inglada's avatar
Jordi Inglada committed
523
    writer->Update();
OTB Bot's avatar
STYLE  
OTB Bot committed
524
    }
525

Jordi Inglada's avatar
Jordi Inglada committed
526
  // After registration
OTB Bot's avatar
STYLE  
OTB Bot committed
527 528 529 530
  resample->SetTransform(finalTransform);
  if (argc > 5)
    {
    writer->SetFileName(argv[5]);
Jordi Inglada's avatar
Jordi Inglada committed
531
    writer->Update();
OTB Bot's avatar
STYLE  
OTB Bot committed
532
    }
Jordi Inglada's avatar
Jordi Inglada committed
533

534 535
  // Software Guide : BeginLatex
  //
Jordi Inglada's avatar
Jordi Inglada committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549
  // \begin{figure}
  // \center
  // \includegraphics[width=0.32\textwidth]{ImageRegistration2Output.eps}
  // \includegraphics[width=0.32\textwidth]{ImageRegistration2CheckerboardBefore.eps}
  // \includegraphics[width=0.32\textwidth]{ImageRegistration2CheckerboardAfter.eps}
  // \itkcaption[Multi-Modality Registration outputs]{Mapped moving image (left)
  // and composition of fixed and moving images before (center) and after
  // (right) registration.}
  // \label{fig:ImageRegistration2Output}
  // \end{figure}
  //
  //  The moving image after resampling is presented on the left
  //  side of Figure \ref{fig:ImageRegistration2Output}. The center and right
  //  figures present a checkerboard composite of the fixed and
Jordi Inglada's avatar
Jordi Inglada committed
550
  //  moving images before and after registration. Since the real
Jordi Inglada's avatar
Jordi Inglada committed
551 552 553
  //  deformation between the 2 images is not simply a shift, some
  //  registration errors remain, but the left part of the images is
  //  correctly registered.
Jordi Inglada's avatar
Jordi Inglada committed
554
  //
555
  //  Software Guide : EndLatex
Jordi Inglada's avatar
Jordi Inglada committed
556

557
  return EXIT_SUCCESS;
Jordi Inglada's avatar
Jordi Inglada committed
558
}