cmtkVoxelMatchingCorrRatio.h

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 #ifndef __cmtkVoxelMatchingCorrRatio_h_included_
00034 #define __cmtkVoxelMatchingCorrRatio_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <Registration/cmtkRegistrationJointHistogram.h>
00039 #include <Base/cmtkInterpolator.h>
00040 #include <System/cmtkSmartPtr.h>
00041 
00042 namespace
00043 cmtk
00044 {
00045 
00048 
00052 template< Interpolators::InterpolationEnum I = Interpolators::LINEAR >
00053 class VoxelMatchingCorrRatio : 
00054   public VoxelMatchingMetric<short,TYPE_SHORT,I>
00055 {
00056 public:
00058   typedef VoxelMatchingCorrRatio<I> Self;
00059 
00061   typedef SmartPointer<Self> SmartPtr;
00062 
00068   VoxelMatchingCorrRatio ( const UniformVolume* refVolume, const UniformVolume* fltVolume, const unsigned int numBins = CMTK_HISTOGRAM_AUTOBINS )
00069     : VoxelMatchingMetric<short,TYPE_SHORT,I>( refVolume, fltVolume )
00070   { 
00071     NumBinsX = NumBinsY = numBins;
00072 
00073     if ( NumBinsX == CMTK_HISTOGRAM_AUTOBINS )
00074       NumBinsX = std::max<unsigned>( std::min<unsigned>( refVolume->GetNumberOfPixels(), 128 ), 8 );
00075     HistogramI.Resize( NumBinsX );
00076 
00077     if ( NumBinsY == CMTK_HISTOGRAM_AUTOBINS )
00078       NumBinsY = std::max<unsigned>( std::min<unsigned>( fltVolume->GetNumberOfPixels(), 128 ), 8 );
00079     HistogramJ.Resize( NumBinsY );
00080 
00081     HistogramI.SetRange( refVolume->GetData()->GetRange() );
00082 
00083     SumJ.resize( NumBinsX );
00084     SumJ2.resize( NumBinsX );
00085     
00086     fltVolume->GetData()->GetStatistics( MuJ, SigmaSqJ );
00087 
00088     HistogramJ.SetRange( fltVolume->GetData()->GetRange() );
00089 
00090     SumI.resize( NumBinsY );
00091     SumI2.resize( NumBinsY );
00092     
00093     refVolume->GetData()->GetStatistics( MuI, SigmaSqI );
00094   }
00095 
00100   void Reset() 
00101   {
00102     HistogramI.Reset();
00103     HistogramJ.Reset();
00104     std::fill( SumI.begin(), SumI.end(), 0 );
00105     std::fill( SumJ.begin(), SumJ.end(), 0 );
00106     std::fill( SumI2.begin(), SumI2.end(), 0 );
00107     std::fill( SumJ2.begin(), SumJ2.end(), 0 );
00108   }
00109 
00114   template<class T> void Increment( const T a, const T b )
00115   {
00116     // what's the reference histogram bin?
00117     size_t bin = HistogramI.ValueToBin( a );
00118     // count this sample
00119     HistogramI.Increment( bin );
00120     // add floating value to sum of values for this class
00121     SumJ[bin] += b;
00122     // add squared floating value to sum of squared values for this class
00123     SumJ2[bin] += b * b;
00124 
00125     // same in reverse
00126     bin = HistogramJ.ValueToBin( b );
00127     HistogramJ.Increment( bin );
00128     SumI[bin] += a;
00129     SumI2[bin] += a * a;
00130   }
00131 
00134   template<class T> void Decrement( const T a, const T b )
00135   {
00136     // what's the reference histogram bin?
00137     size_t bin = HistogramI.ValueToBin( a );
00138     // count this sample
00139     HistogramI.Decrement( bin );
00140     // add floating value to sum of values for this class
00141     SumJ[bin] -= b;
00142     // add squared floating value to sum of squared values for this class
00143     SumJ2[bin] -= b * b;
00144 
00145     // same in reverse
00146     bin = HistogramJ.ValueToBin( b );
00147     HistogramJ.Decrement( bin );
00148     SumI[bin] -= a;
00149     SumI2[bin] -= a * a;
00150   }
00151 
00154   void AddMetric ( const Self& other )
00155   {
00156     HistogramI.AddHistogram( other.HistogramI );
00157     for ( size_t bin = 0; bin < NumBinsX; ++bin ) 
00158       {
00159       SumJ[bin] += other.SumJ[bin];
00160       SumJ2[bin] += other.SumJ2[bin];
00161       }
00162     
00163     HistogramJ.AddHistogram( other.HistogramJ );
00164     for ( size_t bin = 0; bin < NumBinsY; ++bin ) 
00165       {
00166       SumI[bin] += other.SumI[bin];
00167       SumI2[bin] += other.SumI2[bin];
00168       }
00169   }
00170   
00173   void RemoveMetric ( const Self& other )
00174   {
00175     HistogramI.RemoveHistogram( other.HistogramI );
00176     for ( size_t bin = 0; bin < NumBinsX; ++bin ) {
00177       SumJ[bin] -= other.SumJ[bin];
00178       SumJ2[bin] -= other.SumJ2[bin];
00179     }
00180 
00181     HistogramJ.RemoveHistogram( other.HistogramJ );
00182     for ( size_t bin = 0; bin < NumBinsY; ++bin ) {
00183       SumI[bin] -= other.SumI[bin];
00184       SumI2[bin] -= other.SumI2[bin];
00185     }
00186   }
00187 
00189   typename Self::ReturnType Get () const;
00190 
00191 private:
00193   size_t NumBinsX;
00194   
00196   std::vector<double> SumJ;
00197 
00199   std::vector<double> SumJ2;
00200 
00202   Histogram<unsigned int> HistogramI;
00203 
00204   // Variance of complete floating image for normalization.
00205   Types::DataItem SigmaSqJ;
00206 
00207   // Mean of complete floating image for normalization.
00208   Types::DataItem MuJ;
00209 
00211   size_t NumBinsY;
00212   
00214   std::vector<double> SumI;
00215 
00217   std::vector<double> SumI2;
00218 
00220   Histogram<unsigned int> HistogramJ;
00221 
00222   // Variance of complete floating image for normalization.
00223   Types::DataItem SigmaSqI;
00224 
00225   // Mean of complete floating image for normalization.
00226   Types::DataItem MuI;
00227 };
00228 
00230 typedef VoxelMatchingCorrRatio<Interpolators::LINEAR> VoxelMatchingCorrRatio_Trilinear;
00231 
00233 typedef VoxelMatchingCorrRatio<Interpolators::NEAREST_NEIGHBOR> VoxelMatchingCorrRatio_NearestNeighbor;
00234 
00236 
00237 } // namespace cmtk
00238 
00239 #endif // #ifndef __cmtkVoxelMatchingCorrRatio_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines