OTB  9.0.0
Orfeo Toolbox
otbPeriodicSOM.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  * Copyright (C) 2007-2012 Institut Mines Telecom / Telecom Bretagne
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 
23 #ifndef otbPeriodicSOM_hxx
24 #define otbPeriodicSOM_hxx
25 
26 #include "itkNumericTraits.h"
27 #include "itkNeighborhoodIterator.h"
28 
29 #include "otbPeriodicSOM.h"
30 
31 namespace otb
32 {
33 
40 template <class TListSample, class TMap, class TSOMLearningBehaviorFunctor, class TSOMNeighborhoodBehaviorFunctor>
42  SizeType& radius)
43 {
44  unsigned int i, j;
45 
46  // output map pointer
47  MapPointerType map = this->GetOutput(0);
48 
49  // winner index in the map
50  IndexType position = map->GetWinner(sample);
51  NeuronType winner = map->GetPixel(position);
52 
53  // Local neighborhood definition
54  typedef typename MapType::Superclass ImageMapType;
55  typedef itk::NeighborhoodIterator<ImageMapType> NeighborhoodIteratorType;
56  typename MapType::RegionType mapRegion = map->GetLargestPossibleRegion();
57  NeighborhoodIteratorType it(radius, map, mapRegion);
58 
59  // Here, the periodic update is achieved 'by hand' since
60  // PeriodicBoundaryCondition does not allow modifying
61  // VectorImage contents
62  SizeType mapSize = mapRegion.GetSize();
63  IndexType positionToUpdate;
64 
65  // Iterate over the neighborhood of the winner neuron
66  it.SetLocation(position);
67 
68  for (i = 0; i < it.Size(); ++i)
69  {
70  typename NeighborhoodIteratorType::OffsetType offset = it.GetOffset(i);
71 
72  // The neighborhood is of elliptic shape
73  double theDistance = itk::NumericTraits<double>::Zero;
74  for (j = 0; j < MapType::ImageDimension; ++j)
75  theDistance += pow(static_cast<double>(offset[j]), 2.0) / pow(static_cast<double>(radius[j]), 2.0);
76 
77  if (theDistance <= 1.0)
78  {
79  for (j = 0; j < MapType::ImageDimension; ++j)
80  {
81  int pos = offset[j] + position[j];
82  positionToUpdate[j] = (pos >= 0) ? pos % mapSize[j] : (mapSize[j] - ((-pos) % mapSize[j])) % mapSize[j];
83  }
84 
85  NeuronType tempNeuron = it.GetPixel(i);
86  // NeuronType newNeuron ( tempNeuron.Size() );
87  // newNeuron.Fill( 0.0 ); // FIXME
88  NeuronType newNeuron(tempNeuron);
89 
90  double tempBeta = beta / (1.0 + theDistance);
91  for (j = 0; j < newNeuron.Size(); ++j)
92  {
93  newNeuron[j] += static_cast<typename NeuronType::ValueType>((sample[j] - tempNeuron[j]) * tempBeta);
94  }
95  map->SetPixel(positionToUpdate, newNeuron);
96  }
97  }
98 }
99 
100 } // end of namespace otb
101 
102 #endif
otb::PeriodicSOM< TListSample, TMap, Functor::CzihoSOMLearningBehaviorFunctor, Functor::CzihoSOMNeighborhoodBehaviorFunctor >::NeuronType
MapType::PixelType NeuronType
Definition: otbPeriodicSOM.h:80
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::PeriodicSOM< TListSample, TMap, Functor::CzihoSOMLearningBehaviorFunctor, Functor::CzihoSOMNeighborhoodBehaviorFunctor >::MapPointerType
MapType::Pointer MapPointerType
Definition: otbPeriodicSOM.h:85
otbPeriodicSOM.h
otb::PeriodicSOM< TListSample, TMap, Functor::CzihoSOMLearningBehaviorFunctor, Functor::CzihoSOMNeighborhoodBehaviorFunctor >::SizeType
MapType::SizeType SizeType
Definition: otbPeriodicSOM.h:83
otb::PeriodicSOM< TListSample, TMap, Functor::CzihoSOMLearningBehaviorFunctor, Functor::CzihoSOMNeighborhoodBehaviorFunctor >::IndexType
MapType::IndexType IndexType
Definition: otbPeriodicSOM.h:82
otb::PeriodicSOM::UpdateMap
void UpdateMap(const NeuronType &sample, double beta, SizeType &radius) override
Definition: otbPeriodicSOM.hxx:41