26 #include "itkImageRegionIteratorWithIndex.h"
27 #include "itkFixedArray.h"
29 #include "itkImageRegionIterator.h"
30 #include "itkMersenneTwisterRandomVariateGenerator.h"
37 template <
class TListSample,
class TMap,
class TSOMLearningBehaviorFunctor,
class TSOMNeighborhoodBehaviorFunctor>
40 this->SetNumberOfRequiredInputs(0);
41 this->SetNumberOfRequiredOutputs(1);
45 m_NumberOfIterations = 10;
48 m_NeighborhoodSizeInit.Fill(3);
49 m_MinWeight =
static_cast<ValueType>(0.0);
50 m_MaxWeight =
static_cast<ValueType>(128.0);
57 template <
class TListSample,
class TMap,
class TSOMLearningBehaviorFunctor,
class TSOMNeighborhoodBehaviorFunctor>
68 template <
class TListSample,
class TMap,
class TSOMLearningBehaviorFunctor,
class TSOMNeighborhoodBehaviorFunctor>
75 typedef itk::ImageRegionIteratorWithIndex<MapType> IteratorType;
76 typedef itk::FixedArray<double, MapType::ImageDimension> FixedArrayIndexType;
77 typedef itk::Statistics::EuclideanDistanceMetric<FixedArrayIndexType> DistanceType;
78 typename DistanceType::Pointer distance = DistanceType::New();
81 IndexType position = map->GetWinner(sample);
88 for (
unsigned int i = 0; i < MapType::ImageDimension; ++i)
90 localSize[i] = 2 * radius[i] + 1;
92 localRegion.SetIndex(localIndex);
93 localRegion.SetSize(localSize);
94 localRegion.Crop(map->GetLargestPossibleRegion());
95 IteratorType it(map, localRegion);
99 for (it.GoToBegin(); !it.IsAtEnd(); ++it)
104 FixedArrayIndexType positionFA, indexFA;
105 positionFA[0] = position[0];
106 positionFA[1] = position[1];
108 indexFA[0] = it.GetIndex()[0];
109 indexFA[1] = it.GetIndex()[1];
111 double tempBeta = beta / (1 + distance->Evaluate(positionFA, indexFA));
113 for (
unsigned int i = 0; i < newNeuron.Size(); ++i)
115 newNeuron[i] = tempNeuron[i] +
static_cast<typename NeuronType::ValueType
>((sample[i] - tempNeuron[i]) * tempBeta);
124 template <
class TListSample,
class TMap,
class TSOMLearningBehaviorFunctor,
class TSOMNeighborhoodBehaviorFunctor>
128 double newBeta = m_BetaFunctor(currentIteration, m_NumberOfIterations, m_BetaInit, m_BetaEnd);
131 SizeType newSize = m_NeighborhoodSizeFunctor(currentIteration, m_NumberOfIterations, m_NeighborhoodSizeInit);
135 for (
typename ListSampleType::Iterator it = m_ListSample->Begin(); it != m_ListSample->End(); ++it)
137 UpdateMap(it.GetMeasurementVector(), newBeta, newSize);
143 template <
class TListSample,
class TMap,
class TSOMLearningBehaviorFunctor,
class TSOMNeighborhoodBehaviorFunctor>
146 Superclass::GenerateOutputInformation();
147 this->GetOutput()->SetNumberOfComponentsPerPixel(m_ListSample->GetMeasurementVectorSize());
154 region.SetIndex(index);
155 region.SetSize(this->GetMapSize());
156 this->GetOutput()->SetRegions(region);
161 template <
class TListSample,
class TMap,
class TSOMLearningBehaviorFunctor,
class TSOMNeighborhoodBehaviorFunctor>
164 if (this->GetNumberOfOutputs() != 1)
165 itkExceptionMacro(<<
"Number of output image should be 1");
175 template <
class TListSample,
class TMap,
class TSOMLearningBehaviorFunctor,
class TSOMNeighborhoodBehaviorFunctor>
178 this->AllocateOutputs();
179 this->BeforeThreadedGenerateData();
186 typedef itk::Statistics::MersenneTwisterRandomVariateGenerator GeneratorType;
187 typedef itk::ImageRegionIterator<MapType> IteratorType;
188 GeneratorType::Pointer generator = GeneratorType::New();
189 generator->Initialize(m_Seed);
191 NeuronType neuronInit(m_ListSample->GetMeasurementVectorSize());
192 IteratorType it(map, map->GetLargestPossibleRegion());
194 for (it.GoToBegin(); !it.IsAtEnd(); ++it)
196 for (
unsigned int i = 0; i < neuronInit.Size(); ++i)
199 static_cast<typename NeuronType::ValueType
>(generator->GetUniformVariate(
static_cast<double>(m_MinWeight),
static_cast<double>(m_MaxWeight)));
206 NeuronType neuronInit(m_ListSample->GetMeasurementVectorSize());
207 neuronInit.Fill(m_MaxWeight);
208 map->FillBuffer(neuronInit);
212 for (
unsigned int i = 0; i < m_NumberOfIterations; ++i)
215 std::cerr <<
"Step " << i + 1 <<
" / " << m_NumberOfIterations <<
" \r";
219 this->AfterThreadedGenerateData();
224 template <
class TListSample,
class TMap,
class TSOMLearningBehaviorFunctor,
class TSOMNeighborhoodBehaviorFunctor>
227 Superclass::PrintSelf(os, indent);