otbKMeansClassification.cxx 4.24 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
    AddDocTag(Tags::Segmentation);

    // Perform initialization
    ClearApplications();

60
    // initialisation parameters and synchronizes parameters
61 62
    initKMParams();

Marina Bertolino's avatar
Marina Bertolino committed
63
    if (IsParameterEnabled("vm") && HasValue("vm")) ConnectKMClassificationMask();
64

65 66
    AddRANDParameter();

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

75
    SetOfficialDocLink();
76
  }
77

78
  void DoUpdateParameters() ITK_OVERRIDE
79 80 81
  {
  }

82
  void DoExecute() ITK_OVERRIDE
83
  {
84
    KMeansFileNamesHandler fileNames;
85

86
    std::string fieldName = "field";
87

88
    fileNames.CreateTemporaryFileNames(GetParameterString( "out" ));
89

90
    // Create an image envelope
91
    ComputeImageEnvelope(fileNames.tmpVectorFile);
92
    // Add a new field at the ImageEnvelope output file
93
    ComputeAddField(fileNames.tmpVectorFile, fieldName);
94

95
    // Compute PolygonStatistics app
96 97
    UpdateKMPolygonClassStatisticsParameters(fileNames.tmpVectorFile);
    ComputePolygonStatistics(fileNames.polyStatOutput, fieldName);
98

99 100 101 102 103
    // Compute number of sample max for KMeans
    const int theoricNBSamplesForKMeans = GetParameterInt("ts");
    const int upperThresholdNBSamplesForKMeans = 1000 * 1000;
    const int actualNBSamplesForKMeans = std::min(theoricNBSamplesForKMeans,
                                                  upperThresholdNBSamplesForKMeans);
104 105
    otbAppLogINFO(<< actualNBSamplesForKMeans << " is the maximum sample size that will be used." \
                  << std::endl);
106

107
    // Compute SampleSelection and SampleExtraction app
108 109 110
    SelectAndExtractSamples(fileNames.polyStatOutput, fieldName,
                            fileNames.sampleSelectOutput,
                            fileNames.sampleExtractOutput,
111
                            actualNBSamplesForKMeans);
112

113 114 115
    // Compute a train model with TrainVectorClassifier app
    TrainKMModel(GetParameterImage("in"), fileNames.sampleExtractOutput,
                 fileNames.modelFile);
116

117 118
    // Compute a classification of the input image according to a model file
    KMeansClassif();
119

120
    // remove all tempory files
121
    if( IsParameterEnabled( "cleanup" ) )
122
      {
123
      otbAppLogINFO( <<"Final clean-up ..." );
124
      fileNames.clear();
125
      }
126

127 128
  }

129 130 131 132 133 134
private :
  void UpdateKMPolygonClassStatisticsParameters(const std::string &vectorFileName)
  {
    GetInternalApplication( "polystats" )->SetParameterString( "vec", vectorFileName, false );
    UpdateInternalParameters( "polystats" );
  }
135

136
};
137

138 139
}
}
140

141
OTB_APPLICATION_EXPORT(otb::Wrapper::KMeansClassification)
142 143