cmtkImagePairSimilarityJointHistogram.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/cmtkImagePairSimilarityJointHistogram.h>
00034 
00035 namespace
00036 cmtk
00037 {
00038 
00041 
00042 ImagePairSimilarityJointHistogram::ImagePairSimilarityJointHistogram
00043 ( UniformVolume::SmartConstPtr& refVolume, UniformVolume::SmartConstPtr& fltVolume, const Interpolators::InterpolationEnum interpolation )
00044   : ImagePairSimilarityMeasure( Self::PrescaleData( refVolume, &this->m_NumberOfBinsX, &this->m_ScaleFactorReference, &this->m_ScaleOffsetReference ), 
00045                                 Self::PrescaleData( fltVolume, &this->m_NumberOfBinsY, &this->m_ScaleFactorFloating, &this->m_ScaleOffsetFloating ), interpolation )
00046 {
00047   this->m_JointHistogram.Resize( this->m_NumberOfBinsX, this->m_NumberOfBinsY );
00048 }
00049 
00050 void
00051 ImagePairSimilarityJointHistogram::SetReferenceVolume( const UniformVolume::SmartConstPtr& refVolume )
00052 {
00053   Superclass::SetReferenceVolume( Self::PrescaleData( refVolume, &this->m_NumberOfBinsX, &this->m_ScaleFactorReference, &this->m_ScaleOffsetReference ) );
00054   this->m_JointHistogram.Resize( this->m_NumberOfBinsX, this->m_NumberOfBinsY );
00055 }
00056 
00057 void
00058 ImagePairSimilarityJointHistogram::SetFloatingVolume( const UniformVolume::SmartConstPtr& fltVolume )
00059 {
00060   Superclass::SetFloatingVolume( Self::PrescaleData( fltVolume, &this->m_NumberOfBinsY, &this->m_ScaleFactorFloating, &this->m_ScaleOffsetFloating ) );
00061   this->m_JointHistogram.Resize( this->m_NumberOfBinsX, this->m_NumberOfBinsY );
00062 }
00063 
00064 UniformVolume::SmartPtr
00065 ImagePairSimilarityJointHistogram::PrescaleData
00066 ( const UniformVolume::SmartConstPtr& volume, size_t* numberOfBins, Types::DataItem* scaleFactor, Types::DataItem* scaleOffset )
00067 {
00068   UniformVolume::SmartPtr newVolume( volume->CloneGrid() );
00069   newVolume->CreateDataArray( TYPE_ITEM );
00070   const size_t numberOfPixels = volume->GetNumberOfPixels();
00071     
00072   Types::DataItem value = 0;
00073   Types::DataItem minValue = FLT_MAX;
00074   Types::DataItem maxValue = -FLT_MAX;
00075 
00076   const DataGrid::IndexType& cropFrom = volume->CropRegion().From();
00077   const DataGrid::IndexType& cropTo = volume->CropRegion().To();
00078   const DataGrid::IndexType increments = volume->GetCropRegionIncrements();
00079 
00080   int offset = increments[0];
00081   for ( int z = cropFrom[2]; z < cropTo[2]; ++z, offset += increments[2] ) 
00082     {
00083     for ( int y = cropFrom[1]; y < cropTo[1]; ++y, offset += increments[1] ) 
00084       {
00085       for ( int x = cropFrom[0]; x < cropTo[0]; ++x, ++offset ) 
00086         {
00087         if ( volume->GetDataAt( value, offset ) ) 
00088           {
00089           if ( value > maxValue ) maxValue = value;
00090           if ( value < minValue ) minValue = value;
00091           }
00092         }
00093       }
00094     }
00095   
00096   switch ( volume->GetData()->GetDataClass() ) 
00097     {
00098     case DATACLASS_LABEL: 
00099     {
00100     *numberOfBins = 1 + static_cast<unsigned int>(maxValue-minValue);
00101     if ( *numberOfBins > 254 ) 
00102       {
00103       StdErr << "Fatal error: Cannot handle more than 254 different labels.\n";
00104       exit( 1 );
00105       }
00106 
00107     *scaleOffset = -minValue;
00108     *scaleFactor = 1.0;
00109 
00110     for ( size_t idx = 0; idx < numberOfPixels; ++idx ) 
00111       {
00112       if ( volume->GetDataAt( value, idx ) )
00113         newVolume->SetDataAt( static_cast<Types::DataItem>( value + *scaleOffset ), idx );
00114       else
00115         newVolume->GetData()->SetPaddingAt( idx );
00116       }
00117     }
00118     break;
00119     default: // Handle everything else as grey-level data.
00120     case DATACLASS_GREY: 
00121     {
00122     *numberOfBins = JointHistogramBase::CalcNumBins( volume );
00123     
00124     *scaleFactor = (*numberOfBins-1) / ( maxValue - minValue );
00125     *scaleOffset = -minValue * *scaleFactor;
00126 
00127     for ( size_t idx = 0; idx < numberOfPixels; ++idx ) 
00128       {
00129       if ( volume->GetDataAt( value, idx ) ) 
00130         {
00131         value = std::max( std::min( value, maxValue ), minValue );
00132         newVolume->SetDataAt( static_cast<Types::DataItem>( floor(*scaleFactor*value+*scaleOffset) ), idx );
00133         } 
00134       else 
00135         {
00136         newVolume->GetData()->SetPaddingAt( idx );
00137         }
00138       }
00139     }
00140     break;
00141     } 
00142 
00143   return newVolume;
00144 }
00145 
00146 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines