otbClassificationMapRegularization.cxx 8.22 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 22 23 24 25 26 27 28 29 30


// Wrappers
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"

// Majority Voting filter includes
#include "otbNeighborhoodMajorityVotingImageFilter.h"

namespace otb
{
31 32 33
namespace Wrapper
{

34
class ClassificationMapRegularization : public Application
35 36 37
{
public:
  /** Standard class typedefs. */
38
  typedef ClassificationMapRegularization            Self;
39 40 41 42 43 44 45
  typedef Application                   Superclass;
  typedef itk::SmartPointer<Self>       Pointer;
  typedef itk::SmartPointer<const Self> ConstPointer;

  /** Standard macro */
  itkNewMacro(Self);

46
  itkTypeMacro(ClassificationMapRegularization, otb::Application);
47

OTB Bot's avatar
STYLE  
OTB Bot committed
48
  /** Filters typedef */
49
  typedef UInt16ImageType IOLabelImageType;
50

51
  // Neighborhood majority voting filter type
52
  typedef otb::NeighborhoodMajorityVotingImageFilter<IOLabelImageType> NeighborhoodMajorityVotingFilterType;
53

54 55
  // Binary ball Structuring Element type
  typedef NeighborhoodMajorityVotingFilterType::KernelType StructuringType;
56 57 58 59
  typedef StructuringType::RadiusType RadiusType;


private:
60
  void DoInit() override
61
  {
62
    SetName("ClassificationMapRegularization");
63 64
    SetDescription("Filters the input labeled image using Majority Voting in a ball shaped neighbordhood");

65
    SetDocName("Classification Map Regularization");
66 67 68 69 70 71 72 73

    SetDocLongDescription(
        "This application filters the input labeled image (with a maximal class label = 65535) using Majority Voting in a ball shaped neighbordhood."
        "Majority Voting takes the more representative value of all the pixels identified by the ball shaped structuring element and then sets the center pixel to this majority label value.\n\n"

        "* NoData is the label of the NOT classified pixels in the input image. These input pixels keep their NoData label in the output image.\n\n"
        "* Pixels with more than 1 majority class are marked as Undecided if the parameter 'ip.suvbool == true', or keep their Original labels otherwise.");

74
    SetDocLimitations("The input image must be a single band labeled image (with a maximal class label = 65535). The structuring element radius must have a minimum value equal to 1 pixel. Please note that the Undecided value must be different from existing labels in the input labeled image.");
75

76
    SetDocAuthors("OTB-Team");
77
    SetDocSeeAlso("");
78 79 80 81 82 83 84

    AddDocTag(Tags::Learning);
    AddDocTag(Tags::Analysis);


    /** GROUP IO CLASSIFICATION */
    AddParameter(ParameterType_Group,"io","Input and output images");
85
    SetParameterDescription("io","This group of parameters allows setting input and output images for classification map regularization by Majority Voting.");
86

87
    AddParameter(ParameterType_InputImage, "io.in",  "Input classification image");
88 89
    SetParameterDescription( "io.in", "The input labeled image to regularize.");

90 91
    AddParameter(ParameterType_OutputImage, "io.out",  "Output regularized image");
    SetParameterDescription( "io.out", "The output regularized labeled image.");
92
    SetDefaultOutputPixelType( "io.out", ImagePixelType_uint8);
93 94


95
    AddParameter(ParameterType_Group,"ip","Regularization parameters");
96
    SetParameterDescription("ip","This group allows setting parameters for classification map regularization by Majority Voting.");
97

98 99 100
    AddParameter(ParameterType_Int, "ip.radius", "Structuring element radius (in pixels)");
    SetParameterDescription("ip.radius", "The radius of the ball shaped structuring element (expressed in pixels). By default, 'ip.radius = 1 pixel'.");
    SetDefaultParameterInt("ip.radius", 1.0);
101

102
    AddParameter(ParameterType_Bool, "ip.suvbool", "Multiple majority: Undecided(X)/Original");
103
    SetParameterDescription("ip.suvbool", "Pixels with more than 1 majority class are marked as Undecided if this parameter is checked (true), or keep their Original labels otherwise (false). Please note that the Undecided value must be different from existing labels in the input labeled image. By default, 'ip.suvbool = false'.");
104

105
    AddParameter(ParameterType_Int, "ip.nodatalabel", "Label for the NoData class");
106
    SetParameterDescription("ip.nodatalabel", "Label for the NoData class. Such input pixels keep their NoData label in the output image. By default, 'ip.nodatalabel = 0'.");
107
    SetDefaultParameterInt("ip.nodatalabel", 0.0);
108

109
    AddParameter(ParameterType_Int, "ip.undecidedlabel", "Label for the Undecided class");
110
    SetParameterDescription("ip.undecidedlabel", "Label for the Undecided class. By default, 'ip.undecidedlabel = 0'.");
111
    SetDefaultParameterInt("ip.undecidedlabel", 0.0);
112

113
    AddParameter(ParameterType_Bool, "ip.onlyisolatedpixels", "Process isolated pixels only");
114
    SetParameterDescription("ip.onlyisolatedpixels", "Only pixels whose label is unique in the neighbordhood will be processed. By default, 'ip.onlyisolatedpixels = false'.");
115

116 117 118
    AddParameter(ParameterType_Int, "ip.isolatedthreshold", "Threshold for isolated pixels");
    SetParameterDescription("ip.isolatedthreshold", "Maximum number of neighbours with the same label as the center pixel to consider that it is an isolated pixel. By default, 'ip.isolatedthreshold = 1'.");       
    SetDefaultParameterInt("ip.isolatedthreshold", 1);
119

120 121 122 123

    AddRAMParameter();

    // Doc example parameter settings
124 125 126
    SetDocExampleParameterValue("io.in", "clLabeledImageQB123_1.tif");
    SetDocExampleParameterValue("io.out", "clLabeledImageQB123_1_CMR_r2_nodl_10_undl_7.tif");
    SetDocExampleParameterValue("ip.radius", "2");
127
    SetDocExampleParameterValue("ip.suvbool", "true");
128
    SetDocExampleParameterValue("ip.onlyisolatedpixels", "true");
129 130
    SetDocExampleParameterValue("ip.nodatalabel", "10");
    SetDocExampleParameterValue("ip.undecidedlabel", "7");
131

132
    SetOfficialDocLink();
133 134
  }

135
  void DoUpdateParameters() override
136 137 138
  {
    // Nothing to do here : all parameters are independent
  }
139

140
  void DoExecute() override
141 142 143
  {
    // Majority Voting
    m_NeighMajVotingFilter = NeighborhoodMajorityVotingFilterType::New();
144

145
    // Load input labeled image to regularize
146
    UInt16ImageType::Pointer inImage = GetParameterUInt16Image("io.in");
147

148
    // Neighborhood majority voting filter settings
149
    RadiusType rad;
150 151
    rad[0] = GetParameterInt("ip.radius");
    rad[1] = GetParameterInt("ip.radius");
152

153
    StructuringType seBall;
154 155 156
    seBall.SetRadius(rad);
    seBall.CreateStructuringElement();
    m_NeighMajVotingFilter->SetKernel(seBall);
157

158
    m_NeighMajVotingFilter->SetInput(inImage);
159 160
    m_NeighMajVotingFilter->SetLabelForNoDataPixels(GetParameterInt("ip.nodatalabel"));
    m_NeighMajVotingFilter->SetLabelForUndecidedPixels(GetParameterInt("ip.undecidedlabel"));
161

162
    // Set to Undecided label if NOT unique Majority Voting
163
    if (GetParameterInt("ip.suvbool"))
164
      {
165
      m_NeighMajVotingFilter->SetKeepOriginalLabelBool(false);
166
      }
167
    // Keep Original label value if NOT unique Majority Voting
168
    else
169
      {
170
      m_NeighMajVotingFilter->SetKeepOriginalLabelBool(true);
171
      }
172

173
    // Process isolated pixels only
174
    if (GetParameterInt("ip.onlyisolatedpixels"))
175 176
      {
      m_NeighMajVotingFilter->SetOnlyIsolatedPixels(true);
177
      m_NeighMajVotingFilter->SetIsolatedThreshold(GetParameterInt("ip.isolatedthreshold"));
178 179 180 181 182 183
      }
    else
      {
      m_NeighMajVotingFilter->SetOnlyIsolatedPixels(false);
      }

184
    /** REGULARIZATION OF CLASSIFICATION */
185
    SetParameterOutputImage<IOLabelImageType>("io.out", m_NeighMajVotingFilter->GetOutput());
186

187
  }// END DoExecute()
188

189 190

  NeighborhoodMajorityVotingFilterType::Pointer m_NeighMajVotingFilter;
191
}; // END class ClassificationMapRegularization
192 193


194 195
}// END namespace wrapper
}// END namespace otb
196

197
OTB_APPLICATION_EXPORT(otb::Wrapper::ClassificationMapRegularization)