otbLSGRM.cxx 5.56 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"

remicres's avatar
remicres committed
18
19
20
21
22
23
24
namespace otb
{

namespace Wrapper
{

class LSGRM : public Application
25
{
remicres's avatar
remicres committed
26
27
28
29
30
31
32
33
34
35
36
37
38
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:

39
40
41
42
43
44
45
46
  /* Tiling mode choice */
  enum TilingMode
  {
    TILING_AUTO,
    TILING_USER,
    TILING_NONE
  };

remicres's avatar
remicres committed
47
48
49
50
51
52
53
54
  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
55
56
  AddParameter(ParameterType_OutputImage, "out", "Ouput Label Image");
  SetDefaultOutputPixelType("out", ImagePixelType_uint32);
remicres's avatar
remicres committed
57
58
59
60
61
62
63
64

//  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");

65
66
  AddParameter(ParameterType_Int, "niter", "Maximum number of iterations");
  SetDefaultParameterInt("niter", 75);
remicres's avatar
remicres committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  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
83
84
85
86
87
88
89
  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");
90
  AddChoice("tiling.none", "No tiling layout");
remicres's avatar
remicres committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  }

  void DoUpdateParameters()
  {
  }

  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
112
    ImageType::Pointer inputImage = GetParameterFloatVectorImage("in");
remicres's avatar
remicres committed
113
114

    ControllerType controller;
remicres's avatar
remicres committed
115
    controller.SetInputImage(inputImage);
remicres's avatar
remicres committed
116
117
    controller.SetTemporaryDirectory(GetParameterAsString("tmpdir"));

118
119
120
    // Tiling mode
    int inputTilingMode = GetParameterInt("tiling");
    if (inputTilingMode == TILING_AUTO)
remicres's avatar
remicres committed
121
      {
122
      controller.SetTilingModeAuto();
remicres's avatar
remicres committed
123
      }
124
    else if (inputTilingMode == TILING_USER)
remicres's avatar
remicres committed
125
      {
126
      // User
127
      controller.SetTilingModeUser();
remicres's avatar
remicres committed
128
129
130
131
132
      controller.SetTileWidth(GetParameterInt("tiling.user.sizex"));
      controller.SetTileHeight(GetParameterInt("tiling.user.sizey"));
      controller.SetNumberOfFirstIterations(GetParameterInt("tiling.user.nfirstiter"));
      controller.SetInternalMemoryAvailable(GetParameterInt("tiling.user.memory"));
      }
133
134
135
136
137
138
139
140
    else if (inputTilingMode == TILING_NONE)
      {
      controller.SetTilingModeNone();
      }
    else
      {
      otbAppLogFATAL("Unknow input tiling mode!");
      }
remicres's avatar
remicres committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

    // 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);
157
    controller.SetNumberOfIterations(GetParameterInt("niter"));
remicres's avatar
remicres committed
158

remicres's avatar
remicres committed
159
    // Run the segmentation
160
    controller.SetDebug(true);
remicres's avatar
remicres committed
161
162
    controller.RunSegmentation();

remicres's avatar
remicres committed
163
164
165
166
167
168
169
170
171
    // 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
172
173
174
175
176
177
178
179
180
181
182
183
  }

  void AfterExecuteAndWriteOutputs()
  {
  }

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


OTB_APPLICATION_EXPORT(otb::Wrapper::LSGRM)
184