00001 /* 00002 // 00003 // Copyright 2004-2010 SRI International 00004 // 00005 // Copyright 1997-2009 Torsten Rohlfing 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: 2553 $ 00026 // 00027 // $LastChangedDate: 2010-11-06 15:14:57 -0700 (Sat, 06 Nov 2010) $ 00028 // 00029 // $LastChangedBy: torsten_at_home $ 00030 // 00031 */ 00032 00033 #include "cmtkTypedArrayFunctionHistogramMatching.h" 00034 00035 #include <vector> 00036 00037 cmtk::TypedArrayFunctionHistogramMatching 00038 ::TypedArrayFunctionHistogramMatching 00039 ( const TypedArray& variableArray, const TypedArray& fixedArray, const size_t numberOfHistogramBins ) 00040 : m_Lookup( numberOfHistogramBins ) 00041 { 00042 this->m_FixedArrayHistogram = fixedArray.GetHistogram( numberOfHistogramBins, true /*centeredBins*/ ); 00043 this->m_FixedArrayHistogram->ConvertToCumulative(); 00044 00045 this->m_VariableArrayHistogram = variableArray.GetHistogram( numberOfHistogramBins, true /*centeredBins*/ ); 00046 this->m_VariableArrayHistogram->ConvertToCumulative(); 00047 00048 this->CreateLookup(); 00049 } 00050 00051 cmtk::TypedArrayFunctionHistogramMatching 00052 ::TypedArrayFunctionHistogramMatching( const Self::HistogramType& variableHistogram, const Self::HistogramType& fixedHistogram ) 00053 : m_Lookup( variableHistogram.GetNumBins() ) 00054 { 00055 this->m_FixedArrayHistogram = Self::HistogramType::SmartPtr( fixedHistogram.Clone() ); 00056 this->m_FixedArrayHistogram->ConvertToCumulative(); 00057 00058 this->m_VariableArrayHistogram = Self::HistogramType::SmartPtr( variableHistogram.Clone() ); 00059 this->m_VariableArrayHistogram->ConvertToCumulative(); 00060 00061 this->CreateLookup(); 00062 } 00063 00064 void 00065 cmtk::TypedArrayFunctionHistogramMatching 00066 ::CreateLookup() 00067 { 00068 const size_t variableNumBins = this->m_VariableArrayHistogram->GetNumBins(); 00069 std::vector<double> normalizedVariableHistogram( variableNumBins ); 00070 for ( size_t l = 0; l < variableNumBins; ++l ) 00071 { 00072 normalizedVariableHistogram[l] = 1.0 * (*(this->m_VariableArrayHistogram))[l] / (*(this->m_VariableArrayHistogram))[variableNumBins-1]; 00073 } 00074 00075 const size_t fixedNumBins = this->m_FixedArrayHistogram->GetNumBins(); 00076 std::vector<double> normalizedFixedHistogram( fixedNumBins ); 00077 for ( size_t l = 0; l < fixedNumBins; ++l ) 00078 { 00079 normalizedFixedHistogram[l] = 1.0 * (*(this->m_FixedArrayHistogram))[l] / (*(this->m_FixedArrayHistogram))[fixedNumBins-1]; 00080 } 00081 00082 size_t j = 0; 00083 for ( size_t i = 0; i < variableNumBins; ++i ) 00084 { 00085 while ((j < fixedNumBins) && (normalizedFixedHistogram[j] < normalizedVariableHistogram[i])) 00086 { 00087 ++j; 00088 } 00089 this->m_Lookup[i] = j; 00090 } 00091 } 00092 00093 cmtk::Types::DataItem 00094 cmtk::TypedArrayFunctionHistogramMatching 00095 ::operator()( const cmtk::Types::DataItem valueIn ) const 00096 { 00097 return this->m_FixedArrayHistogram->BinToValue( this->m_Lookup[ this->m_VariableArrayHistogram->ValueToBin( valueIn ) ] ); 00098 }