Commit aa288e4d authored by remicres's avatar remicres
Browse files

REFAC: KWStyle

parent a34732ef
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
#include "itkFixedArray.h" #include "itkFixedArray.h"
#include "itkImageSource.h" #include "itkImageSource.h"
// GRM // LSGRM
#include <iostream> #include <iostream>
#include "lsgrmBaatzSegmenter.h" #include "lsgrmBaatzSegmenter.h"
#include "lsgrmSpringSegmenter.h" #include "lsgrmSpringSegmenter.h"
...@@ -44,9 +44,6 @@ public: ...@@ -44,9 +44,6 @@ public:
typedef lsgrm::BaatzSegmenter<ImageType> BaatzSegmenterType; typedef lsgrm::BaatzSegmenter<ImageType> BaatzSegmenterType;
typedef lsgrm::SpringSegmenter<ImageType> SpringSegmenterType; typedef lsgrm::SpringSegmenter<ImageType> SpringSegmenterType;
typedef lsgrm::FullLambdaScheduleSegmenter<ImageType> FLSSegmenterType; typedef lsgrm::FullLambdaScheduleSegmenter<ImageType> FLSSegmenterType;
typedef lsgrm::Controller<BaatzSegmenterType> BaatzControllerType;
typedef lsgrm::Controller<SpringSegmenterType> SpringControllerType;
typedef lsgrm::Controller<FLSSegmenterType> FLSControllerType;
private: private:
...@@ -69,8 +66,8 @@ private: ...@@ -69,8 +66,8 @@ private:
void DoInit() void DoInit()
{ {
SetName("GenericRegionMerging"); SetName("GenericRegionMerging");
SetDescription("This application allows to use the Generic Region Merging library " SetDescription("This application allows to use the Large Scale Generic Region Merging library "
"(GRM) and provides currently 3 homogeneity criteria: Euclidean Distance, " "(LSGRM) 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 // Input and Output images
...@@ -114,7 +111,6 @@ private: ...@@ -114,7 +111,6 @@ private:
{ {
} }
/* /*
* Return a prefix for temporary files * Return a prefix for temporary files
*/ */
...@@ -143,9 +139,16 @@ private: ...@@ -143,9 +139,16 @@ private:
/* /*
* This function sets the generic parameters of a controller and runs the segmentation * This function sets the generic parameters of a controller and runs the segmentation
*/ */
template<class TController> template<class TSegmenter>
UInt32ImageType::Pointer SetGenericParametersAndRunSegmentation(typename TController::Pointer& controller) UInt32ImageType::Pointer SetGenericParametersAndRunSegmentation(const typename TSegmenter::ParamType params){
{
// Instantiate the controller
typedef typename lsgrm::Controller<TSegmenter> ControlerType;
typename ControlerType::Pointer controller = ControlerType::New();
// Set specific parameters
controller->SetSpecificParameters(params);
// Set input image // Set input image
controller->SetInputImage(GetParameterFloatVectorImage("in")); controller->SetInputImage(GetParameterFloatVectorImage("in"));
...@@ -195,8 +198,6 @@ private: ...@@ -195,8 +198,6 @@ private:
return controller->GetLabeledClusteredOutput(); return controller->GetLabeledClusteredOutput();
} }
void DoExecute() void DoExecute()
{ {
/* /*
...@@ -214,33 +215,20 @@ private: ...@@ -214,33 +215,20 @@ private:
int inputCriterion = GetParameterInt("criterion"); int inputCriterion = GetParameterInt("criterion");
if (inputCriterion == CRITERION_BAATZ) if (inputCriterion == CRITERION_BAATZ)
{ {
// Baatz controller
BaatzControllerType::Pointer baatzController = BaatzControllerType::New();
// Specific parameters
grm::BaatzParam params; grm::BaatzParam params;
params.m_SpectralWeight = GetParameterFloat("criterion.bs.cw"); params.m_SpectralWeight = GetParameterFloat("criterion.bs.cw");
params.m_ShapeWeight = GetParameterFloat("criterion.bs.sw"); params.m_ShapeWeight = GetParameterFloat("criterion.bs.sw");
baatzController->SetSpecificParameters(params); labelImage = SetGenericParametersAndRunSegmentation<BaatzSegmenterType>(params);
// Run segmentation
labelImage = SetGenericParametersAndRunSegmentation<BaatzControllerType>(baatzController);
} }
else if (inputCriterion == CRITERION_SPRING) else if (inputCriterion == CRITERION_SPRING)
{ {
// Spring controller grm::SpringParam params;
SpringControllerType::Pointer springController = SpringControllerType::New(); labelImage = SetGenericParametersAndRunSegmentation<SpringSegmenterType>(params);
// Run segmentation
labelImage = SetGenericParametersAndRunSegmentation<SpringControllerType>(springController);
} }
else if (inputCriterion == CRITERION_FLS) else if (inputCriterion == CRITERION_FLS)
{ {
// Full Lambda Schedule (FLS) controller grm::FLSParam params;
FLSControllerType::Pointer flsController = FLSControllerType::New(); labelImage = SetGenericParametersAndRunSegmentation<FLSSegmenterType>(params);
// Run segmentation
labelImage = SetGenericParametersAndRunSegmentation<FLSControllerType>(flsController);
} }
else else
{ {
...@@ -253,7 +241,6 @@ private: ...@@ -253,7 +241,6 @@ private:
labelImage->SetSpacing(inputImage->GetSpacing()); labelImage->SetSpacing(inputImage->GetSpacing());
SetParameterOutputImage<UInt32ImageType>("out", labelImage); SetParameterOutputImage<UInt32ImageType>("out", labelImage);
} }
void AfterExecuteAndWriteOutputs() void AfterExecuteAndWriteOutputs()
...@@ -275,6 +262,4 @@ private: ...@@ -275,6 +262,4 @@ private:
} // end of namespace wrapper } // end of namespace wrapper
} // end of namespace otb } // end of namespace otb
OTB_APPLICATION_EXPORT(otb::Wrapper::LSGRM) OTB_APPLICATION_EXPORT(otb::Wrapper::LSGRM)
...@@ -6,56 +6,56 @@ ...@@ -6,56 +6,56 @@
namespace lsgrm namespace lsgrm
{ {
template<class TImage> template<class TImage>
void void
BaatzSegmenter<TImage>::WriteSpecificAttributes(NodePointerType node, FILE * nodeStream) BaatzSegmenter<TImage>::WriteSpecificAttributes(NodePointerType node, FILE * nodeStream)
{ {
std::size_t bands = node->m_Means.size(); std::size_t bands = node->m_Means.size();
fwrite(&(bands), sizeof(bands), 1, nodeStream); fwrite(&(bands), sizeof(bands), 1, nodeStream);
for(unsigned int b = 0; b < node->m_Means.size(); b++) for(unsigned int b = 0; b < node->m_Means.size(); b++)
{
fwrite(&(node->m_Means[b]), sizeof(node->m_Means[b]), 1, nodeStream);
fwrite(&(node->m_SquareMeans[b]), sizeof(node->m_SquareMeans[b]), 1, nodeStream);
fwrite(&(node->m_SpectralSum[b]), sizeof(node->m_SpectralSum[b]), 1, nodeStream);
fwrite(&(node->m_Std[b]), sizeof(node->m_Std[b]), 1, nodeStream);
}
}
template<class TImage>
void
BaatzSegmenter<TImage>::ReadSpecificAttributes(NodePointerType node, FILE * nodeStream)
{ {
std::size_t bands; fwrite(&(node->m_Means[b]), sizeof(node->m_Means[b]), 1, nodeStream);
fread(&(bands), sizeof(bands), 1, nodeStream); fwrite(&(node->m_SquareMeans[b]), sizeof(node->m_SquareMeans[b]), 1, nodeStream);
node->m_Means.assign(bands, 0); fwrite(&(node->m_SpectralSum[b]), sizeof(node->m_SpectralSum[b]), 1, nodeStream);
node->m_SquareMeans.assign(bands, 0); fwrite(&(node->m_Std[b]), sizeof(node->m_Std[b]), 1, nodeStream);
node->m_SpectralSum.assign(bands, 0);
node->m_Std.assign(bands, 0);
for(unsigned int b = 0; b < bands; b++)
{
fread(&(node->m_Means[b]), sizeof(node->m_Means[b]), 1, nodeStream);
fread(&(node->m_SquareMeans[b]), sizeof(node->m_SquareMeans[b]), 1, nodeStream);
fread(&(node->m_SpectralSum[b]), sizeof(node->m_SpectralSum[b]), 1, nodeStream);
fread(&(node->m_Std[b]), sizeof(node->m_Std[b]), 1, nodeStream);
}
} }
}
template<class TImage> template<class TImage>
long long unsigned int void
BaatzSegmenter<TImage>::GetNodeMemory(NodePointerType &node) BaatzSegmenter<TImage>::ReadSpecificAttributes(NodePointerType node, FILE * nodeStream)
{
std::size_t bands;
fread(&(bands), sizeof(bands), 1, nodeStream);
node->m_Means.assign(bands, 0);
node->m_SquareMeans.assign(bands, 0);
node->m_SpectralSum.assign(bands, 0);
node->m_Std.assign(bands, 0);
for(unsigned int b = 0; b < bands; b++)
{ {
fread(&(node->m_Means[b]), sizeof(node->m_Means[b]), 1, nodeStream);
fread(&(node->m_SquareMeans[b]), sizeof(node->m_SquareMeans[b]), 1, nodeStream);
fread(&(node->m_SpectralSum[b]), sizeof(node->m_SpectralSum[b]), 1, nodeStream);
fread(&(node->m_Std[b]), sizeof(node->m_Std[b]), 1, nodeStream);
}
}
unsigned int nBands = node->m_Means.size(); //this->m_InputImage->GetNumberOfComponentsPerPixel(); template<class TImage>
long long unsigned int
BaatzSegmenter<TImage>::GetNodeMemory(NodePointerType &node)
{
long long unsigned int memory = 0; unsigned int nBands = node->m_Means.size(); //this->m_InputImage->GetNumberOfComponentsPerPixel();
memory += sizeof(NodePointerType); // size of the node pointer
memory += sizeof(NodeType); // size of the node (actually, size of the base node) long long unsigned int memory = 0;
memory += 4 * nBands * sizeof(float); // size of the 4 attributes, multiplied by the nb. of bands memory += sizeof(NodePointerType); // size of the node pointer
memory += node->m_Edges.size() * sizeof(EdgeType); // size of the edges memory += sizeof(NodeType); // size of the node (actually, size of the base node)
return memory; memory += 4 * nBands * sizeof(float); // size of the 4 attributes, multiplied by the nb. of bands
} memory += node->m_Edges.size() * sizeof(EdgeType); // size of the edges
return memory;
}
} // end of namespace lsrm } // end of namespace lsrm
#endif #endif
......
...@@ -108,10 +108,6 @@ void Controller<TSegmenter>::RunSegmentation() ...@@ -108,10 +108,6 @@ void Controller<TSegmenter>::RunSegmentation()
{ {
numberOfIterationsRemaining -= numberOfIterationsForPartialSegmentations; numberOfIterationsRemaining -= numberOfIterationsForPartialSegmentations;
} }
else
{
isFusion = false;
}
#ifdef OTB_USE_MPI #ifdef OTB_USE_MPI
// Gathering useful variables // Gathering useful variables
...@@ -152,11 +148,21 @@ void Controller<TSegmenter>::RunSegmentation() ...@@ -152,11 +148,21 @@ void Controller<TSegmenter>::RunSegmentation()
itkExceptionMacro(<< "No more possible fusions, but can not store the output graph"); itkExceptionMacro(<< "No more possible fusions, but can not store the output graph");
} }
} }
else // tiling_mode is none else if (m_TilingMode == LSGRM_TILING_NONE)// tiling_mode is none
{ {
// todo use classic grm // Use classic grm
TSegmenter segmenter;
segmenter.SetParam(m_SpecificParameters);
segmenter.SetThreshold(m_Threshold);
segmenter.SetDoFastSegmentation(true);
segmenter.SetNumberOfIterations(m_NumberOfIterations);
segmenter.SetInput(m_InputImage);
segmenter.Update();
}
else
{
itkExceptionMacro(<<"Unknow tiling mode!");
} }
} }
......
...@@ -6,47 +6,47 @@ ...@@ -6,47 +6,47 @@
namespace lsgrm namespace lsgrm
{ {
template<class TImage> template<class TImage>
void void
FullLambdaScheduleSegmenter<TImage>::WriteSpecificAttributes(NodePointerType node, FILE * nodeStream) FullLambdaScheduleSegmenter<TImage>::WriteSpecificAttributes(NodePointerType node, FILE * nodeStream)
{ {
std::size_t bands = node->m_Means.size(); std::size_t bands = node->m_Means.size();
fwrite(&(bands), sizeof(bands), 1, nodeStream); fwrite(&(bands), sizeof(bands), 1, nodeStream);
for(unsigned int b = 0; b < node->m_Means.size(); b++) for(unsigned int b = 0; b < node->m_Means.size(); b++)
{
fwrite(&(node->m_Means[b]), sizeof(node->m_Means[b]), 1, nodeStream);
}
}
template<class TImage>
void
FullLambdaScheduleSegmenter<TImage>::ReadSpecificAttributes(NodePointerType node, FILE * nodeStream)
{ {
std::size_t bands; fwrite(&(node->m_Means[b]), sizeof(node->m_Means[b]), 1, nodeStream);
fread(&(bands), sizeof(bands), 1, nodeStream);
node->m_Means.assign(bands, 0);
for(unsigned int b = 0; b < bands; b++)
{
fread(&(node->m_Means[b]), sizeof(node->m_Means[b]), 1, nodeStream);
}
} }
}
template<class TImage>
void
FullLambdaScheduleSegmenter<TImage>::ReadSpecificAttributes(NodePointerType node, FILE * nodeStream)
{
std::size_t bands;
fread(&(bands), sizeof(bands), 1, nodeStream);
node->m_Means.assign(bands, 0);
template<class TImage> for(unsigned int b = 0; b < bands; b++)
long long unsigned int
FullLambdaScheduleSegmenter<TImage>::GetNodeMemory(NodePointerType &node)
{ {
fread(&(node->m_Means[b]), sizeof(node->m_Means[b]), 1, nodeStream);
}
}
template<class TImage>
long long unsigned int
FullLambdaScheduleSegmenter<TImage>::GetNodeMemory(NodePointerType &node)
{
unsigned int nBands = node->m_Means.size(); //this->m_InputImage->GetNumberOfComponentsPerPixel(); unsigned int nBands = node->m_Means.size(); //this->m_InputImage->GetNumberOfComponentsPerPixel();
long long unsigned int memory = 0; long long unsigned int memory = 0;
memory += sizeof(NodePointerType); // size of the node pointer memory += sizeof(NodePointerType); // size of the node pointer
memory += sizeof(NodeType); // size of the node (actually, size of the base node) memory += sizeof(NodeType); // size of the node (actually, size of the base node)
memory += 1 * nBands * sizeof(float); // size of the 1 attribute, multiplied by the nb. of bands memory += 1 * nBands * sizeof(float); // size of the 1 attribute, multiplied by the nb. of bands
memory += node->m_Edges.size() * sizeof(EdgeType); // size of the edges memory += node->m_Edges.size() * sizeof(EdgeType); // size of the edges
return memory; return memory;
} }
} // end of namespace lsrm } // end of namespace lsrm
#endif #endif
......
...@@ -661,14 +661,14 @@ long long unsigned int RunFirstPartialSegmentation( ...@@ -661,14 +661,14 @@ long long unsigned int RunFirstPartialSegmentation(
// and write them to the stability margin // and write them to the stability margin
std::cout << "\tComputing stability margin..." << std::endl; std::cout << "\tComputing stability margin..." << std::endl;
{ {
std::unordered_map<typename TSegmenter::NodePointerType, unsigned int> borderNodeMap; std::unordered_map<typename TSegmenter::NodePointerType, unsigned int> borderNodeMap;
DetectBorderNodes<TSegmenter>(segmenter.m_Graph, currentTile, DetectBorderNodes<TSegmenter>(segmenter.m_Graph, currentTile,
borderNodeMap, imageWidth, imageHeight); borderNodeMap, imageWidth, imageHeight);
ExtractStabilityMargin<TSegmenter>(borderNodeMap, numberOfNeighborLayers); ExtractStabilityMargin<TSegmenter>(borderNodeMap, numberOfNeighborLayers);
WriteStabilityMargin<TSegmenter>(borderNodeMap, currentTile.nodeMarginFileName, currentTile.edgeMarginFileName); WriteStabilityMargin<TSegmenter>(borderNodeMap, currentTile.nodeMarginFileName, currentTile.edgeMarginFileName);
} }
} }
} // for each col } // for each col
......
...@@ -14,26 +14,26 @@ ...@@ -14,26 +14,26 @@
namespace lsgrm namespace lsgrm
{ {
template<class TGraph> template<class TGraph>
class GraphToOtbImage class GraphToOtbImage
{ {
public: public:
/* Some convenient typedefs */ /* Some convenient typedefs */
typedef TGraph GraphType; typedef TGraph GraphType;
typedef typename GraphType::NodeType NodeType; typedef typename GraphType::NodeType NodeType;
typedef std::vector< std::shared_ptr<NodeType> > NodeList; typedef std::vector< std::shared_ptr<NodeType> > NodeList;
typedef typename NodeList::const_iterator NodeConstIterator; typedef typename NodeList::const_iterator NodeConstIterator;
typedef unsigned int LabelPixelType; typedef unsigned int LabelPixelType;
typedef otb::Image<LabelPixelType, 2> LabelImageType; typedef otb::Image<LabelPixelType, 2> LabelImageType;
using ContourOperator = lp::ContourOperations; using ContourOperator = lp::ContourOperations;
LabelImageType::Pointer GetLabelImage(const GraphType& graph,
const unsigned int width,
const unsigned int height);
};
LabelImageType::Pointer GetLabelImage(const GraphType& graph,
const unsigned int width,
const unsigned int height);
};
} // end of namespace lsrm } // end of namespace lsrm
#include "lsgrmGraphToOtbImage.txx" #include "lsgrmGraphToOtbImage.txx"
#endif #endif
...@@ -6,70 +6,70 @@ ...@@ -6,70 +6,70 @@
namespace lsgrm namespace lsgrm
{ {
template<class TGraph> template<class TGraph>
typename GraphToOtbImage<TGraph>::LabelImageType::Pointer typename GraphToOtbImage<TGraph>::LabelImageType::Pointer
GraphToOtbImage<TGraph>::GetLabelImage(const GraphType& graph, GraphToOtbImage<TGraph>::GetLabelImage(const GraphType& graph,
const unsigned int width, const unsigned int width,
const unsigned int height) const unsigned int height)
{ {
LabelImageType::IndexType labelImageRegionStart; LabelImageType::IndexType labelImageRegionStart;
LabelImageType::SizeType labelImageRegionSize; LabelImageType::SizeType labelImageRegionSize;
LabelImageType::RegionType labelImageRegion; LabelImageType::RegionType labelImageRegion;
labelImageRegionStart[0] = 0; labelImageRegionStart[1] = 0; labelImageRegionStart[0] = 0; labelImageRegionStart[1] = 0;
labelImageRegionSize[0] = width; labelImageRegionSize[1] = height; labelImageRegionSize[0] = width; labelImageRegionSize[1] = height;
labelImageRegion.SetIndex(labelImageRegionStart); labelImageRegion.SetIndex(labelImageRegionStart);
labelImageRegion.SetSize(labelImageRegionSize); labelImageRegion.SetSize(labelImageRegionSize);
LabelImageType::Pointer label_img = LabelImageType::New(); LabelImageType::Pointer label_img = LabelImageType::New();
label_img->SetRegions(labelImageRegion); label_img->SetRegions(labelImageRegion);
label_img->Allocate(); label_img->Allocate();
using LabelImageIterator = itk::ImageRegionIterator<LabelImageType>; using LabelImageIterator = itk::ImageRegionIterator<LabelImageType>;
LabelImageIterator it(label_img, label_img->GetLargestPossibleRegion()); LabelImageIterator it(label_img, label_img->GetLargestPossibleRegion());
for(it.GoToBegin();!it.IsAtEnd(); ++it) for(it.GoToBegin();!it.IsAtEnd(); ++it)
it.Set(0); it.Set(0);
// Start at 1 (value 0 can be used for invalid pixels)
LabelImageType::InternalPixelType label = 1;
for(auto& region : graph.m_Nodes)
{
lp::CellLists borderPixels;
ContourOperator::GenerateBorderCells(borderPixels, region->m_Contour, region->m_Id, width);
for (auto& pix: borderPixels)
{
labelImageRegionStart[0] = pix % width;
labelImageRegionStart[1] = pix / width;
label_img->SetPixel(labelImageRegionStart, label);
}
++label;
}
// Sort labels in their appearance order from left->right to top->bottom // Start at 1 (value 0 can be used for invalid pixels)
const LabelImageType::InternalPixelType noDataLabel = 0; LabelImageType::InternalPixelType label = 1;
vnl_vector<LabelImageType::InternalPixelType> lut(label,noDataLabel); for(auto& region : graph.m_Nodes)
label=1; {
for(it.GoToBegin();!it.IsAtEnd(); ++it) lp::CellLists borderPixels;
{ ContourOperator::GenerateBorderCells(borderPixels, region->m_Contour, region->m_Id, width);
unsigned int inputLabel = it.Get();
if (lut[ inputLabel ] == noDataLabel) for (auto& pix: borderPixels)
{ {
lut[ inputLabel ] = label; labelImageRegionStart[0] = pix % width;
label++; labelImageRegionStart[1] = pix / width;
} label_img->SetPixel(labelImageRegionStart, label);
} }
for(it.GoToBegin();!it.IsAtEnd(); ++it) ++label;
it.Set(lut[it.Get()]); }
// Sort labels in their appearance order from left->right to top->bottom
const LabelImageType::InternalPixelType noDataLabel = 0;
vnl_vector<LabelImageType::InternalPixelType> lut(label,noDataLabel);
label=1;
for(it.GoToBegin();!it.IsAtEnd(); ++it)
{
unsigned int inputLabel = it.Get();
if (lut[ inputLabel ] == noDataLabel)
{
lut[ inputLabel ] = label;
label++;
}
}
for(it.GoToBegin();!it.IsAtEnd(); ++it)
it.Set(lut[it.Get()]);
// Fill holes
typedef itk::GrayscaleFillholeImageFilter<LabelImageType,LabelImageType> FillholeFilterType;
FillholeFilterType::Pointer fillFilter = FillholeFilterType::New();
fillFilter->SetInput(label_img);
fillFilter->Update();
return fillFilter->GetOutput();
}
// Fill holes
typedef itk::GrayscaleFillholeImageFilter<LabelImageType,LabelImageType> FillholeFilterType;
FillholeFilterType::Pointer fillFilter = FillholeFilterType::New();
fillFilter->SetInput(label_img);
fillFilter->Update();
return fillFilter->GetOutput();
}
} // end of namespace lsrm } // end of namespace lsrm
#endif #endif
...@@ -4,10 +4,10 @@ namespace lsgrm ...@@ -4,10 +4,10 @@ namespace lsgrm
{ {
enum RelativePosition{ enum RelativePosition{
POS_TOP, POS_TOP,
POS_RIGHT, POS_RIGHT,
POS_BOTTOM, POS_BOTTOM,
POS_LEFT POS_LEFT
}; };
enum NeighborhoodRelativePosition{ enum NeighborhoodRelativePosition{
...@@ -29,7 +29,7 @@ std::vector<ProcessingTile> SplitOTBImage(TInputImage * imagePtr, // input image ...@@ -29,7 +29,7 @@ std::vector<ProcessingTile> SplitOTBImage(TInputImage * imagePtr, // input image
unsigned int &nbTilesX, unsigned int &nbTilesX,
unsigned int &nbTilesY, unsigned int &nbTilesY,
std::string temporaryFilesPrefix) std::string temporaryFilesPrefix)
{