otbLSGRM.cxx 4.13 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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");
//  AddParameter(ParameterType_OutputImage, "out", "Ouput Label Image");

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

  AddParameter(ParameterType_Int, "niter", "Number of iterations");
  SetDefaultParameterInt("niter", 0);
  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");
  AddParameter(ParameterType_Int, "tsizex", "tile width");
  AddParameter(ParameterType_Int, "tsizey", "tile height");
  AddParameter(ParameterType_Int, "nfirstiter", "number of first iterations");
  }

  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>;


    ControllerType controller;
    controller.SetInputImage(GetParameterFloatVectorImage("in"));
    controller.SetTileDirectory(GetParameterAsString("tmpdir"));
    controller.SetTemporaryDirectory(GetParameterAsString("tmpdir"));
    controller.SetOutputGraphDirectory(GetParameterAsString("tmpdir"));

    // Memory configuration
    controller.SetInternalMemoryAvailable(0); // 0 = automatic
    controller.SetTileWidth(GetParameterInt("tsizex"));
    controller.SetTileHeight(GetParameterInt("tsizey"));
    controller.SetNumberOfFirstIterations(GetParameterInt("nfirstiter"));

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

    controller.RunSegmentation();

  }

  void AfterExecuteAndWriteOutputs()
  {
  }

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


OTB_APPLICATION_EXPORT(otb::Wrapper::LSGRM)
141