18 #ifndef __otbKMeansImageClassificationFilter_txx
19 #define __otbKMeansImageClassificationFilter_txx
23 #include "itkNumericTraits.h"
30 template <
class TInputImage,
class TOutputImage,
unsigned int VMaxSampleDimension,
class TMaskImage>
34 this->SetNumberOfInputs(2);
35 this->SetNumberOfRequiredInputs(1);
36 m_DefaultLabel = itk::NumericTraits<LabelType>::ZeroValue();
39 template <
class TInputImage,
class TOutputImage,
unsigned int VMaxSampleDimension,
class TMaskImage>
47 template <
class TInputImage,
class TOutputImage,
unsigned int VMaxSampleDimension,
class TMaskImage>
53 if (this->GetNumberOfInputs() < 2)
60 template <
class TInputImage,
class TOutputImage,
unsigned int VMaxSampleDimension,
class TMaskImage>
65 unsigned int sample_size = MaxSampleDimension;
66 unsigned int nb_classes = m_Centroids.Size() / sample_size;
68 for (
LabelType label = 1; label <= static_cast<LabelType>(nb_classes); ++label)
72 m_CentroidsMap[label] = new_centroid;
74 for (
unsigned int i = 0; i < MaxSampleDimension; ++i)
76 m_CentroidsMap[label][i] =
77 static_cast<ValueType>(m_Centroids[MaxSampleDimension * (static_cast <
unsigned int > (label) - 1) + i]);
82 template <
class TInputImage,
class TOutputImage,
unsigned int VMaxSampleDimension,
class TMaskImage>
95 InputIteratorType inIt(inputPtr, outputRegionForThread);
96 OutputIteratorType outIt(outputPtr, outputRegionForThread);
98 MaskIteratorType maskIt;
101 maskIt = MaskIteratorType(inputMaskPtr, outputRegionForThread);
104 unsigned int maxDimension = SampleType::Dimension;
105 unsigned int sampleSize = std::min(inputPtr->GetNumberOfComponentsPerPixel(),
108 bool validPoint =
true;
110 while (!outIt.IsAtEnd())
112 outIt.Set(m_DefaultLabel);
122 while (!outIt.IsAtEnd() && (!inIt.IsAtEnd()))
126 validPoint = maskIt.Get() > 0;
135 for (
unsigned int i = 0; i < sampleSize; ++i)
137 pixel[i] = inIt.Get()[i];
140 double current_distance = distance->Evaluate(pixel, m_CentroidsMap[label]);
142 for (label = 2; label <= static_cast<LabelType>(m_CentroidsMap.size()); ++label)
144 double tmp_dist = distance->Evaluate(pixel, m_CentroidsMap[label]);
145 if (tmp_dist < current_distance)
147 current_label = label;
148 current_distance = tmp_dist;
151 outIt.Set(current_label);
160 template <
class TInputImage,
class TOutputImage,
unsigned int VMaxSampleDimension,
class TMaskImage>
165 Superclass::PrintSelf(os, indent);