otbKMeansClassification.cxx 4.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
20

21
#include "otbClassKMeansBase.h"
22

23 24 25
namespace otb
{
namespace Wrapper
26 27
{

28
class KMeansClassification: public ClassKMeansBase
29 30 31 32
{
public:
  /** Standard class typedefs. */
  typedef KMeansClassification Self;
33
  typedef ClassKMeansBase Superclass;
34 35
  typedef itk::SmartPointer<Self> Pointer;
  typedef itk::SmartPointer<const Self> ConstPointer;
36

37 38
  /** Standard macro */
  itkNewMacro(Self);
39

40
  itkTypeMacro(Self, Superclass);
41

42
private:
43
  void DoInit() ITK_OVERRIDE
44 45
  {
    SetName("KMeansClassification");
46 47
    SetDescription("Unsupervised KMeans image classification");

48
    SetDocName("Unsupervised KMeans image classification");
49
    SetDocLongDescription("Performs unsupervised KMeans image classification.");
50 51 52
    SetDocLimitations("None");
    SetDocAuthors("OTB-Team");
    SetDocSeeAlso(" ");
53

54
    AddDocTag(Tags::Learning);
55 56 57 58 59 60 61 62 63
    AddDocTag(Tags::Segmentation);

    // Perform initialization
    ClearApplications();

    initKMIO();
    InitKMSampling();
    InitKMClassification();

64
    AddParameter(ParameterType_OutputImage, "out", "Output Image");
65
    SetParameterDescription("out", "Output image containing the class indexes.");
66
    SetDefaultOutputPixelType("out",ImagePixelType_uint8);
67

68
    AddRAMParameter(); // TODO verifier si les RAMParameter sont bien tous cablés
69

70
    // Doc example parameter settings
71
    SetDocExampleParameterValue("in", "QB_1_ortho.tif");
72
    SetDocExampleParameterValue("ts", "1000");
73
    SetDocExampleParameterValue("nc", "5");
74
    SetDocExampleParameterValue("maxit", "1000");
75
    SetDocExampleParameterValue("ct", "0.0001");
76
    SetDocExampleParameterValue("out", "ClassificationFilterOutput.tif");
77

78
    SetOfficialDocLink();
79
  }
80

81
  void DoUpdateParameters() ITK_OVERRIDE
82
  {
83
    //UpdateInternalParameters("");
84 85
  }

86
  void DoExecute() ITK_OVERRIDE
87
  {
88
    KMeansFileNamesHandler fileNames;
89

90
    std::string fieldName = "field";
91

92 93
    fileNames.CreateTemporaryFileNames(GetParameterString( "out" ));
    otbAppLogINFO(" init filename : " << fileNames.tmpVectorFile);      //RM
94

95 96
    ComputeImageEnvelope(fileNames.tmpVectorFile);
    ComputeAddField(fileNames.tmpVectorFile, fieldName);
97

98 99
    UpdateKMPolygonClassStatisticsParameters(fileNames.tmpVectorFile);
    ComputePolygonStatistics(fileNames.polyStatOutput, fieldName);
100

101
    const double theoricNBSamplesForKMeans = GetParameterInt("ts");
102
    const double upperThresholdNBSamplesForKMeans = 1000 * 1000;
103 104
    const double actualNBSamplesForKMeans = std::min(theoricNBSamplesForKMeans,
                                                     upperThresholdNBSamplesForKMeans);
105

106 107
    otbAppLogINFO(<< actualNBSamplesForKMeans << " is the maximum sample size that will be used." \
                  << std::endl);
108

109 110
    SelectAndExtractSamples(fileNames.sampleSelectOutput, fileNames.polyStatOutput, 
                            fieldName, fileNames.sampleExtractOutput);
111

112 113 114
    // todo RM
    //std::vector<std::string> sampleTrainFileName = {fileNames.sampleSelectOutput};
    //UpdateTrainKMModelParameters(sampleTrainFileName);
115

116
    TrainKMModel(GetParameterImage("in"), fileNames.sampleExtractOutput);
117

118 119 120
/* TODO cleanup
    // cleanup
    if( IsParameterEnabled( "cleanup" ) )
121
      {
122 123
      otbAppLogINFO( <<"Final clean-up ..." );
      fileNames.clear();                          // TODO create clear()
124
      }
125
*/
126 127
  }

128
private :
129

130 131 132 133 134 135 136 137 138 139 140 141
  void UpdateKMPolygonClassStatisticsParameters(const std::string &vectorFileName)
  {
    GetInternalApplication( "polystats" )->SetParameterString( "vec", vectorFileName, false );
    UpdateInternalParameters( "polystats" );
  }
  /*
  void UpdateTrainKMModelParameters(const std::vector<std::string> &sampleTrainFileName)
  {
    GetInternalApplication( "training" )->SetParameterStringList( "io.vd", sampleTrainFileName, false );
    UpdateInternalParameters( "training" );
  }
  */
142
};
143

144

145 146
}
}
147

148
OTB_APPLICATION_EXPORT(otb::Wrapper::KMeansClassification)
149 150