otbLSGRM.cxx 6.32 KB
Newer Older
remicres's avatar
remicres committed
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "itkFixedArray.h"
#include "itkObjectFactory.h"

// Elevation handler
#include "otbWrapperElevationParametersHandler.h"
#include "otbWrapperApplicationFactory.h"

// Application engine
#include "otbStandardFilterWatcher.h"
#include "itkFixedArray.h"
#include "itkImageSource.h"

// GRM
14
15
16
17
#include <iostream>
#include "lsrmBaatzSegmenter.h"
#include "lsgrmController.h"

18
19
20
// system tools
#include <itksys/SystemTools.hxx>

remicres's avatar
remicres committed
21
22
23
24
25
26
27
namespace otb
{

namespace Wrapper
{

class LSGRM : public Application
28
{
remicres's avatar
remicres committed
29
30
31
32
33
34
35
36
37
38
39
40
41
public:
  /** Standard class typedefs. */
  typedef LSGRM                        Self;
  typedef Application                   Superclass;
  typedef itk::SmartPointer<Self>       Pointer;
  typedef itk::SmartPointer<const Self> ConstPointer;

  /** Standard macro */
  itkNewMacro(Self);
  itkTypeMacro(LSGRM, Application);

private:

42
43
44
45
46
47
48
49
  /* Tiling mode choice */
  enum TilingMode
  {
    TILING_AUTO,
    TILING_USER,
    TILING_NONE
  };

remicres's avatar
remicres committed
50
51
52
53
54
55
56
57
  void DoInit()
  {
  SetName("GenericRegionMerging");
  SetDescription("This application allows to use the Generic Region Merging library "
      "(GRM) and provides currently 3 homogeneity criteria: Euclidean Distance, "
      "Full Lambda Schedule and Baatz & Schape criterion.");

  AddParameter(ParameterType_InputImage, "in", "Input Image");
remicres's avatar
remicres committed
58
59
  AddParameter(ParameterType_OutputImage, "out", "Ouput Label Image");
  SetDefaultOutputPixelType("out", ImagePixelType_uint32);
remicres's avatar
remicres committed
60
61
62
63
64
65
66
67

//  AddParameter(ParameterType_Choice, "criterion", "Homogeneity criterion to use");
//  AddChoice("criterion.bs", "Baatz & Schape");
//  AddChoice("criterion.ed", "Euclidean Distance");
//  AddChoice("criterion.fls", "Full Lambda Schedule");

  AddParameter(ParameterType_Float, "threshold", "Threshold for the criterion");

68
69
  AddParameter(ParameterType_Int, "niter", "Maximum number of iterations");
  SetDefaultParameterInt("niter", 75);
remicres's avatar
remicres committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  MandatoryOff("niter");

  AddParameter(ParameterType_Int, "speed", "Activate it to boost the segmentation speed");
  SetDefaultParameterInt("speed", 0);
  MandatoryOff("speed");

  // For Baatz & Schape
  AddParameter(ParameterType_Float, "cw", "Weight for the spectral homogeneity");
  SetDefaultParameterFloat("cw", 0.5);
  MandatoryOff("cw");
  AddParameter(ParameterType_Float, "sw", "Weight for the spatial homogeneity");
  SetDefaultParameterFloat("sw", 0.5);
  MandatoryOff("sw");

  // For large scale
  AddParameter(ParameterType_Directory, "tmpdir", "temporary directory for tiles and graphs");
remicres's avatar
remicres committed
86
87
88
89
90
91
92
  AddParameter(ParameterType_Choice, "tiling", "Tiling layout for the large scale segmentation");
  AddChoice("tiling.auto", "Automatic tiling layout");
  AddChoice("tiling.user", "User tiling layout");
  AddParameter(ParameterType_Int, "tiling.user.sizex", "Tiles width");
  AddParameter(ParameterType_Int, "tiling.user.sizey", "Tiles height");
  AddParameter(ParameterType_Int, "tiling.user.nfirstiter", "Number of first iterations");
  AddParameter(ParameterType_Int, "tiling.user.memory", "Available memory");
93
  AddChoice("tiling.none", "No tiling layout");
remicres's avatar
remicres committed
94
95
96
97
98
99
  }

  void DoUpdateParameters()
  {
  }

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  /*
   * Return a prefix for temporary files
   */
  std::string GetTemporaryFilesPrefix()
  {

    // Get output filename (without extension)
    std::string outfname = GetParameterString("out");
    std::string outbfname = itksys::SystemTools::GetFilenameWithoutExtension(outfname.c_str());

    // Get specified temporary directory
    std::string tmpdir = GetParameterAsString("tmpdir");
    if (!tmpdir.empty())
      {
      // A temporary directory is specified: we check that it ends with a POSIX separator
      if (tmpdir[tmpdir.size()-1] != '/')
        {
        // If not, we add the separator
        tmpdir.append("/");
        }
      }
    std::string prefix = tmpdir + outbfname;
    return prefix;
  }

remicres's avatar
remicres committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  void DoExecute()
  {
    /*
        To add:
        internal memory available
        If we have to do the image division
        if we have to clean up the directory
        the output directory in case the global graph cannot fit in memory

     */

    using ImageType = otb::VectorImage<float, 2>;
    using SegmenterType = lsrm::BaatzSegmenter<ImageType>;
    using ControllerType = lsgrm::Controller<SegmenterType>;

remicres's avatar
remicres committed
140
    ImageType::Pointer inputImage = GetParameterFloatVectorImage("in");
remicres's avatar
remicres committed
141
142

    ControllerType controller;
remicres's avatar
remicres committed
143
    controller.SetInputImage(inputImage);
144
    controller.SetTemporaryFilesPrefix(this->GetTemporaryFilesPrefix());
remicres's avatar
remicres committed
145

146
147
148
    // Tiling mode
    int inputTilingMode = GetParameterInt("tiling");
    if (inputTilingMode == TILING_AUTO)
remicres's avatar
remicres committed
149
      {
150
      controller.SetTilingModeAuto();
remicres's avatar
remicres committed
151
      }
152
    else if (inputTilingMode == TILING_USER)
remicres's avatar
remicres committed
153
      {
154
      // User
155
      controller.SetTilingModeUser();
remicres's avatar
remicres committed
156
157
158
159
160
      controller.SetTileWidth(GetParameterInt("tiling.user.sizex"));
      controller.SetTileHeight(GetParameterInt("tiling.user.sizey"));
      controller.SetNumberOfFirstIterations(GetParameterInt("tiling.user.nfirstiter"));
      controller.SetInternalMemoryAvailable(GetParameterInt("tiling.user.memory"));
      }
161
162
163
164
165
166
167
168
    else if (inputTilingMode == TILING_NONE)
      {
      controller.SetTilingModeNone();
      }
    else
      {
      otbAppLogFATAL("Unknow input tiling mode!");
      }
remicres's avatar
remicres committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

    // MPI configuration
#ifdef OTB_USE_MPI
    if (myrank==0)
      std::cout << "Number of MPI process : " << nprocs << std::endl;
    controller.SetNumberOfProcess(nprocs);
    controller.SetProcessRank(myrank);
#endif

    // Specific parameters
    lsrm::BaatzParam params;
    params.m_SpectralWeight = GetParameterFloat("cw");
    params.m_ShapeWeight = GetParameterFloat("sw");
    controller.SetSpecificParameters(params);
    float thres = GetParameterFloat("threshold");
    controller.SetThreshold(thres*thres);
185
    controller.SetNumberOfIterations(GetParameterInt("niter"));
remicres's avatar
remicres committed
186

remicres's avatar
remicres committed
187
    // Run the segmentation
188
    controller.SetDebug(true);
remicres's avatar
remicres committed
189
190
    controller.RunSegmentation();

remicres's avatar
remicres committed
191
192
193
194
195
196
197
198
199
    // Output images
    UInt32ImageType::Pointer labelImage = controller.GetLabeledClusteredOutput();;

    // Set output image projection, origin and spacing for labelImage
    labelImage->SetProjectionRef(inputImage->GetProjectionRef());
    labelImage->SetOrigin(inputImage->GetOrigin());
    labelImage->SetSpacing(inputImage->GetSpacing());
    SetParameterOutputImage<UInt32ImageType>("out", labelImage);

remicres's avatar
remicres committed
200
201
202
203
204
205
206
207
208
209
210
211
  }

  void AfterExecuteAndWriteOutputs()
  {
  }

}; // app class
} // end of namespace wrapper
} // end of namespace otb


OTB_APPLICATION_EXPORT(otb::Wrapper::LSGRM)
212