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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkImageClassifierFilter.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 __itkImageClassifierFilter_txx
#define __itkImageClassifierFilter_txx
#include "itkImageClassifierFilter.h"
namespace itk {
namespace Statistics {
template< class TSample, class TInputImage, class TOutputImage >
ImageClassifierFilter<TSample,TInputImage,TOutputImage>
::ImageClassifierFilter()
{
this->m_NumberOfClasses = 0;
this->SetNumberOfRequiredInputs( 3 );
this->SetNumberOfRequiredOutputs( 1 );
/** Initialize decision rule */
m_DecisionRule = NULL;
m_NumberOfClasses = 0;
}
template< class TSample, class TInputImage, class TOutputImage >
void
ImageClassifierFilter<TSample,TInputImage,TOutputImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "Number of classes: "
<< this->GetNumberOfClasses()
<< std::endl;
os << indent << "Decision Rule: "
<< this->GetDecisionRule()
<< std::endl;
os << indent << "Image: "
<< this->GetImage()
<< std::endl;
}
template< class TSample, class TInputImage, class TOutputImage >
void
ImageClassifierFilter<TSample,TInputImage,TOutputImage>
::SetImage( const InputImageType * image )
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(0,
const_cast< InputImageType * >( image ) );
}
template< class TSample, class TInputImage, class TOutputImage >
const TInputImage *
ImageClassifierFilter<TSample,TInputImage,TOutputImage>
::GetImage( ) const
{
if (this->GetNumberOfInputs() < 1)
{
return 0;
}
return static_cast<const TInputImage * >
(this->ProcessObject::GetInput(0) );
}
template< class TSample, class TInputImage, class TOutputImage >
void
ImageClassifierFilter<TSample,TInputImage,TOutputImage>
::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, class TInputImage, class TOutputImage >
void
ImageClassifierFilter<TSample,TInputImage,TOutputImage>
::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, class TInputImage, class TOutputImage >
void
ImageClassifierFilter<TSample,TInputImage,TOutputImage>
::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, class TInputImage, class TOutputImage >
void
ImageClassifierFilter<TSample,TInputImage,TOutputImage>
::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 InputImageType * inputImage =
static_cast< const InputImageType * >( this->ProcessObject::GetInput( 0 ) );
std::vector< double > discriminantScores;
discriminantScores.resize( this->m_NumberOfClasses );
OutputImageType * outputImage = dynamic_cast< OutputImageType * >(
this->ProcessObject::GetOutput(0));
outputImage->CopyInformation( inputImage );
outputImage->SetRegions( inputImage->GetBufferedRegion() );
outputImage->Allocate();
ImageRegionConstIterator< InputImageType > inpItr( inputImage, inputImage->GetBufferedRegion() );
ImageRegionIterator< OutputImageType > outItr( outputImage, outputImage->GetBufferedRegion() );
inpItr.GoToBegin();
outItr.GoToBegin();
while( !inpItr.IsAtEnd() )
{
MeasurementVectorType measurements;
MeasurementVectorTraits::Assign( measurements, inpItr.Get() );
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);
OutputPixelType value = static_cast< OutputPixelType >(classLabels[classIndex]);
outItr.Set(value);
++inpItr;
++outItr;
}
}
} // end of namespace Statistics
} // end of namespace itk
#endif
|