20 #ifndef otbSOMModel_hxx
21 #define otbSOMModel_hxx
27 #include "itkImageRegionIterator.h"
28 #include "itkImageRegionConstIterator.h"
39 std::ostream&
BinaryWrite(std::ostream& stream,
const T& value)
41 return stream.write(
reinterpret_cast<const char*
>(&value),
sizeof(T));
46 return stream.write(value.c_str(), value.length());
52 return stream.read(
reinterpret_cast<char*
>(&value),
sizeof(T));
56 template <
class TInputValue,
unsigned int MapDimension>
59 this->m_Dimension = MapType::ImageDimension;
62 template <
class TInputValue,
unsigned int MapDimension>
67 template <
class TInputValue,
unsigned int MapDimension>
71 typename EstimatorType::Pointer estimator = EstimatorType::New();
72 estimator->SetListSample(this->GetInputListSample());
73 estimator->SetMapSize(m_MapSize);
74 estimator->SetNeighborhoodSizeInit(m_NeighborhoodSizeInit);
75 estimator->SetNumberOfIterations(m_NumberOfIterations);
76 estimator->SetBetaInit(m_BetaInit);
77 estimator->SetBetaEnd(m_BetaEnd);
78 estimator->SetMaxWeight(m_MaxWeight);
80 m_SOMMap = estimator->GetOutput();
83 template <
class TInputValue,
unsigned int MapDimension>
97 template <
class TInputValue,
unsigned int MapDimension>
103 template <
class TInputValue,
unsigned int MapDimension>
106 itk::ImageRegionConstIterator<MapType> inputIterator(m_SOMMap, m_SOMMap->GetLargestPossibleRegion());
107 inputIterator.GoToBegin();
108 std::ofstream ofs(filename, std::ios::binary);
111 SizeType size = m_SOMMap->GetLargestPossibleRegion().GetSize();
112 for (
size_t i = 0; i < MapDimension; i++)
118 while (!inputIterator.IsAtEnd())
121 for (
size_t i = 0; i < vect.GetNumberOfElements(); i++)
130 if (this->m_WriteMap ==
true)
132 std::ofstream otxt(filename +
".txt");
133 inputIterator.GoToBegin();
134 while (!inputIterator.IsAtEnd())
137 for (
size_t i = 0; i < vect.GetNumberOfElements(); i++)
139 otxt << vect[i] <<
" ";
148 template <
class TInputValue,
unsigned int MapDimension>
151 std::ifstream ifs(filename, std::ios::binary);
155 for (
int i = 0; i < 3; i++)
159 std::string modelType(s);
163 unsigned int dimension;
165 if (modelType !=
"som" || dimension != MapDimension)
167 itkExceptionMacro(<<
"Error opening " << filename.c_str());
171 itk::Index<MapDimension> index;
172 for (
unsigned int i = 0; i < MapDimension; i++)
177 unsigned int numberOfElements;
179 m_SOMMap = MapType::New();
181 region.SetSize(size);
182 m_SOMMap->SetNumberOfComponentsPerPixel(numberOfElements);
183 region.SetIndex(index);
184 m_SOMMap->SetRegions(region);
185 m_SOMMap->Allocate();
187 itk::ImageRegionIterator<MapType> outputIterator(m_SOMMap, region);
188 outputIterator.GoToBegin();
190 while (!outputIterator.IsAtEnd())
193 for (
unsigned int i = 0; i < numberOfElements; i++)
198 vect[i] =
static_cast<double>(v);
200 outputIterator.Set(vect);
204 this->m_Dimension = MapType::ImageDimension;
207 template <
class TInputValue,
unsigned int MapDimension>
212 target.SetSize(this->m_Dimension);
214 auto winner = m_SOMMap->GetWinner(value);
215 for (
unsigned int i = 0; i < this->m_Dimension; i++)
217 target[i] = winner.GetElement(i);