From 65f3020d8748165ae2655a5b658dc36b4a9d854b Mon Sep 17 00:00:00 2001 From: Pierre Lassalle <lassallep@cesbio.cnes.fr> Date: Fri, 26 Feb 2016 20:55:11 +0100 Subject: [PATCH] Fix bug generation of labels --- Code/lsrmGraphToOtbImage.h | 4 +- Code/lsrmGraphToOtbImage.h~ | 45 ++++++++++++ Code/lsrmGraphToOtbImage.txx | 21 +++--- Code/lsrmGraphToOtbImage.txx~ | 125 ++++++++++++++++++++++++++++++++++ 4 files changed, 181 insertions(+), 14 deletions(-) create mode 100644 Code/lsrmGraphToOtbImage.h~ create mode 100644 Code/lsrmGraphToOtbImage.txx~ diff --git a/Code/lsrmGraphToOtbImage.h b/Code/lsrmGraphToOtbImage.h index 89ef969..0cd93ce 100644 --- a/Code/lsrmGraphToOtbImage.h +++ b/Code/lsrmGraphToOtbImage.h @@ -35,9 +35,9 @@ namespace lsrm const unsigned int width, const unsigned int height); - ClusteredImageType::Pointer GetClusteredOutput(const GraphType& graph, + /*ClusteredImageType::Pointer GetClusteredOutput(const GraphType& graph, const unsigned int width, - const unsigned int height); + const unsigned int height);*/ }; } // end of namespace lsrm diff --git a/Code/lsrmGraphToOtbImage.h~ b/Code/lsrmGraphToOtbImage.h~ new file mode 100644 index 0000000..89ef969 --- /dev/null +++ b/Code/lsrmGraphToOtbImage.h~ @@ -0,0 +1,45 @@ +#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 "lsrmGraph.h" +#include <string> +#include <stdlib.h> +#include <time.h> +#include "lpContour.h" + +namespace lsrm +{ + 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 long int LabelPixelType; + typedef otb::Image<LabelPixelType, 2> LabelImageType; + typedef unsigned char ClusterPixelType; + typedef otb::VectorImage<ClusterPixelType, 2> ClusteredImageType; + using ContourOperator = lp::ContourOperations; + + + LabelImageType::Pointer GetLabelImage(const GraphType& graph, + const unsigned int width, + const unsigned int height); + + ClusteredImageType::Pointer GetClusteredOutput(const GraphType& graph, + const unsigned int width, + const unsigned int height); + }; + +} // end of namespace lsrm +#include "lsrmGraphToOtbImage.txx" +#endif diff --git a/Code/lsrmGraphToOtbImage.txx b/Code/lsrmGraphToOtbImage.txx index 6605208..48d7084 100644 --- a/Code/lsrmGraphToOtbImage.txx +++ b/Code/lsrmGraphToOtbImage.txx @@ -2,6 +2,7 @@ #define __LSRM_GRAPH_TO_OTBIMAGE_TXX #include "lsrmGraphToOtbImage.h" #include "itkImageRegionIterator.h" +#include "itkGrayscaleFillholeImageFilter.h" namespace lsrm { @@ -45,20 +46,16 @@ namespace lsrm ++label; } - long unsigned int pixelValue = 0; - for(it.GoToBegin(); !it.IsAtEnd(); ++it) - { - auto pixel = it.Get(); - if(pixel == 0) - it.Set(pixelValue); - else - pixelValue = pixel; - } + // Fill holes + typedef itk::GrayscaleFillholeImageFilter<LabelImageType,LabelImageType> FillholeFilterType; + FillholeFilterType::Pointer fillFilter = FillholeFilterType::New(); + fillFilter->SetInput(label_img); + fillFilter->Update(); - return label_img; + return fillFilter->GetOutput(); } - template<class TGraph> + /*template<class TGraph> typename GraphToOtbImage<TGraph>::ClusteredImageType::Pointer GraphToOtbImage<TGraph>::GetClusteredOutput(const GraphType& graph, const unsigned int width, @@ -122,7 +119,7 @@ namespace lsrm } return clusterImg; - } + }*/ } // end of namespace lsrm #endif diff --git a/Code/lsrmGraphToOtbImage.txx~ b/Code/lsrmGraphToOtbImage.txx~ new file mode 100644 index 0000000..5fb16c5 --- /dev/null +++ b/Code/lsrmGraphToOtbImage.txx~ @@ -0,0 +1,125 @@ +#ifndef __LSRM_GRAPH_TO_OTBIMAGE_TXX +#define __LSRM_GRAPH_TO_OTBIMAGE_TXX +#include "lsrmGraphToOtbImage.h" +#include "itkImageRegionIterator.h" +#include "itkGrayscaleFillholeImageFilter.h" + +namespace lsrm +{ + template<class TGraph> + typename GraphToOtbImage<TGraph>::LabelImageType::Pointer + GraphToOtbImage<TGraph>::GetLabelImage(const GraphType& graph, + const unsigned int width, + const unsigned int height) + { + LabelImageType::IndexType index; + LabelImageType::SizeType size; + LabelImageType::RegionType region; + + index[0] = 0; index[1] = 0; + size[0] = width; size[1] = height; + region.SetIndex(index); + region.SetSize(size); + + LabelImageType::Pointer label_img = LabelImageType::New(); + label_img->SetRegions(region); + 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) + long unsigned int 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) + { + index[0] = pix % width; + index[1] = pix / width; + label_img->SetPixel(index, label); + } + ++label; + } + + // Fill holes + typedef itk::GrayscaleFillholeImageFilter<LabelImageType,LabelImageType> FillholeFilterType; + FillholeFilterType::Pointer fillFilter = FillholeFilterType::New(); + fillFilter->SetInput(label_img); + fillFilter->Update(); + + return fillFilter->GetOutput(); + } + + template<class TGraph> + typename GraphToOtbImage<TGraph>::ClusteredImageType::Pointer + GraphToOtbImage<TGraph>::GetClusteredOutput(const GraphType& graph, + const unsigned int width, + const unsigned int height) + { + ClusteredImageType::IndexType index; + ClusteredImageType::SizeType size; + ClusteredImageType::RegionType region; + + index[0] = 0; index[1] = 0; + size[0] = width; size[1] = height; + region.SetIndex(index); + region.SetSize(size); + + ClusteredImageType::Pointer clusterImg = ClusteredImageType::New(); + clusterImg->SetRegions(region); + clusterImg->SetNumberOfComponentsPerPixel(3); + clusterImg->Allocate(); + + ClusteredImageType::PixelType pixelValue; + pixelValue.Reserve(3); + pixelValue[0] = 0; + pixelValue[1] = 0; + pixelValue[2] = 0; + + using ClusterImageIterator = itk::ImageRegionIterator<ClusteredImageType>; + ClusterImageIterator it(clusterImg, clusterImg->GetLargestPossibleRegion()); + for(it.GoToBegin();!it.IsAtEnd(); ++it) + it.Set(pixelValue); + + srand(time(NULL)); + unsigned char c1, c2, c3; + for(auto& region : graph.m_Nodes) + { + c1 = rand() % 256; + c2 = rand() % 256; + c3 = rand() % 256; + + lp::CellLists borderPixels; + ContourOperator::GenerateBorderCells(borderPixels, region->m_Contour, region->m_Id, width); + + for (auto& pix : borderPixels) + { + index[0] = pix % width; + index[1] = pix / width; + pixelValue[0] = c1; + pixelValue[1] = c2; + pixelValue[2] = c3; + clusterImg->SetPixel(index, pixelValue); + } + } + + + for(it.GoToBegin(); !it.IsAtEnd(); ++it) + { + auto pixel = it.Get(); + if(pixel[0] == 0 && pixel[1] == 0 && pixel[2] == 0) + it.Set(pixelValue); + else + pixelValue = pixel; + } + + return clusterImg; + } + +} // end of namespace lsrm +#endif -- GitLab