Commit 2ed36bce authored by remicres's avatar remicres
Browse files

ADD: criterion can be chosen amongst bs/ed/fls

Showing with 122 additions and 60 deletions
+122 -60
...@@ -12,7 +12,9 @@ ...@@ -12,7 +12,9 @@
// GRM // GRM
#include <iostream> #include <iostream>
#include "lsrmBaatzSegmenter.h" #include "lsgrmBaatzSegmenter.h"
#include "lsgrmSpringSegmenter.h"
#include "lsgrmFullLambdaScheduleSegmenter.h"
#include "lsgrmController.h" #include "lsgrmController.h"
// system tools // system tools
...@@ -28,7 +30,7 @@ class LSGRM : public Application ...@@ -28,7 +30,7 @@ class LSGRM : public Application
{ {
public: public:
/** Standard class typedefs. */ /** Standard class typedefs. */
typedef LSGRM Self; typedef LSGRM Self;
typedef Application Superclass; typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer; typedef itk::SmartPointer<const Self> ConstPointer;
...@@ -37,6 +39,15 @@ public: ...@@ -37,6 +39,15 @@ public:
itkNewMacro(Self); itkNewMacro(Self);
itkTypeMacro(LSGRM, Application); itkTypeMacro(LSGRM, Application);
/** Useful typedefs */
typedef otb::VectorImage<float, 2> ImageType;
typedef lsgrm::BaatzSegmenter<ImageType> BaatzSegmenterType;
typedef lsgrm::SpringSegmenter<ImageType> SpringSegmenterType;
typedef lsgrm::FullLambdaScheduleSegmenter<ImageType> FLSSegmenterType;
typedef lsgrm::Controller<BaatzSegmenterType> BaatzControllerType;
typedef lsgrm::Controller<SpringSegmenterType> SpringControllerType;
typedef lsgrm::Controller<FLSSegmenterType> FLSControllerType;
private: private:
/* Tiling mode choice */ /* Tiling mode choice */
...@@ -47,6 +58,14 @@ private: ...@@ -47,6 +58,14 @@ private:
TILING_NONE TILING_NONE
}; };
/* Criterion choice */
enum Criterion
{
CRITERION_BAATZ,
CRITERION_SPRING,
CRITERION_FLS
};
void DoInit() void DoInit()
{ {
SetName("GenericRegionMerging"); SetName("GenericRegionMerging");
...@@ -54,35 +73,33 @@ private: ...@@ -54,35 +73,33 @@ private:
"(GRM) and provides currently 3 homogeneity criteria: Euclidean Distance, " "(GRM) and provides currently 3 homogeneity criteria: Euclidean Distance, "
"Full Lambda Schedule and Baatz & Schape criterion."); "Full Lambda Schedule and Baatz & Schape criterion.");
// Input and Output images
AddParameter(ParameterType_InputImage, "in", "Input Image"); AddParameter(ParameterType_InputImage, "in", "Input Image");
AddParameter(ParameterType_OutputImage, "out", "Ouput Label Image"); AddParameter(ParameterType_OutputImage, "out", "Ouput Label Image");
SetDefaultOutputPixelType("out", ImagePixelType_uint32); SetDefaultOutputPixelType("out", ImagePixelType_uint32);
// AddParameter(ParameterType_Choice, "criterion", "Homogeneity criterion to use"); // Criterion choice
// AddChoice("criterion.bs", "Baatz & Schape"); AddParameter(ParameterType_Choice, "criterion", "Homogeneity criterion to use");
// AddChoice("criterion.ed", "Euclidean Distance"); AddChoice("criterion.bs", "Baatz & Schape");
// AddChoice("criterion.fls", "Full Lambda Schedule"); AddChoice("criterion.ed", "Euclidean Distance");
AddChoice("criterion.fls", "Full Lambda Schedule");
// Generic parameters
AddParameter(ParameterType_Float, "threshold", "Threshold for the criterion"); AddParameter(ParameterType_Float, "threshold", "Threshold for the criterion");
AddParameter(ParameterType_Int, "niter", "Maximum number of iterations"); AddParameter(ParameterType_Int, "niter", "Maximum number of iterations");
SetDefaultParameterInt("niter", 75); SetDefaultParameterInt("niter", 75);
MandatoryOff("niter"); MandatoryOff("niter");
AddParameter(ParameterType_Int, "speed", "Activate it to boost the segmentation speed"); // Specific parameters for Baatz & Schape
SetDefaultParameterInt("speed", 0); AddParameter(ParameterType_Float, "criterion.bs.cw", "Weight for the spectral homogeneity");
MandatoryOff("speed"); SetDefaultParameterFloat("criterion.bs.cw", 0.5);
MandatoryOff("criterion.bs.cw");
// For Baatz & Schape AddParameter(ParameterType_Float, "criterion.bs.sw", "Weight for the spatial homogeneity");
AddParameter(ParameterType_Float, "cw", "Weight for the spectral homogeneity"); SetDefaultParameterFloat("criterion.bs.sw", 0.5);
SetDefaultParameterFloat("cw", 0.5); MandatoryOff("criterion.bs.sw");
MandatoryOff("cw");
AddParameter(ParameterType_Float, "sw", "Weight for the spatial homogeneity");
SetDefaultParameterFloat("sw", 0.5);
MandatoryOff("sw");
// For large scale // For large scale
AddParameter(ParameterType_Directory, "tmpdir", "temporary directory for tiles and graphs"); AddParameter(ParameterType_Directory, "tmpdir", "Directory for temporary files");
AddParameter(ParameterType_Choice, "tiling", "Tiling layout for the large scale segmentation"); AddParameter(ParameterType_Choice, "tiling", "Tiling layout for the large scale segmentation");
AddChoice("tiling.auto", "Automatic tiling layout"); AddChoice("tiling.auto", "Automatic tiling layout");
AddChoice("tiling.user", "User tiling layout"); AddChoice("tiling.user", "User tiling layout");
...@@ -97,6 +114,7 @@ private: ...@@ -97,6 +114,7 @@ private:
{ {
} }
/* /*
* Return a prefix for temporary files * Return a prefix for temporary files
*/ */
...@@ -122,66 +140,112 @@ private: ...@@ -122,66 +140,112 @@ private:
return prefix; return prefix;
} }
void DoExecute() /*
* This function sets the generic parameters of a controller and runs the segmentation
*/
template<class TController>
UInt32ImageType::Pointer SetGenericParametersAndRunSegmentation(typename TController::Pointer& controller)
{ {
/* // Set input image
To add: controller->SetInputImage(GetParameterFloatVectorImage("in"));
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
*/ // Set threshold
float thres = GetParameterFloat("threshold");
using ImageType = otb::VectorImage<float, 2>; controller->SetThreshold(thres*thres);
using SegmenterType = lsgrm::BaatzSegmenter<ImageType>;
using ControllerType = lsgrm::Controller<SegmenterType>;
ImageType::Pointer inputImage = GetParameterFloatVectorImage("in"); // Set number of iterations
controller->SetNumberOfIterations(GetParameterInt("niter"));
ControllerType controller; // Set temporary files prefix
controller.SetInputImage(inputImage); controller->SetTemporaryFilesPrefix(this->GetTemporaryFilesPrefix());
controller.SetTemporaryFilesPrefix(this->GetTemporaryFilesPrefix());
// Tiling mode // Switch tiling mode
int inputTilingMode = GetParameterInt("tiling"); int inputTilingMode = GetParameterInt("tiling");
if (inputTilingMode == TILING_AUTO) if (inputTilingMode == TILING_AUTO)
{ {
controller.SetTilingModeAuto(); // Automatic mode
controller->SetTilingModeAuto();
} }
else if (inputTilingMode == TILING_USER) else if (inputTilingMode == TILING_USER)
{ {
// User // User mode
controller.SetTilingModeUser(); controller->SetTilingModeUser();
controller.SetTileWidth(GetParameterInt("tiling.user.sizex")); controller->SetTileWidth(GetParameterInt("tiling.user.sizex"));
controller.SetTileHeight(GetParameterInt("tiling.user.sizey")); controller->SetTileHeight(GetParameterInt("tiling.user.sizey"));
controller.SetNumberOfFirstIterations(GetParameterInt("tiling.user.nfirstiter")); controller->SetNumberOfFirstIterations(GetParameterInt("tiling.user.nfirstiter"));
controller.SetInternalMemoryAvailable(GetParameterInt("tiling.user.memory")); controller->SetInternalMemoryAvailable(GetParameterInt("tiling.user.memory"));
} }
else if (inputTilingMode == TILING_NONE) else if (inputTilingMode == TILING_NONE)
{ {
controller.SetTilingModeNone(); // None mode
controller->SetTilingModeNone();
} }
else else
{ {
otbAppLogFATAL("Unknow input tiling mode!"); otbAppLogFATAL("Unknow tiling mode!");
} }
// Specific parameters
grm::BaatzParam params;
params.m_SpectralWeight = GetParameterFloat("cw");
params.m_ShapeWeight = GetParameterFloat("sw");
controller.SetSpecificParameters(params);
float thres = GetParameterFloat("threshold");
controller.SetThreshold(thres*thres);
controller.SetNumberOfIterations(GetParameterInt("niter"));
// Run the segmentation // Run the segmentation
controller.SetDebug(true); controller->RunSegmentation();
controller.RunSegmentation();
// Output images // Get temporary files list
UInt32ImageType::Pointer labelImage = controller.GetLabeledClusteredOutput();; m_TemporaryFilesList = controller->GetTemporaryFilesList();
// Return the label image
return controller->GetLabeledClusteredOutput();
}
void DoExecute()
{
/*
To add:
the output directory in case the global graph cannot fit in memory
*/
// Input image
ImageType::Pointer inputImage = GetParameterFloatVectorImage("in");
// Output image
UInt32ImageType::Pointer labelImage = UInt32ImageType::New();
// Switch criterion
int inputCriterion = GetParameterInt("criterion");
if (inputCriterion == CRITERION_BAATZ)
{
// Baatz controller
BaatzControllerType::Pointer baatzController = BaatzControllerType::New();
// Specific parameters
grm::BaatzParam params;
params.m_SpectralWeight = GetParameterFloat("criterion.bs.cw");
params.m_ShapeWeight = GetParameterFloat("criterion.bs.sw");
baatzController->SetSpecificParameters(params);
// Run segmentation
labelImage = SetGenericParametersAndRunSegmentation<BaatzControllerType>(baatzController);
}
else if (inputCriterion == CRITERION_SPRING)
{
// Spring controller
SpringControllerType::Pointer springController = SpringControllerType::New();
// Run segmentation
labelImage = SetGenericParametersAndRunSegmentation<SpringControllerType>(springController);
}
else if (inputCriterion == CRITERION_FLS)
{
// Full Lambda Schedule (FLS) controller
FLSControllerType::Pointer flsController = FLSControllerType::New();
// Run segmentation
labelImage = SetGenericParametersAndRunSegmentation<FLSControllerType>(flsController);
}
else
{
otbAppLogFATAL("Unknow criterion!")
}
// Set output image projection, origin and spacing for labelImage // Set output image projection, origin and spacing for labelImage
labelImage->SetProjectionRef(inputImage->GetProjectionRef()); labelImage->SetProjectionRef(inputImage->GetProjectionRef());
...@@ -189,8 +253,6 @@ private: ...@@ -189,8 +253,6 @@ private:
labelImage->SetSpacing(inputImage->GetSpacing()); labelImage->SetSpacing(inputImage->GetSpacing());
SetParameterOutputImage<UInt32ImageType>("out", labelImage); SetParameterOutputImage<UInt32ImageType>("out", labelImage);
// Get temporary files list
m_TemporaryFilesList = controller.GetTemporaryFilesList();
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment