cmtkGroupwiseRegistrationFunctionalXformTemplateBase.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 //#define DEBUG_COMM
00034 
00035 #include <Registration/cmtkGroupwiseRegistrationFunctionalXformTemplateBase.h>
00036 
00037 #include <Base/cmtkMathUtil.h>
00038 #include <IO/cmtkVolumeIO.h>
00039 
00040 #ifdef CMTK_BUILD_MPI
00041 #  include <mpi.h>
00042 #  include <IO/cmtkMPI.h>
00043 #endif
00044 
00045 namespace
00046 cmtk
00047 {
00048 
00051 
00052 template<class TXform>
00053 GroupwiseRegistrationFunctionalXformTemplateBase<TXform>::GroupwiseRegistrationFunctionalXformTemplateBase() :
00054   m_HistogramBins( 64 ),
00055   m_HistogramKernelRadiusMax( 0 ),
00056   m_MaxRelativeNumberOutsidePixels( 0.99 ), // if there is an image with more then 99% pixels outside FOV, registration probably failed
00057   m_CropImageHistograms( false )
00058 {}
00059 
00060 template<class TXform>
00061 GroupwiseRegistrationFunctionalXformTemplateBase<TXform>::~GroupwiseRegistrationFunctionalXformTemplateBase()
00062 {
00063 }
00064 
00065 template<class TXform>
00066 void
00067 GroupwiseRegistrationFunctionalXformTemplateBase<TXform>
00068 ::SetNumberOfHistogramBins( const size_t numberOfHistogramBins )
00069 {
00070   this->m_HistogramBins = numberOfHistogramBins;
00071   if ( this->m_OriginalImageVector.size() )
00072     {
00073     std::cerr << "WARNING: you called GroupwiseRegistrationFunctionalBase::SetNumberOfHistogramBins(),\n"
00074               << "         but target images were already set. To be safe, I am re-generating\n"
00075               << "         pre-scaled images.\n\n";
00076     this->SetTargetImages( this->m_OriginalImageVector );
00077     }
00078 }
00079 
00080 template<class TXform>
00081 UniformVolume::SmartPtr
00082 GroupwiseRegistrationFunctionalXformTemplateBase<TXform>
00083 ::PrepareSingleImage( UniformVolume::SmartPtr& image )
00084 {
00085   UniformVolume::SmartPtr newTargetImage = this->Superclass::PrepareSingleImage( image );
00086 
00087   TypedArray::SmartPtr data = newTargetImage->GetData();
00088   if ( this->m_CropImageHistograms )
00089     {
00090     data->PruneHistogram( true, false, this->m_HistogramBins );
00091     }
00092   
00093   data->Rescale( 1.0 * (this->m_HistogramBins-1) / data->GetRange().Width(), this->m_HistogramKernelRadiusMax );
00094   
00095   newTargetImage->SetData( TypedArray::SmartPtr( data->Convert( TYPE_BYTE ) ) );
00096   return newTargetImage;
00097 }
00098 
00099 template<class TXform>
00100 void
00101 GroupwiseRegistrationFunctionalXformTemplateBase<TXform>
00102 ::PrepareTargetImages()
00103 {
00104   this->m_ImageVector.resize( this->m_OriginalImageVector.size() );
00105 
00106 #ifdef CMTK_BUILD_MPI
00107   // using MPI, prepare only some of the images locally, obtain others from other nodes
00108   const size_t imageFrom = this->m_RankMPI;
00109   const size_t imageSkip = this->m_SizeMPI;
00110 #else
00111   const size_t imageFrom = 0;
00112   const size_t imageSkip = 1;
00113 #endif
00114   for ( size_t i = imageFrom; i < this->m_ImageVector.size(); i += imageSkip )
00115     {
00116     this->m_ImageVector[i] = UniformVolume::SmartPtr( this->PrepareSingleImage( this->m_OriginalImageVector[i] ) );
00117     }
00118   
00119 #ifdef CMTK_BUILD_MPI
00120   // obtain filtered, scaled image data from other nodes
00121   for ( size_t i = 0; i < this->m_ImageVector.size(); ++i )
00122     {
00123     cmtk::mpi::Broadcast( MPI::COMM_WORLD, this->m_ImageVector[i], i % this->m_SizeMPI );
00124     }
00125 #endif
00126 
00127   this->m_PrivateUserBackgroundValue = this->m_UserBackgroundValue + this->m_HistogramKernelRadiusMax;
00128 }
00129 
00131 
00132 } // namespace cmtk
00133 
00134 #include <Base/cmtkAffineXform.h>
00135 #include <Base/cmtkSplineWarpXform.h>
00136 
00137 template class cmtk::GroupwiseRegistrationFunctionalXformTemplateBase<cmtk::AffineXform>;
00138 template class cmtk::GroupwiseRegistrationFunctionalXformTemplateBase<cmtk::SplineWarpXform>;
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines