Commit b4326671 authored by remicres's avatar remicres

BUG: Use grm::GraphToOTBImage, and fix polygon re-ordering

parent d7071c37
......@@ -75,7 +75,7 @@ void Controller<TSegmenter>::RunSegmentation()
if (m_TilingMode == LSGRM_TILING_AUTO || m_TilingMode == LSGRM_TILING_USER)
{
unsigned int numberOfIterationsForPartialSegmentations = 3; // TODO: find a smart value
const unsigned int numberOfIterationsForPartialSegmentations = 3; // TODO: find a smart value
unsigned int numberOfIterationsRemaining = m_NumberOfIterations;
// Boolean indicating if there are remaining fusions
......
......@@ -94,8 +94,28 @@ MergeAllGraphsAndAchieveSegmentation(
// // Write output graph to the output graph directory
// WriteGraph<TSegmenter>(segmenter.m_Graph, tmpDir, 0, 0);
return segmenter.GetLabeledClusteredOutput();
// Convert graph to label image
typename TSegmenter::LabelImageType::Pointer labelImage = segmenter.GetLabeledClusteredOutput();
// Re-order nodes labels (left->right to top->bottom)
using LabelImageIterator = itk::ImageRegionIterator<typename TSegmenter::LabelImageType>;
LabelImageIterator it(labelImage, labelImage->GetLargestPossibleRegion());
const typename TSegmenter::LabelImageType::InternalPixelType noDataLabel = 0;
typename TSegmenter::LabelImageType::InternalPixelType label = 1;
vnl_vector<typename TSegmenter::LabelImageType::InternalPixelType> lut(label,noDataLabel);
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()]);
return labelImage;
}
template<class TSegmenter>
......
#ifndef __LSRM_GRAPH_TO_OTBIMAGE_H
#define __LSRM_GRAPH_TO_OTBIMAGE_H
#include <itkRGBPixel.h>
#include <itkImageRegion.h>
#include <otbImage.h>
#include <otbVectorImage.h>
#include <otbImageFileReader.h>
#include <otbImageFileWriter.h>
#include "grmGraph.h"
#include <string>
#include <stdlib.h>
#include <time.h>
#include "lpContour.h"
namespace lsgrm
{
template<class TGraph>
class GraphToOtbImage
{
public:
/* Some convenient typedefs */
typedef TGraph GraphType;
typedef typename GraphType::NodeType NodeType;
typedef std::vector< std::shared_ptr<NodeType> > NodeList;
typedef typename NodeList::const_iterator NodeConstIterator;
typedef unsigned int LabelPixelType;
typedef otb::Image<LabelPixelType, 2> LabelImageType;
using ContourOperator = lp::ContourOperations;
LabelImageType::Pointer GetLabelImage(const GraphType& graph,
const unsigned int width,
const unsigned int height);
};
} // end of namespace lsrm
#include "lsgrmGraphToOtbImage.txx"
#endif
#ifndef __LSRM_GRAPH_TO_OTBIMAGE_TXX
#define __LSRM_GRAPH_TO_OTBIMAGE_TXX
#include "lsgrmGraphToOtbImage.h"
#include "itkImageRegionIterator.h"
#include "itkGrayscaleFillholeImageFilter.h"
namespace lsgrm
{
template<class TGraph>
typename GraphToOtbImage<TGraph>::LabelImageType::Pointer
GraphToOtbImage<TGraph>::GetLabelImage(const GraphType& graph,
const unsigned int width,
const unsigned int height)
{
LabelImageType::IndexType labelImageRegionStart;
LabelImageType::SizeType labelImageRegionSize;
LabelImageType::RegionType labelImageRegion;
labelImageRegionStart[0] = 0; labelImageRegionStart[1] = 0;
labelImageRegionSize[0] = width; labelImageRegionSize[1] = height;
labelImageRegion.SetIndex(labelImageRegionStart);
labelImageRegion.SetSize(labelImageRegionSize);
LabelImageType::Pointer label_img = LabelImageType::New();
label_img->SetRegions(labelImageRegion);
label_img->Allocate();
using LabelImageIterator = itk::ImageRegionIterator<LabelImageType>;
LabelImageIterator it(label_img, label_img->GetLargestPossibleRegion());
for(it.GoToBegin();!it.IsAtEnd(); ++it)
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
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();
}
} // end of namespace lsrm
#endif
......@@ -39,7 +39,7 @@ public:
typedef typename GraphType::EdgeListType EdgeListType;
typedef typename grm::GraphOperations<TSegmenterType> GraphOperatorType;
typedef typename GraphOperatorType::NodePointerType NodePointerType;
typedef GraphToOtbImage<GraphType> IOType;
typedef grm::GraphToOtbImage<GraphType> IOType;
typedef typename IOType::LabelImageType LabelImageType;
~Segmenter(){};
......
Markdown is supported
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