ITK  4.4.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  itkGetConstObjectMacro(FixedImage, FixedImageType );
147 
149  itkSetConstObjectMacro( MovingImage, MovingImageType );
150  itkGetConstObjectMacro(MovingImage, MovingImageType );
152 
154  itkSetObjectMacro( Transform, TransformType );
155 
157  itkGetModifiableObjectMacro(Transform, TransformType);
158 
160  itkSetObjectMacro(Interpolator, InterpolatorType);
161 
163  itkGetModifiableObjectMacro(Interpolator, InterpolatorType);
164 
166  SizeValueType GetNumberOfMovingImageSamples(void)
167  {
168  return this->GetNumberOfPixelsCounted();
169  }
170 
171  itkGetConstReferenceMacro(NumberOfPixelsCounted, SizeValueType);
172 
174  void SetFixedImageRegion(const FixedImageRegionType reg);
175 
177  itkGetConstReferenceMacro(FixedImageRegion, FixedImageRegionType);
178 
180  itkSetObjectMacro(MovingImageMask, MovingImageMaskType);
181  itkSetConstObjectMacro(MovingImageMask, MovingImageMaskType);
182  itkGetConstObjectMacro(MovingImageMask, MovingImageMaskType);
184 
186  itkSetObjectMacro(FixedImageMask, FixedImageMaskType);
187  itkSetConstObjectMacro(FixedImageMask, FixedImageMaskType);
188  itkGetConstObjectMacro(FixedImageMask, FixedImageMaskType);
190 
193  void SetFixedImageIndexes(const FixedImageIndexContainer & indexes);
194 
195  void SetUseFixedImageIndexes(bool useIndex);
196 
197  itkGetConstReferenceMacro(UseFixedImageIndexes, bool);
198 
200  void SetNumberOfThreads(ThreadIdType numberOfThreads);
201  itkGetConstReferenceMacro(NumberOfThreads, ThreadIdType);
203 
205  itkSetMacro(ComputeGradient, bool);
206  itkGetConstReferenceMacro(ComputeGradient, bool);
207  itkBooleanMacro(ComputeGradient);
209 
211  virtual void ComputeGradient(void);
212 
214  itkGetModifiableObjectMacro(GradientImage, GradientImageType);
215 
217  void SetTransformParameters(const ParametersType & parameters) const;
218 
220  unsigned int GetNumberOfParameters(void) const
221  {
222  return m_Transform->GetNumberOfParameters();
223  }
224 
227  virtual void Initialize(void)
228  throw ( ExceptionObject );
229 
231  virtual void MultiThreadingInitialize(void) throw ( ExceptionObject );
232 
235  virtual void SetNumberOfFixedImageSamples(SizeValueType numSamples);
236  itkGetConstReferenceMacro(NumberOfFixedImageSamples, SizeValueType);
238 
241  void SetNumberOfSpatialSamples(SizeValueType num)
242  {
243  this->SetNumberOfFixedImageSamples(num);
244  }
245 
246  SizeValueType GetNumberOfSpatialSamples(void)
247  {
248  return this->GetNumberOfFixedImageSamples();
249  }
250 
253  void SetFixedImageSamplesIntensityThreshold(const FixedImagePixelType & thresh);
254 
255  itkGetConstReferenceMacro(FixedImageSamplesIntensityThreshold, FixedImagePixelType);
256 
257  void SetUseFixedImageSamplesIntensityThreshold(bool useThresh);
258 
259  itkGetConstReferenceMacro(UseFixedImageSamplesIntensityThreshold, bool);
260 
264  void SetUseAllPixels(bool useAllPixels);
265 
266  void UseAllPixelsOn(void)
267  {
268  this->SetUseAllPixels(true);
269  }
270 
271  void UseAllPixelsOff(void)
272  {
273  this->SetUseAllPixels(false);
274  }
275 
276  itkGetConstReferenceMacro(UseAllPixels, bool);
277 
282  void SetUseSequentialSampling(bool sequentialSampling);
283 
284  itkGetConstReferenceMacro(UseSequentialSampling, bool);
285 
295  void ReinitializeSeed();
296  void ReinitializeSeed(int seed);
298 
315  itkSetMacro(UseCachingOfBSplineWeights, bool);
316  itkGetConstReferenceMacro(UseCachingOfBSplineWeights, bool);
317  itkBooleanMacro(UseCachingOfBSplineWeights);
319 
320  typedef MultiThreader MultiThreaderType;
322  itkGetModifiableObjectMacro(Threader, MultiThreaderType);
323  const TransformPointer * GetThreaderTransform()
324  {
325  return m_ThreaderTransform;
326  }
328 
329 protected:
331  virtual ~ImageToImageMetric();
332 
333  void PrintSelf(std::ostream & os, Indent indent) const;
334 
341  {
342 public:
344  {
345  point.Fill(0.0);
346  value = 0;
347  valueIndex = 0;
348  }
349 
351 
352 public:
354  double value;
355  unsigned int valueIndex;
356  };
357 
360 
363 
365  typedef std::vector< FixedImageSamplePoint > FixedImageSampleContainer;
366 
368  virtual void SampleFixedImageRegion(FixedImageSampleContainer & samples) const;
369 
370  virtual void SampleFixedImageIndexes(FixedImageSampleContainer & samples) const;
371 
373  virtual void SampleFullFixedImageRegion(FixedImageSampleContainer &
374  samples) const;
375 
378 
380 
382  //m_NumberOfPixelsCounted must be mutable because the const
383  //thread consolidation functions merge each threads valus
384  //onto this accumulator variable.
386 
389 
392 
396 
398 
401 
404 
406 
409 
411 
413 
422 
426 
427  itkStaticConstMacro(DeformationSplineOrder, unsigned int, 3);
428 
430  FixedImageType ::ImageDimension,
431  itkGetStaticConstMacro(DeformationSplineOrder) > BSplineTransformType;
432 
436 
440 
441  typedef std::vector< MovingImagePointType > MovingImagePointArrayType;
442  typedef std::vector< bool > BooleanArrayType;
452 
459 
461 
466 
468 
469  // Variables needed for optionally caching values when using a BSpline
470  // transform.
474 
477 
478  virtual void PreComputeTransformValues(void);
479 
482  virtual void TransformPoint(unsigned int sampleNumber,
483  MovingImagePointType & mappedPoint,
484  bool & sampleWithinSupportRegion,
485  double & movingImageValue,
486  ThreadIdType threadID) const;
487 
488  virtual void TransformPointWithDerivatives(unsigned int sampleNumber,
489  MovingImagePointType & mappedPoint,
490  bool & sampleWithinSupportRegion,
491  double & movingImageValue,
492  ImageDerivativesType & gradient,
493  ThreadIdType threadID) const;
494 
497 
500 
503 
505  virtual void ComputeImageDerivatives(const MovingImagePointType & mappedPoint,
506  ImageDerivativesType & gradient,
507  ThreadIdType threadID) const;
508 
515  };
516 
517  MultiThreaderType::Pointer m_Threader;
519  mutable unsigned int * m_ThreaderNumberOfMovingImageSamples;
522 
523  void GetValueMultiThreadedPreProcessInitiate(void) const;
524 
525  void GetValueMultiThreadedInitiate(void) const;
526 
527  void GetValueMultiThreadedPostProcessInitiate(void) const;
528 
529  static ITK_THREAD_RETURN_TYPE GetValueMultiThreadedPreProcess(void *arg);
530 
531  static ITK_THREAD_RETURN_TYPE GetValueMultiThreaded(void *arg);
532 
533  static ITK_THREAD_RETURN_TYPE GetValueMultiThreadedPostProcess(void *arg);
534 
535  virtual inline void GetValueThread(ThreadIdType threadID) const;
536 
537  virtual inline void GetValueThreadPreProcess(
538  ThreadIdType itkNotUsed(threadID),
539  bool itkNotUsed(withinSampleThread) ) const
540  {}
541  virtual inline bool GetValueThreadProcessSample(
542  ThreadIdType itkNotUsed(threadID),
543  SizeValueType itkNotUsed(fixedImageSample),
544  const MovingImagePointType & itkNotUsed(mappedPoint),
545  double itkNotUsed(movingImageValue) ) const
546  { return false; }
547  virtual inline void GetValueThreadPostProcess(
548  ThreadIdType itkNotUsed(threadID),
549  bool itkNotUsed(withinSampleThread) ) const
550  {}
551 
552  void GetValueAndDerivativeMultiThreadedPreProcessInitiate(void) const;
553 
554  void GetValueAndDerivativeMultiThreadedInitiate(void) const;
555 
556  void GetValueAndDerivativeMultiThreadedPostProcessInitiate(void) const;
557 
558  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPreProcess(void *arg);
559 
560  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreaded(void *arg);
561 
562  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPostProcess(void *arg);
563 
564  virtual inline void GetValueAndDerivativeThread(ThreadIdType threadID) const;
565 
566  virtual inline void GetValueAndDerivativeThreadPreProcess(
567  ThreadIdType itkNotUsed(threadID),
568  bool itkNotUsed(withinSampleThread) ) const
569  {}
570  virtual inline bool GetValueAndDerivativeThreadProcessSample(
571  ThreadIdType itkNotUsed(threadID),
572  SizeValueType itkNotUsed(fixedImageSample),
573  const MovingImagePointType & itkNotUsed(mappedPoint),
574  double itkNotUsed(movingImageValue),
575  const ImageDerivativesType & itkNotUsed(movingImageGradientValue) ) const
576  { return false; }
577  virtual inline void GetValueAndDerivativeThreadPostProcess(
578  ThreadIdType itkNotUsed(threadID),
579  bool itkNotUsed(withinSampleThread) ) const
580  {}
581 
585  virtual void SynchronizeTransforms() const;
586 
587 private:
588  ImageToImageMetric(const Self &); //purposely not implemented
589  void operator=(const Self &); //purposely not implemented
590 
592 };
593 } // end namespace itk
594 
595 #ifndef ITK_MANUAL_INSTANTIATION
596 #include "itkImageToImageMetric.hxx"
597 #endif
598 
599 #endif
600