Orfeo Toolbox  3.16
otbKMeansImageClassificationFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbKMeansImageClassificationFilter_txx
19 #define __otbKMeansImageClassificationFilter_txx
20 
22 #include "itkImageRegionIterator.h"
23 #include "itkNumericTraits.h"
24 
25 namespace otb
26 {
30 template <class TInputImage, class TOutputImage, unsigned int VMaxSampleDimension, class TMaskImage>
33 {
34  this->SetNumberOfInputs(2);
35  this->SetNumberOfRequiredInputs(1);
36  m_DefaultLabel = itk::NumericTraits<LabelType>::ZeroValue();
37 }
38 
39 template <class TInputImage, class TOutputImage, unsigned int VMaxSampleDimension, class TMaskImage>
40 void
43 {
44  this->itk::ProcessObject::SetNthInput(1, const_cast<MaskImageType *>(mask));
45 }
46 
47 template <class TInputImage, class TOutputImage, unsigned int VMaxSampleDimension, class TMaskImage>
49 ::MaskImageType *
52 {
53  if (this->GetNumberOfInputs() < 2)
54  {
55  return 0;
56  }
57  return static_cast<const MaskImageType *>(this->itk::ProcessObject::GetInput(1));
58 }
59 
60 template <class TInputImage, class TOutputImage, unsigned int VMaxSampleDimension, class TMaskImage>
61 void
64 {
65  unsigned int sample_size = MaxSampleDimension;
66  unsigned int nb_classes = m_Centroids.Size() / sample_size;
67 
68  for (LabelType label = 1; label <= static_cast<LabelType>(nb_classes); ++label)
69  {
70  SampleType new_centroid;
71  new_centroid.Fill(0);
72  m_CentroidsMap[label] = new_centroid;
73 
74  for (unsigned int i = 0; i < MaxSampleDimension; ++i)
75  {
76  m_CentroidsMap[label][i] =
77  static_cast<ValueType>(m_Centroids[MaxSampleDimension * (static_cast < unsigned int > (label) - 1) + i]);
78  }
79  }
80 }
81 
82 template <class TInputImage, class TOutputImage, unsigned int VMaxSampleDimension, class TMaskImage>
83 void
85 ::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, int threadId)
86 {
87  InputImageConstPointerType inputPtr = this->GetInput();
88  MaskImageConstPointerType inputMaskPtr = this->GetInputMask();
89  OutputImagePointerType outputPtr = this->GetOutput();
90 
91  typedef itk::ImageRegionConstIterator<InputImageType> InputIteratorType;
92  typedef itk::ImageRegionConstIterator<MaskImageType> MaskIteratorType;
93  typedef itk::ImageRegionIterator<OutputImageType> OutputIteratorType;
94 
95  InputIteratorType inIt(inputPtr, outputRegionForThread);
96  OutputIteratorType outIt(outputPtr, outputRegionForThread);
97 
98  MaskIteratorType maskIt;
99  if (inputMaskPtr)
100  {
101  maskIt = MaskIteratorType(inputMaskPtr, outputRegionForThread);
102  maskIt.GoToBegin();
103  }
104  unsigned int maxDimension = SampleType::Dimension;
105  unsigned int sampleSize = std::min(inputPtr->GetNumberOfComponentsPerPixel(),
106  maxDimension);
107 
108  bool validPoint = true;
109 
110  while (!outIt.IsAtEnd())
111  {
112  outIt.Set(m_DefaultLabel);
113  ++outIt;
114  }
115 
116  outIt.GoToBegin();
117 
118  validPoint = true;
119 
120  typename DistanceType::Pointer distance = DistanceType::New();
121 
122  while (!outIt.IsAtEnd() && (!inIt.IsAtEnd()))
123  {
124  if (inputMaskPtr)
125  {
126  validPoint = maskIt.Get() > 0;
127  ++maskIt;
128  }
129  if (validPoint)
130  {
131  LabelType label = 1;
132  LabelType current_label = 1;
133  SampleType pixel;
134  pixel.Fill(0);
135  for (unsigned int i = 0; i < sampleSize; ++i)
136  {
137  pixel[i] = inIt.Get()[i];
138  }
139 
140  double current_distance = distance->Evaluate(pixel, m_CentroidsMap[label]);
141 
142  for (label = 2; label <= static_cast<LabelType>(m_CentroidsMap.size()); ++label)
143  {
144  double tmp_dist = distance->Evaluate(pixel, m_CentroidsMap[label]);
145  if (tmp_dist < current_distance)
146  {
147  current_label = label;
148  current_distance = tmp_dist;
149  }
150  }
151  outIt.Set(current_label);
152  }
153  ++outIt;
154  ++inIt;
155  }
156 }
160 template <class TInputImage, class TOutputImage, unsigned int VMaxSampleDimension, class TMaskImage>
161 void
163 ::PrintSelf(std::ostream& os, itk::Indent indent) const
164 {
165  Superclass::PrintSelf(os, indent);
166 }
167 } // End namespace otb
168 #endif

Generated at Sun May 19 2013 00:33:19 for Orfeo Toolbox with doxygen 1.8.3.1