cmtkGroupwiseRegistrationFunctionalAffineInitializer.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2398 $
00026 //
00027 //  $LastChangedDate: 2010-10-05 14:54:37 -0700 (Tue, 05 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include <Registration/cmtkGroupwiseRegistrationFunctionalAffineInitializer.h>
00034 
00035 #include <Base/cmtkMathUtil.h>
00036 
00037 namespace
00038 cmtk
00039 {
00040 
00043 
00044 void
00045 GroupwiseRegistrationFunctionalAffineInitializer::InitializeXforms
00046 ( GroupwiseRegistrationFunctionalBase& functional, const bool alignCenters, const bool alignCenterOfMass, const bool initScales )
00047 {
00048   const size_t numberOfImages = functional.m_ImageVector.size();
00049 
00050   const Vector3D centerTemplate = functional.m_TemplateGrid->GetCenterCropRegion();
00051   
00052   std::vector<Vector3D> centers( numberOfImages );
00053   std::vector<Vector3D> firstOrderMoments;
00054   if ( initScales )
00055     firstOrderMoments.resize( numberOfImages );
00056   functional.m_XformVector.resize( numberOfImages );
00057 
00058   Vector3D centerAverage;
00059   std::fill( centerAverage.begin(), centerAverage.end(), 0 );
00060 
00061   // first get all image centers (either FOV or center of mass)
00062   for ( size_t imageIdx = 0; imageIdx < numberOfImages; ++imageIdx )
00063     {
00064     if ( alignCenters )
00065       {
00066       if ( alignCenterOfMass )
00067         {
00068         if ( initScales )
00069           {
00070           centers[imageIdx] = functional.m_ImageVector[imageIdx]->GetCenterOfMass( firstOrderMoments[imageIdx] );
00071           }
00072         else
00073           {
00074           centers[imageIdx] = functional.m_ImageVector[imageIdx]->GetCenterOfMass();
00075           }
00076         }
00077       else
00078         {
00079         centers[imageIdx] = functional.m_ImageVector[imageIdx]->GetCenter();
00080         }
00081       }
00082     centerAverage += centers[imageIdx];
00083     }
00084 
00085   // compute average of all image centers
00086   centerAverage *= (1.0 / numberOfImages);
00087   
00088   // now make sure every image gests shifted so their center align, and the overall shift is zero
00089   for ( size_t imageIdx = 0; imageIdx < numberOfImages; ++imageIdx )
00090     {
00091     AffineXform::SmartPtr xform( new AffineXform );
00092     xform->SetUseLogScaleFactors( true );
00093     xform->SetCenter( centerTemplate.begin() );
00094  
00095     const Vector3D delta( centers[imageIdx] - centerAverage );
00096     
00097     xform->SetXlate( delta.begin() );
00098     functional.m_XformVector[imageIdx] = xform;
00099     }
00100   
00101   // convert first order moments to scale with average log factor 0
00102   if ( initScales )
00103     {
00104     UniformVolume::CoordinateVectorType avgScales( UniformVolume::CoordinateVectorType::Init( 0 ) );
00105     UniformVolume::CoordinateVectorType fom0( firstOrderMoments[0] );
00106     for ( size_t imageIdx = 0; imageIdx < numberOfImages; ++imageIdx )
00107       {
00108       for ( int dim = 0; dim < 3; ++dim )
00109         firstOrderMoments[imageIdx][dim] = log( firstOrderMoments[imageIdx][dim] / fom0[dim] );
00110       avgScales += firstOrderMoments[imageIdx];
00111       }
00112     avgScales *= ( 1.0 /  numberOfImages );
00113     for ( size_t imageIdx = 0; imageIdx < numberOfImages; ++imageIdx )
00114       {
00115       firstOrderMoments[imageIdx] -= avgScales;
00116       AffineXform::SmartPtr::DynamicCastFrom( functional.m_XformVector[imageIdx] )->SetScales( firstOrderMoments[imageIdx].begin() );
00117       }
00118     }
00119 }
00120 
00121 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines