ITK  4.2.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 
138  typedef typename Superclass::DerivativeType DerivativeType;
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 public:
355  double value;
356  unsigned int valueIndex;
357  };
358 
361 
364 
366  typedef std::vector< FixedImageSamplePoint > FixedImageSampleContainer;
367 
369  virtual void SampleFixedImageRegion(FixedImageSampleContainer & samples) const;
370 
371  virtual void SampleFixedImageIndexes(FixedImageSampleContainer & samples) const;
372 
374  virtual void SampleFullFixedImageRegion(FixedImageSampleContainer &
375  samples) const;
376 
379 
381 
383  //m_NumberOfPixelsCounted must be mutable because the const
384  //thread consolidation functions merge each threads valus
385  //onto this accumulator variable.
387 
390 
393 
397 
399 
402 
405 
407 
410 
412 
414 
423 
427 
428  itkStaticConstMacro(DeformationSplineOrder, unsigned int, 3);
429 
432  itkGetStaticConstMacro(DeformationSplineOrder) > BSplineTransformType;
433 
437 
441 
442  typedef std::vector< MovingImagePointType > MovingImagePointArrayType;
443  typedef std::vector< bool > BooleanArrayType;
453 
460 
462 
467 
469 
470  // Variables needed for optionally caching values when using a BSpline
471  // transform.
475 
478 
479  virtual void PreComputeTransformValues(void);
480 
483  virtual void TransformPoint(unsigned int sampleNumber,
484  MovingImagePointType & mappedPoint,
485  bool & sampleWithinSupportRegion,
486  double & movingImageValue,
487  ThreadIdType threadID) const;
488 
489  virtual void TransformPointWithDerivatives(unsigned int sampleNumber,
490  MovingImagePointType & mappedPoint,
491  bool & sampleWithinSupportRegion,
492  double & movingImageValue,
493  ImageDerivativesType & gradient,
494  ThreadIdType threadID) const;
495 
498 
501 
504 
506  virtual void ComputeImageDerivatives(const MovingImagePointType & mappedPoint,
507  ImageDerivativesType & gradient,
508  ThreadIdType threadID) const;
509 
516  };
517 
518  MultiThreaderType::Pointer m_Threader;
520  mutable unsigned int * m_ThreaderNumberOfMovingImageSamples;
523 
524  void GetValueMultiThreadedPreProcessInitiate(void) const;
525 
526  void GetValueMultiThreadedInitiate(void) const;
527 
528  void GetValueMultiThreadedPostProcessInitiate(void) const;
529 
530  static ITK_THREAD_RETURN_TYPE GetValueMultiThreadedPreProcess(void *arg);
531 
532  static ITK_THREAD_RETURN_TYPE GetValueMultiThreaded(void *arg);
533 
534  static ITK_THREAD_RETURN_TYPE GetValueMultiThreadedPostProcess(void *arg);
535 
536  virtual inline void GetValueThread(ThreadIdType threadID) const;
537 
538  virtual inline void GetValueThreadPreProcess(
539  ThreadIdType itkNotUsed(threadID),
540  bool itkNotUsed(withinSampleThread) ) const
541  {}
542  virtual inline bool GetValueThreadProcessSample(
543  ThreadIdType itkNotUsed(threadID),
544  SizeValueType itkNotUsed(fixedImageSample),
545  const MovingImagePointType & itkNotUsed(mappedPoint),
546  double itkNotUsed(movingImageValue) ) const
547  { return false; }
548  virtual inline void GetValueThreadPostProcess(
549  ThreadIdType itkNotUsed(threadID),
550  bool itkNotUsed(withinSampleThread) ) const
551  {}
552 
553  void GetValueAndDerivativeMultiThreadedPreProcessInitiate(void) const;
554 
555  void GetValueAndDerivativeMultiThreadedInitiate(void) const;
556 
557  void GetValueAndDerivativeMultiThreadedPostProcessInitiate(void) const;
558 
559  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPreProcess(void *arg);
560 
561  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreaded(void *arg);
562 
563  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPostProcess(void *arg);
564 
565  virtual inline void GetValueAndDerivativeThread(ThreadIdType threadID) const;
566 
567  virtual inline void GetValueAndDerivativeThreadPreProcess(
568  ThreadIdType itkNotUsed(threadID),
569  bool itkNotUsed(withinSampleThread) ) const
570  {}
571  virtual inline bool GetValueAndDerivativeThreadProcessSample(
572  ThreadIdType itkNotUsed(threadID),
573  SizeValueType itkNotUsed(fixedImageSample),
574  const MovingImagePointType & itkNotUsed(mappedPoint),
575  double itkNotUsed(movingImageValue),
576  const ImageDerivativesType & itkNotUsed(movingImageGradientValue) ) const
577  { return false; }
578  virtual inline void GetValueAndDerivativeThreadPostProcess(
579  ThreadIdType itkNotUsed(threadID),
580  bool itkNotUsed(withinSampleThread) ) const
581  {}
582 
586  virtual void SynchronizeTransforms() const;
587 
588 private:
589  ImageToImageMetric(const Self &); //purposely not implemented
590  void operator=(const Self &); //purposely not implemented
591 
593 };
594 } // end namespace itk
595 
596 #ifndef ITK_MANUAL_INSTANTIATION
597 #include "itkImageToImageMetric.hxx"
598 #endif
599 
600 #endif
601