OTB  9.0.0
Orfeo Toolbox
otbTrainImagesBase.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #ifndef otbTrainImagesBase_h
21 #define otbTrainImagesBase_h
22 
26 
31 #include <string>
32 
33 namespace otb
34 {
35 namespace Wrapper
36 {
37 
48 {
49 public:
53  typedef itk::SmartPointer<Self> Pointer;
54  typedef itk::SmartPointer<const Self> ConstPointer;
55 
57  itkTypeMacro(TrainImagesBase, Superclass);
58 
61 
63 
64 protected:
65  typedef enum { CLASS, GEOMETRIC } SamplingStrategy;
66  struct SamplingRates;
67  class TrainFileNamesHandler;
68 
72  void InitIO();
73 
77  void InitSampling();
78 
81  void InitClassification();
84 
91  void ComputePolygonStatistics(FloatVectorImageListType* imageList, const std::vector<std::string>& vectorFileNames,
92  const std::vector<std::string>& statisticsFileNames);
93 
99  SamplingRates ComputeFinalMaximumSamplingRates(bool dedicatedValidation);
100 
101 
109  void ComputeSamplingRate(const std::vector<std::string>& statisticsFileNames, const std::string& ratesFileName, long maximum);
110 
117  void TrainModel(FloatVectorImageListType* imageList, const std::vector<std::string>& sampleTrainFileNames,
118  const std::vector<std::string>& sampleValidationFileNames);
119 
129  void SelectAndExtractSamples(FloatVectorImageType* image, std::string vectorFileName, std::string sampleFileName, std::string statisticsFileName,
130  std::string ratesFileName, SamplingStrategy strategy, std::string selectedField = "");
131 
140  void SelectAndExtractTrainSamples(const TrainFileNamesHandler& fileNames, FloatVectorImageListType* imageList, std::vector<std::string> vectorFileNames,
141  SamplingStrategy strategy, std::string selectedFieldName = "");
142 
143 
153  void SelectAndExtractValidationSamples(const TrainFileNamesHandler& fileNames, FloatVectorImageListType* imageList,
154  const std::vector<std::string>& validationVectorFileList = std::vector<std::string>());
155 
162  void SplitTrainingToValidationSamples(const TrainFileNamesHandler& fileNames, FloatVectorImageListType* imageList);
163 
164 private:
173  void SplitTrainingAndValidationSamples(FloatVectorImageType* image, std::string sampleFileName, std::string sampleTrainFileName,
174  std::string sampleValidFileName, std::string ratesTrainFileName);
175 
176 
177 protected:
179  {
180  long int fmt;
181  long int fmv;
182  };
183 
191  {
192  public:
193  void CreateTemporaryFileNames(std::string outModel, size_t nbInputs, bool dedicatedValidation)
194  {
195 
196  if (dedicatedValidation)
197  {
198  rateTrainOut = outModel + "_ratesTrain.csv";
199  }
200  else
201  {
202  rateTrainOut = outModel + "_rates.csv";
203  }
204 
205  rateValidOut = outModel + "_ratesValid.csv";
206  for (unsigned int i = 0; i < nbInputs; i++)
207  {
208  std::ostringstream oss;
209  oss << i + 1;
210  std::string strIndex(oss.str());
211  if (dedicatedValidation)
212  {
213  polyStatTrainOutputs.push_back(outModel + "_statsTrain_" + strIndex + ".xml");
214  polyStatValidOutputs.push_back(outModel + "_statsValid_" + strIndex + ".xml");
215  ratesTrainOutputs.push_back(outModel + "_ratesTrain_" + strIndex + ".csv");
216  ratesValidOutputs.push_back(outModel + "_ratesValid_" + strIndex + ".csv");
217  sampleOutputs.push_back(outModel + "_samplesTrain_" + strIndex + ".shp");
218  }
219  else
220  {
221  polyStatTrainOutputs.push_back(outModel + "_stats_" + strIndex + ".xml");
222  ratesTrainOutputs.push_back(outModel + "_rates_" + strIndex + ".csv");
223  sampleOutputs.push_back(outModel + "_samples_" + strIndex + ".shp");
224  }
225  sampleTrainOutputs.push_back(outModel + "_samplesTrain_" + strIndex + ".shp");
226  sampleValidOutputs.push_back(outModel + "_samplesValid_" + strIndex + ".shp");
227  }
228  }
229 
230  void clear()
231  {
232  for (unsigned int i = 0; i < polyStatTrainOutputs.size(); i++)
234  for (unsigned int i = 0; i < polyStatValidOutputs.size(); i++)
236  for (unsigned int i = 0; i < ratesTrainOutputs.size(); i++)
238  for (unsigned int i = 0; i < ratesValidOutputs.size(); i++)
240  for (unsigned int i = 0; i < sampleOutputs.size(); i++)
242  for (unsigned int i = 0; i < sampleTrainOutputs.size(); i++)
244  for (unsigned int i = 0; i < sampleValidOutputs.size(); i++)
246  for (unsigned int i = 0; i < tmpVectorFileList.size(); i++)
248  }
249 
250  public:
251  std::vector<std::string> polyStatTrainOutputs;
252  std::vector<std::string> polyStatValidOutputs;
253  std::vector<std::string> ratesTrainOutputs;
254  std::vector<std::string> ratesValidOutputs;
255  std::vector<std::string> sampleOutputs;
256  std::vector<std::string> sampleTrainOutputs;
257  std::vector<std::string> sampleValidOutputs;
258  std::vector<std::string> tmpVectorFileList;
259  std::string rateValidOut;
260  std::string rateTrainOut;
261 
262  private:
263  bool RemoveFile(std::string& filePath)
264  {
265  bool res = true;
266  if (itksys::SystemTools::FileExists(filePath))
267  {
268  size_t posExt = filePath.rfind('.');
269  if (posExt != std::string::npos && filePath.compare(posExt, std::string::npos, ".shp") == 0)
270  {
271  std::string shxPath = filePath.substr(0, posExt) + std::string(".shx");
272  std::string dbfPath = filePath.substr(0, posExt) + std::string(".dbf");
273  std::string prjPath = filePath.substr(0, posExt) + std::string(".prj");
274  RemoveFile(shxPath);
275  RemoveFile(dbfPath);
276  RemoveFile(prjPath);
277  }
278  res = itksys::SystemTools::RemoveFile(filePath);
279  if (!res)
280  {
281  // otbAppLogINFO( <<"Unable to remove file "<<filePath );
282  }
283  }
284  return res;
285  }
286  };
287 };
288 
289 } // end namespace Wrapper
290 } // end namespace otb
291 
292 #ifndef OTB_MANUAL_INSTANTIATION
293 #include "otbTrainImagesBase.hxx"
294 #endif
295 
296 #endif // otbTrainImagesBase_h
otbOGRDataToSamplePositionFilter.h
otb::OGRDataToSamplePositionFilter
Extracts sample position from an image using a persistent filter.
Definition: otbOGRDataToSamplePositionFilter.h:175
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::sampleTrainOutputs
std::vector< std::string > sampleTrainOutputs
Definition: otbTrainImagesBase.h:256
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler
Definition: otbTrainImagesBase.h:190
otb::Wrapper::TrainImagesBase::Superclass
CompositeApplication Superclass
Definition: otbTrainImagesBase.h:52
otbWrapperCompositeApplication.h
otb::Wrapper::TrainImagesBase::TrainModel
void TrainModel(FloatVectorImageListType *imageList, const std::vector< std::string > &sampleTrainFileNames, const std::vector< std::string > &sampleValidationFileNames)
Definition: otbTrainImagesBase.hxx:248
otb::Wrapper::TrainImagesBase::SamplingRates::fmv
long int fmv
Definition: otbTrainImagesBase.h:181
otb::Wrapper::TrainImagesBase::ShareClassificationParams
void ShareClassificationParams()
Definition: otbTrainImagesBase.hxx:139
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::sampleOutputs
std::vector< std::string > sampleOutputs
Definition: otbTrainImagesBase.h:255
otbVectorDataFileWriter.h
otb::Wrapper::TrainImagesBase::ComputePolygonStatistics
void ComputePolygonStatistics(FloatVectorImageListType *imageList, const std::vector< std::string > &vectorFileNames, const std::vector< std::string > &statisticsFileNames)
Definition: otbTrainImagesBase.hxx:155
otb::Wrapper::TrainImagesBase::GEOMETRIC
@ GEOMETRIC
Definition: otbTrainImagesBase.h:65
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::sampleValidOutputs
std::vector< std::string > sampleValidOutputs
Definition: otbTrainImagesBase.h:257
otb::Wrapper::TrainImagesBase::InitClassification
void InitClassification()
Definition: otbTrainImagesBase.hxx:127
otb::Wrapper::TrainImagesBase::ConnectSamplingParameters
void ConnectSamplingParameters()
Definition: otbTrainImagesBase.hxx:111
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbWrapperApplicationFactory.h
otb::Wrapper::TrainImagesBase::InitIO
void InitIO()
Definition: otbTrainImagesBase.hxx:30
otb::Wrapper::TrainImagesBase::SelectAndExtractSamples
void SelectAndExtractSamples(FloatVectorImageType *image, std::string vectorFileName, std::string sampleFileName, std::string statisticsFileName, std::string ratesFileName, SamplingStrategy strategy, std::string selectedField="")
Definition: otbTrainImagesBase.hxx:276
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::rateTrainOut
std::string rateTrainOut
Definition: otbTrainImagesBase.h:260
otbImageToEnvelopeVectorDataFilter.h
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::polyStatTrainOutputs
std::vector< std::string > polyStatTrainOutputs
Definition: otbTrainImagesBase.h:251
otb::Wrapper::TrainImagesBase::Self
TrainImagesBase Self
Definition: otbTrainImagesBase.h:51
otb::Wrapper::TrainImagesBase::CLASS
@ CLASS
Definition: otbTrainImagesBase.h:65
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::clear
void clear()
Definition: otbTrainImagesBase.h:230
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::rateValidOut
std::string rateValidOut
Definition: otbTrainImagesBase.h:259
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::tmpVectorFileList
std::vector< std::string > tmpVectorFileList
Definition: otbTrainImagesBase.h:258
otb::Wrapper::TrainImagesBase::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbTrainImagesBase.h:53
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::RemoveFile
bool RemoveFile(std::string &filePath)
Definition: otbTrainImagesBase.h:263
otb::Wrapper::CompositeApplication
This class is a base class for composite applications.
Definition: otbWrapperCompositeApplication.h:48
otb::SamplingRateCalculator::MapRateType
std::map< std::string, TripletType > MapRateType
Definition: otbSamplingRateCalculator.h:60
otb::Wrapper::TrainImagesBase::SamplingRates
Definition: otbTrainImagesBase.h:178
otbTrainImagesBase.hxx
otb::Wrapper::TrainImagesBase
Base class for the TrainImagesClassifier.
Definition: otbTrainImagesBase.h:47
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::ratesTrainOutputs
std::vector< std::string > ratesTrainOutputs
Definition: otbTrainImagesBase.h:253
otb::Wrapper::TrainImagesBase::ComputeFinalMaximumSamplingRates
SamplingRates ComputeFinalMaximumSamplingRates(bool dedicatedValidation)
Definition: otbTrainImagesBase.hxx:169
otb::Wrapper::TrainImagesBase::SamplingRates::fmt
long int fmt
Definition: otbTrainImagesBase.h:180
otb::Wrapper::TrainImagesBase::SamplingStrategy
SamplingStrategy
Definition: otbTrainImagesBase.h:65
otb::Wrapper::TrainImagesBase::SplitTrainingAndValidationSamples
void SplitTrainingAndValidationSamples(FloatVectorImageType *image, std::string sampleFileName, std::string sampleTrainFileName, std::string sampleValidFileName, std::string ratesTrainFileName)
Definition: otbTrainImagesBase.hxx:350
otb::Wrapper::Application::Superclass
itk::Object Superclass
Definition: otbWrapperApplication.h:81
otb::Wrapper::TrainImagesBase::ComputeSamplingRate
void ComputeSamplingRate(const std::vector< std::string > &statisticsFileNames, const std::string &ratesFileName, long maximum)
Definition: otbTrainImagesBase.hxx:222
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::CreateTemporaryFileNames
void CreateTemporaryFileNames(std::string outModel, vcl_size_t nbInputs, bool dedicatedValidation)
Definition: otbTrainImagesBase.h:193
otb::Wrapper::TrainImagesBase::InitSampling
void InitSampling()
Definition: otbTrainImagesBase.hxx:47
otb::Wrapper::TrainImagesBase::PeriodicSamplerType
otb::OGRDataToSamplePositionFilter< FloatVectorImageType, UInt8ImageType, otb::PeriodicSampler > PeriodicSamplerType
Definition: otbTrainImagesBase.h:57
otbStatisticsXMLFileWriter.h
otb::Wrapper::TrainImagesBase::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbTrainImagesBase.h:54
otb::ObjectList
This class is a generic all-purpose wrapping around an std::vector<itk::SmartPointer<ObjectType> >.
Definition: otbObjectList.h:40
otb::Wrapper::TrainImagesBase::SplitTrainingToValidationSamples
void SplitTrainingToValidationSamples(const TrainFileNamesHandler &fileNames, FloatVectorImageListType *imageList)
Definition: otbTrainImagesBase.hxx:341
otbSamplingRateCalculator.h
otb::Wrapper::TrainImagesBase::ShareSamplingParameters
void ShareSamplingParameters()
Definition: otbTrainImagesBase.hxx:98
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::polyStatValidOutputs
std::vector< std::string > polyStatValidOutputs
Definition: otbTrainImagesBase.h:252
otb::Wrapper::TrainImagesBase::SelectAndExtractTrainSamples
void SelectAndExtractTrainSamples(const TrainFileNamesHandler &fileNames, FloatVectorImageListType *imageList, std::vector< std::string > vectorFileNames, SamplingStrategy strategy, std::string selectedFieldName="")
Definition: otbTrainImagesBase.hxx:318
otb::VectorImage
Creation of an "otb" vector image which contains metadata.
Definition: otbVectorImage.h:45
otb::Wrapper::TrainImagesBase::SelectAndExtractValidationSamples
void SelectAndExtractValidationSamples(const TrainFileNamesHandler &fileNames, FloatVectorImageListType *imageList, const std::vector< std::string > &validationVectorFileList=std::vector< std::string >())
Definition: otbTrainImagesBase.hxx:331
otb::Wrapper::TrainImagesBase::ConnectClassificationParams
void ConnectClassificationParams()
Definition: otbTrainImagesBase.hxx:150
otb::Wrapper::TrainImagesBase::TrainFileNamesHandler::ratesValidOutputs
std::vector< std::string > ratesValidOutputs
Definition: otbTrainImagesBase.h:254
otb::Wrapper::TrainImagesBase::MapRateType
otb::SamplingRateCalculator::MapRateType MapRateType
Definition: otbTrainImagesBase.h:62