ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkImageRegistrationMethodImageSource.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkImageRegistrationMethodImageSource_h
00019 #define __itkImageRegistrationMethodImageSource_h
00020 #include "itkImage.h"
00021 #include "itkImageRegionIteratorWithIndex.h"
00022 #include "itkCommandIterationUpdate.h"
00023 #include "itkOptimizerParameters.h"
00024 
00034 namespace itk
00035 {
00036 
00037 namespace testhelper
00038 {
00039 
00040 template< typename TFixedPixelType,
00041           typename TMovingPixelType,
00042           unsigned int NDimension     >
00043 class ImageRegistrationMethodImageSource : public itk::Object
00044 {
00045 public:
00046 
00047   typedef ImageRegistrationMethodImageSource    Self;
00048   typedef Object                                Superclass;
00049   typedef SmartPointer<Self>                    Pointer;
00050   typedef SmartPointer<const Self>              ConstPointer;
00051   typedef OptimizerParameters<double>           ParametersType;
00052 
00054   itkNewMacro(Self);
00055 
00057   itkTypeMacro(Image, Object);
00058 
00059 
00060   typedef itk::Image<TMovingPixelType,NDimension> MovingImageType;
00061   typedef itk::Image<TFixedPixelType,NDimension > FixedImageType;
00062 
00063 const MovingImageType * GetMovingImage(void) const
00064   {
00065   return m_MovingImage.GetPointer();
00066   }
00067 
00068 const FixedImageType * GetFixedImage(void) const
00069   {
00070   return m_FixedImage.GetPointer();
00071   }
00072 
00073 const ParametersType & GetActualParameters(void) const
00074 {
00075   return m_Parameters;
00076 }
00077 
00078 
00079 void GenerateImages( const typename MovingImageType::SizeType & size )
00080 {
00081   typename MovingImageType::IndexType index;
00082   index.Fill(0);
00083   typename MovingImageType::RegionType region;
00084   region.SetSize( size );
00085   region.SetIndex( index );
00086 
00087   m_MovingImage->SetLargestPossibleRegion( region );
00088   m_MovingImage->SetBufferedRegion( region );
00089   m_MovingImage->SetRequestedRegion( region );
00090   m_MovingImage->Allocate();
00091 
00092   m_FixedImage->SetLargestPossibleRegion( region );
00093   m_FixedImage->SetBufferedRegion( region );
00094   m_FixedImage->SetRequestedRegion( region );
00095   m_FixedImage->Allocate();
00096 
00097   /* Fill images with a 2D gaussian*/
00098   typedef  itk::ImageRegionIteratorWithIndex<MovingImageType> MovingImageIteratorType;
00099 
00100   typedef  itk::ImageRegionIteratorWithIndex<FixedImageType> FixedImageIteratorType;
00101 
00102 
00103   itk::Point<double,2> center;
00104   center[0] = (double)region.GetSize()[0]/2.0;
00105   center[1] = (double)region.GetSize()[1]/2.0;
00106 
00107   const double s = (double)region.GetSize()[0]/2.0;
00108 
00109   itk::Point<double,2>  p;
00110   itk::Vector<double,2> d;
00111 
00112   /* Set the displacement */
00113   itk::Vector<double,2> displacement;
00114   displacement[0] = m_Parameters[0];
00115   displacement[1] = m_Parameters[1];
00116 
00117 
00118   MovingImageIteratorType ri(m_MovingImage,region);
00119   FixedImageIteratorType ti(m_FixedImage,region);
00120   while(!ri.IsAtEnd())
00121   {
00122     p[0] = ri.GetIndex()[0];
00123     p[1] = ri.GetIndex()[1];
00124     d = p-center;
00125     d += displacement;
00126     const double x = d[0];
00127     const double y = d[1];
00128     const double value = 200.0 * vcl_exp( - ( x*x + y*y )/(s*s) );
00129     ri.Set( static_cast<typename MovingImageType::PixelType>(value) );
00130     ++ri;
00131   }
00132 
00133 
00134   while(!ti.IsAtEnd())
00135   {
00136     p[0] = ti.GetIndex()[0];
00137     p[1] = ti.GetIndex()[1];
00138     d = p-center;
00139     const double x = d[0];
00140     const double y = d[1];
00141     const double value = 200.0 * vcl_exp( - ( x*x + y*y )/(s*s) );
00142     ti.Set( static_cast<typename FixedImageType::PixelType>(value) );
00143     ++ti;
00144   }
00145 
00146 
00147 }
00148 
00149 protected:
00150 
00151 ImageRegistrationMethodImageSource()
00152 {
00153   m_MovingImage = MovingImageType::New();
00154   m_FixedImage  = FixedImageType::New();
00155   m_Parameters  = ParametersType(2);
00156   m_Parameters[0] = 7.0;
00157   m_Parameters[1] = 3.0;
00158 }
00159 
00160 
00161 private:
00162 
00163   typename FixedImageType::Pointer     m_FixedImage;
00164   typename MovingImageType::Pointer    m_MovingImage;
00165 
00166   ParametersType                       m_Parameters;
00167 
00168 };
00169 
00170 }  // end namespace testhelper
00171 
00172 }  // end namespace itk
00173 #endif
00174