1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkSampleClassifierFilter.txx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkSampleClassifierFilter_txx
#define __itkSampleClassifierFilter_txx
#include "itkSampleClassifierFilter.h"
namespace itk {
namespace Statistics {
template< class TSample >
SampleClassifierFilter< TSample >
::SampleClassifierFilter()
{
this->m_NumberOfClasses = 0;
this->SetNumberOfRequiredInputs( 3 );
this->SetNumberOfRequiredOutputs( 1 );
this->ProcessObject::SetNthOutput( 0, this->MakeOutput(0) );
/** Initialize decision rule */
m_DecisionRule = NULL;
}
template< class TSample >
void
SampleClassifierFilter< TSample >
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "NumberofClasses: "
<< this->GetNumberOfClasses() << std::endl;
os << indent << "DecisionRule: "
<< this->GetDecisionRule() << std::endl;
}
template< class TSample >
void
SampleClassifierFilter< TSample >
::SetInput( const TSample* sample )
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(0,
const_cast< SampleType * >( sample ) );
}
template< class TSample >
const TSample *
SampleClassifierFilter< TSample >
::GetInput( ) const
{
if (this->GetNumberOfInputs() < 1)
{
return 0;
}
return static_cast<const SampleType * >
(this->ProcessObject::GetInput(0) );
}
template< class TSample >
void
SampleClassifierFilter< TSample >
::SetClassLabels( const ClassLabelVectorObjectType * classLabels )
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(1,
const_cast< ClassLabelVectorObjectType * >( classLabels ) );
}
template< class TSample >
void
SampleClassifierFilter< TSample >
::SetMembershipFunctions( const MembershipFunctionVectorObjectType * membershipFunctions )
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(2,
const_cast< MembershipFunctionVectorObjectType * >( membershipFunctions ) );
}
template< class TSample >
void
SampleClassifierFilter< TSample >
::SetMembershipFunctionsWeightsArray( const
MembershipFunctionsWeightsArrayObjectType * weightsArray )
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(3,
const_cast<
MembershipFunctionsWeightsArrayObjectType * >( weightsArray ) );
}
template< class TSample >
typename SampleClassifierFilter< TSample >::DataObjectPointer
SampleClassifierFilter< TSample >
::MakeOutput(unsigned int)
{
return static_cast<DataObject*>( MembershipSampleType::New().GetPointer() );
}
template< class TSample >
void
SampleClassifierFilter< TSample >
::GenerateData()
{
const ClassLabelVectorObjectType * classLabelsDecorated =
static_cast< const ClassLabelVectorObjectType * >( this->ProcessObject::GetInput( 1 ) );
const MembershipFunctionVectorObjectType * membershipFunctionsDecorated =
static_cast< const MembershipFunctionVectorObjectType * >( this->ProcessObject::GetInput( 2 ) );
const MembershipFunctionsWeightsArrayObjectType *
membershipFunctionsWeightsArrayDecorated =
static_cast< const MembershipFunctionsWeightsArrayObjectType * >( this->ProcessObject::GetInput( 3 ) );
const ClassLabelVectorType & classLabels = classLabelsDecorated->Get();
const MembershipFunctionVectorType & membershipFunctions = membershipFunctionsDecorated->Get();
// Check number of Labels and MembershipSamples against the number of classes */
if( membershipFunctions.size() != this->m_NumberOfClasses )
{
itkExceptionMacro("Number of Membership functions does not match the number of classes");
}
if( classLabels.size() != this->m_NumberOfClasses )
{
itkExceptionMacro("Number of class labels does not match the number of classes");
}
if( m_DecisionRule.IsNull())
{
itkExceptionMacro("Decision rule is not set");
}
MembershipFunctionsWeightsArrayType membershipFunctionsWeightsArray;
if( membershipFunctionsWeightsArrayDecorated == NULL )
{
// no weights array is set and hence all membership functions will have equal
// weight
membershipFunctionsWeightsArray.SetSize( this->m_NumberOfClasses );
membershipFunctionsWeightsArray.Fill(1.0);
}
else
{
membershipFunctionsWeightsArray = membershipFunctionsWeightsArrayDecorated->Get();
}
if ( membershipFunctionsWeightsArray.Size() != this->m_NumberOfClasses
)
{
itkExceptionMacro("Membership functions weight array size does not match the\
number of classes ");
}
const SampleType * sample =
static_cast< const SampleType * >( this->ProcessObject::GetInput( 0 ) );
std::vector< double > discriminantScores;
discriminantScores.resize( this->m_NumberOfClasses );
MembershipSampleType * output = dynamic_cast< MembershipSampleType * >(
this->ProcessObject::GetOutput(0));
output->SetSample( this->GetInput() );
output->SetNumberOfClasses( this->m_NumberOfClasses );
typename TSample::ConstIterator iter = sample->Begin();
typename TSample::ConstIterator end = sample->End();
while (iter != end)
{
typename TSample::MeasurementVectorType measurements;
measurements = iter.GetMeasurementVector();
for (unsigned int i = 0; i < this->m_NumberOfClasses; i++)
{
discriminantScores[i] = membershipFunctionsWeightsArray[i] *
membershipFunctions[i]->Evaluate(measurements);
}
unsigned int classIndex;
classIndex = m_DecisionRule->Evaluate(discriminantScores);
output->AddInstance(classLabels[classIndex], iter.GetInstanceIdentifier());
++iter;
}
}
template< class TSample >
const typename SampleClassifierFilter< TSample >::MembershipSampleType *
SampleClassifierFilter< TSample >
::GetOutput() const
{
return static_cast< const MembershipSampleType * >(this->ProcessObject::GetOutput(0));
}
} // end of namespace Statistics
} // end of namespace itk
#endif
|