otbLSGRM.cxx 5.12 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
39
40
41
42
43
44
45
46
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:

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

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

57
58
  AddParameter(ParameterType_Int, "niter", "Maximum number of iterations");
  SetDefaultParameterInt("niter", 75);
remicres's avatar
remicres committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  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
75
76
77
78
79
80
81
  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");
remicres's avatar
remicres committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  }

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

    ControllerType controller;
remicres's avatar
remicres committed
106
    controller.SetInputImage(inputImage);
remicres's avatar
remicres committed
107
108
109
    controller.SetTemporaryDirectory(GetParameterAsString("tmpdir"));

    // Memory configuration
remicres's avatar
remicres committed
110
111
112
113
114
115
    if (GetParameterInt("tiling") == 0)
      {
      controller.SetInternalMemoryAvailable(0);  // 0 = automatic
      }
    else
      {
116
      // User
remicres's avatar
remicres committed
117
118
119
120
121
      controller.SetTileWidth(GetParameterInt("tiling.user.sizex"));
      controller.SetTileHeight(GetParameterInt("tiling.user.sizey"));
      controller.SetNumberOfFirstIterations(GetParameterInt("tiling.user.nfirstiter"));
      controller.SetInternalMemoryAvailable(GetParameterInt("tiling.user.memory"));
      }
remicres's avatar
remicres committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

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

remicres's avatar
remicres committed
140
    // Run the segmentation
remicres's avatar
remicres committed
141
142
    controller.RunSegmentation();

remicres's avatar
remicres committed
143
144
145
146
147
148
149
150
151
    // 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
152
153
154
155
156
157
158
159
160
161
162
163
  }

  void AfterExecuteAndWriteOutputs()
  {
  }

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


OTB_APPLICATION_EXPORT(otb::Wrapper::LSGRM)
164