ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkImageToImageMetric.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkImageToImageMetric_h
19 #define __itkImageToImageMetric_h
20 
25 #include "itkSpatialObject.h"
27 
28 namespace itk
29 {
50 template< class TFixedImage, class TMovingImage >
51 class ITK_EXPORT ImageToImageMetric:
53 {
54 public:
60 
62  typedef typename Superclass::ParametersValueType CoordinateRepresentationType;
63 
66 
68  typedef TMovingImage MovingImageType;
69  typedef typename TMovingImage::PixelType MovingImagePixelType;
70  typedef typename MovingImageType::ConstPointer MovingImageConstPointer;
71 
73  typedef TFixedImage FixedImageType;
74  typedef typename TFixedImage::PixelType FixedImagePixelType;
75  typedef typename FixedImageType::ConstPointer FixedImageConstPointer;
76  typedef typename FixedImageType::RegionType FixedImageRegionType;
77 
79  itkStaticConstMacro(MovingImageDimension,
80  unsigned int,
81  TMovingImage::ImageDimension);
82  itkStaticConstMacro(FixedImageDimension,
83  unsigned int,
84  TFixedImage::ImageDimension);
86 
89  itkGetStaticConstMacro(MovingImageDimension),
90  itkGetStaticConstMacro(FixedImageDimension) >
92 
98 
100  typedef typename FixedImageType::IndexType FixedImageIndexType;
102  typedef typename MovingImageType::IndexType MovingImageIndexType;
105 
106  typedef std::vector< FixedImageIndexType > FixedImageIndexContainer;
107 
110 
119 
121 
127 
133 
136 
139 
141  typedef typename Superclass::ParametersType ParametersType;
142 
144  itkSetConstObjectMacro( FixedImage, FixedImageType );
145 
147  itkGetConstObjectMacro( FixedImage, FixedImageType );
148 
150  itkSetConstObjectMacro( MovingImage, MovingImageType );
151 
153  itkGetConstObjectMacro( MovingImage, MovingImageType );
154 
156  itkSetObjectMacro( Transform, TransformType );
157 
159  itkGetConstObjectMacro(Transform, TransformType);
160 
162  itkSetObjectMacro(Interpolator, InterpolatorType);
163 
165  itkGetConstObjectMacro(Interpolator, InterpolatorType);
166 
168  SizeValueType GetNumberOfMovingImageSamples(void)
169  {
170  return this->GetNumberOfPixelsCounted();
171  }
172 
173  itkGetConstReferenceMacro(NumberOfPixelsCounted, SizeValueType);
174 
176  void SetFixedImageRegion(const FixedImageRegionType reg);
177 
179  itkGetConstReferenceMacro(FixedImageRegion, FixedImageRegionType);
180 
182  itkSetObjectMacro(MovingImageMask, MovingImageMaskType);
183  itkSetConstObjectMacro(MovingImageMask, MovingImageMaskType);
184  itkGetConstObjectMacro(MovingImageMask, MovingImageMaskType);
186 
188  itkSetObjectMacro(FixedImageMask, FixedImageMaskType);
189  itkSetConstObjectMacro(FixedImageMask, FixedImageMaskType);
190  itkGetConstObjectMacro(FixedImageMask, FixedImageMaskType);
192 
195  void SetFixedImageIndexes(const FixedImageIndexContainer & indexes);
196 
197  void SetUseFixedImageIndexes(bool useIndex);
198 
199  itkGetConstReferenceMacro(UseFixedImageIndexes, bool);
200 
202  void SetNumberOfThreads(ThreadIdType numberOfThreads);
203  itkGetConstReferenceMacro(NumberOfThreads, ThreadIdType);
205 
207  itkSetMacro(ComputeGradient, bool);
208  itkGetConstReferenceMacro(ComputeGradient, bool);
209  itkBooleanMacro(ComputeGradient);
211 
213  virtual void ComputeGradient(void);
214 
216  itkGetConstObjectMacro(GradientImage, GradientImageType);
217 
219  void SetTransformParameters(const ParametersType & parameters) const;
220 
222  unsigned int GetNumberOfParameters(void) const
223  {
224  return m_Transform->GetNumberOfParameters();
225  }
226 
229  virtual void Initialize(void)
230  throw ( ExceptionObject );
231 
233  virtual void MultiThreadingInitialize(void) throw ( ExceptionObject );
234 
237  virtual void SetNumberOfFixedImageSamples(SizeValueType numSamples);
238  itkGetConstReferenceMacro(NumberOfFixedImageSamples, SizeValueType);
240 
243  void SetNumberOfSpatialSamples(SizeValueType num)
244  {
245  this->SetNumberOfFixedImageSamples(num);
246  }
247 
248  SizeValueType GetNumberOfSpatialSamples(void)
249  {
250  return this->GetNumberOfFixedImageSamples();
251  }
252 
255  void SetFixedImageSamplesIntensityThreshold(const FixedImagePixelType & thresh);
256 
257  itkGetConstReferenceMacro(FixedImageSamplesIntensityThreshold, FixedImagePixelType);
258 
259  void SetUseFixedImageSamplesIntensityThreshold(bool useThresh);
260 
261  itkGetConstReferenceMacro(UseFixedImageSamplesIntensityThreshold, bool);
262 
266  void SetUseAllPixels(bool useAllPixels);
267 
268  void UseAllPixelsOn(void)
269  {
270  this->SetUseAllPixels(true);
271  }
272 
273  void UseAllPixelsOff(void)
274  {
275  this->SetUseAllPixels(false);
276  }
277 
278  itkGetConstReferenceMacro(UseAllPixels, bool);
279 
284  void SetUseSequentialSampling(bool sequentialSampling);
285 
286  itkGetConstReferenceMacro(UseSequentialSampling, bool);
287 
297  void ReinitializeSeed();
298  void ReinitializeSeed(int seed);
300 
317  itkSetMacro(UseCachingOfBSplineWeights, bool);
318  itkGetConstReferenceMacro(UseCachingOfBSplineWeights, bool);
319  itkBooleanMacro(UseCachingOfBSplineWeights);
321 
322  typedef MultiThreader MultiThreaderType;
324  itkGetConstObjectMacro(Threader, MultiThreaderType);
325  const TransformPointer * GetThreaderTransform()
326  {
327  return m_ThreaderTransform;
328  }
330 
331 protected:
333  virtual ~ImageToImageMetric();
334 
335  void PrintSelf(std::ostream & os, Indent indent) const;
336 
343  {
344 public:
346  {
347  point.Fill(0.0);
348  value = 0;
349  valueIndex = 0;
350  }
351 
353 
354 public:
356  double value;
357  unsigned int valueIndex;
358  };
359 
362 
365 
367  typedef std::vector< FixedImageSamplePoint > FixedImageSampleContainer;
368 
370  virtual void SampleFixedImageRegion(FixedImageSampleContainer & samples) const;
371 
372  virtual void SampleFixedImageIndexes(FixedImageSampleContainer & samples) const;
373 
375  virtual void SampleFullFixedImageRegion(FixedImageSampleContainer &
376  samples) const;
377 
380 
382 
384  //m_NumberOfPixelsCounted must be mutable because the const
385  //thread consolidation functions merge each threads valus
386  //onto this accumulator variable.
388 
391 
394 
398 
400 
403 
406 
408 
411 
413 
415 
424 
428 
429  itkStaticConstMacro(DeformationSplineOrder, unsigned int, 3);
430 
432  FixedImageType ::ImageDimension,
433  itkGetStaticConstMacro(DeformationSplineOrder) > BSplineTransformType;
434 
438 
442 
443  typedef std::vector< MovingImagePointType > MovingImagePointArrayType;
444  typedef std::vector< bool > BooleanArrayType;
454 
461 
463 
468 
470 
471  // Variables needed for optionally caching values when using a BSpline
472  // transform.
476 
479 
480  virtual void PreComputeTransformValues(void);
481 
484  virtual void TransformPoint(unsigned int sampleNumber,
485  MovingImagePointType & mappedPoint,
486  bool & sampleWithinSupportRegion,
487  double & movingImageValue,
488  ThreadIdType threadID) const;
489 
490  virtual void TransformPointWithDerivatives(unsigned int sampleNumber,
491  MovingImagePointType & mappedPoint,
492  bool & sampleWithinSupportRegion,
493  double & movingImageValue,
494  ImageDerivativesType & gradient,
495  ThreadIdType threadID) const;
496 
499 
502 
505 
507  virtual void ComputeImageDerivatives(const MovingImagePointType & mappedPoint,
508  ImageDerivativesType & gradient,
509  ThreadIdType threadID) const;
510 
517  };
518 
519  MultiThreaderType::Pointer m_Threader;
521  mutable unsigned int * m_ThreaderNumberOfMovingImageSamples;
524 
525  void GetValueMultiThreadedPreProcessInitiate(void) const;
526 
527  void GetValueMultiThreadedInitiate(void) const;
528 
529  void GetValueMultiThreadedPostProcessInitiate(void) const;
530 
531  static ITK_THREAD_RETURN_TYPE GetValueMultiThreadedPreProcess(void *arg);
532 
533  static ITK_THREAD_RETURN_TYPE GetValueMultiThreaded(void *arg);
534 
535  static ITK_THREAD_RETURN_TYPE GetValueMultiThreadedPostProcess(void *arg);
536 
537  virtual inline void GetValueThread(ThreadIdType threadID) const;
538 
539  virtual inline void GetValueThreadPreProcess(
540  ThreadIdType itkNotUsed(threadID),
541  bool itkNotUsed(withinSampleThread) ) const
542  {}
543  virtual inline bool GetValueThreadProcessSample(
544  ThreadIdType itkNotUsed(threadID),
545  SizeValueType itkNotUsed(fixedImageSample),
546  const MovingImagePointType & itkNotUsed(mappedPoint),
547  double itkNotUsed(movingImageValue) ) const
548  { return false; }
549  virtual inline void GetValueThreadPostProcess(
550  ThreadIdType itkNotUsed(threadID),
551  bool itkNotUsed(withinSampleThread) ) const
552  {}
553 
554  void GetValueAndDerivativeMultiThreadedPreProcessInitiate(void) const;
555 
556  void GetValueAndDerivativeMultiThreadedInitiate(void) const;
557 
558  void GetValueAndDerivativeMultiThreadedPostProcessInitiate(void) const;
559 
560  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPreProcess(void *arg);
561 
562  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreaded(void *arg);
563 
564  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPostProcess(void *arg);
565 
566  virtual inline void GetValueAndDerivativeThread(ThreadIdType threadID) const;
567 
568  virtual inline void GetValueAndDerivativeThreadPreProcess(
569  ThreadIdType itkNotUsed(threadID),
570  bool itkNotUsed(withinSampleThread) ) const
571  {}
572  virtual inline bool GetValueAndDerivativeThreadProcessSample(
573  ThreadIdType itkNotUsed(threadID),
574  SizeValueType itkNotUsed(fixedImageSample),
575  const MovingImagePointType & itkNotUsed(mappedPoint),
576  double itkNotUsed(movingImageValue),
577  const ImageDerivativesType & itkNotUsed(movingImageGradientValue) ) const
578  { return false; }
579  virtual inline void GetValueAndDerivativeThreadPostProcess(
580  ThreadIdType itkNotUsed(threadID),
581  bool itkNotUsed(withinSampleThread) ) const
582  {}
583 
587  virtual void SynchronizeTransforms() const;
588 
589 private:
590  ImageToImageMetric(const Self &); //purposely not implemented
591  void operator=(const Self &); //purposely not implemented
592 
594 };
595 } // end namespace itk
596 
597 #ifndef ITK_MANUAL_INSTANTIATION
598 #include "itkImageToImageMetric.hxx"
599 #endif
600 
601 #endif
602