From 3c6422ef3716f704b0cf11b5bb4612f30e75ce47 Mon Sep 17 00:00:00 2001 From: Sebastien Harasse <sebastien.harasse@c-s.fr> Date: Fri, 11 May 2012 10:37:05 +0200 Subject: [PATCH] ENH: region merging: converting label image to label map. --- .../otbLabelImageRegionMergingFilter.h | 16 +++++++ .../otbLabelImageRegionMergingFilter.txx | 44 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/Code/BasicFilters/otbLabelImageRegionMergingFilter.h b/Code/BasicFilters/otbLabelImageRegionMergingFilter.h index 5a7aa321dd..57333a5ab9 100644 --- a/Code/BasicFilters/otbLabelImageRegionMergingFilter.h +++ b/Code/BasicFilters/otbLabelImageRegionMergingFilter.h @@ -21,6 +21,10 @@ #include "otbImage.h" #include "otbVectorImage.h" #include "itkImageToImageFilter.h" +#include "itkLabelMap.h" +#include "itkAttributeLabelObject.h" +#include "otbLabelImageToLabelMapWithAdjacencyFilter.h" +#include "otbLabelMapToLabelImageFilter.h" namespace otb { @@ -67,6 +71,7 @@ public: typedef typename InputImageType::SizeType SizeType; typedef TInputSpectralImage InputSpectralImageType; + typedef typename TInputSpectralImage::PixelType SpectralPixelType; typedef TOutputLabelImage OutputLabelImageType; typedef typename OutputLabelImageType::PixelType OutputLabelType; @@ -76,6 +81,15 @@ public: typedef typename OutputImageType::PixelType OutputPixelType; typedef typename OutputImageType::RegionType OutputRegionType; + itkStaticConstMacro(ImageDimension, unsigned int, InputLabelImageType::ImageDimension); + + /** LabelMap typedefs */ + typedef itk::AttributeLabelObject<InputLabelType, ImageDimension, SpectralPixelType> AttributeLabelObjectType; + typedef otb::LabelImageToLabelMapWithAdjacencyFilter<OutputLabelImageType, + otb::LabelMapWithAdjacency<AttributeLabelObjectType> > LabelMapFilterType; + typedef typename LabelMapFilterType::OutputImageType LabelMapType; + typedef otb::LabelMapToLabelImageFilter<LabelMapType, OutputLabelImageType> LabelMapToLabelImageFilterType; + /** Setters / Getters */ itkSetMacro(RangeBandwidth, RealType); @@ -128,6 +142,8 @@ private: /** Number of components per pixel in the input image */ unsigned int m_NumberOfComponentsPerPixel; + + typename LabelMapType::Pointer m_LabelMap; }; } // end namespace otb diff --git a/Code/BasicFilters/otbLabelImageRegionMergingFilter.txx b/Code/BasicFilters/otbLabelImageRegionMergingFilter.txx index 1d6a229ddd..f2f40099c2 100644 --- a/Code/BasicFilters/otbLabelImageRegionMergingFilter.txx +++ b/Code/BasicFilters/otbLabelImageRegionMergingFilter.txx @@ -107,9 +107,32 @@ LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabe ::BeforeThreadedGenerateData() { typename InputSpectralImageType::Pointer spectralImage = this->GetInputSpectralImage(); + typename InputLabelImageType::Pointer inputLabelImage = this->GetInputLabelImage(); m_NumberOfComponentsPerPixel = spectralImage->GetNumberOfComponentsPerPixel(); + // Convert to label map with adjacency + typename LabelMapFilterType::Pointer labelMapFilter = LabelMapFilterType::New(); + labelMapFilter->SetInput(inputLabelImage); + labelMapFilter->Update(); + + // Associate each label to a spectral value + m_LabelMap = labelMapFilter->GetOutput(); + typename LabelMapType::LabelObjectContainerType & localLabelObjectContainer = m_LabelMap->GetLabelObjectContainer(); + typename LabelMapType::LabelObjectContainerType::iterator labelIt = localLabelObjectContainer.begin(); + while ( labelIt != localLabelObjectContainer.end() ) + { + typename LabelMapType::LabelType l; + l = labelIt->first; + typename AttributeLabelObjectType::Pointer labelObject = labelIt->second; + labelObject->SetAttribute( + spectralImage->GetPixel( + labelObject->GetLine(0).GetIndex() + ) + ); + ++labelIt; + } + } @@ -126,6 +149,27 @@ void LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> ::AfterThreadedGenerateData() { + typename OutputLabelImageType::Pointer outputLabelImage = this->GetOutput(); + typedef itk::ImageRegionIterator<OutputLabelImageType> OutputLabelIteratorType; + OutputLabelIteratorType outputLabelIt(outputLabelImage, outputLabelImage->GetRequestedRegion()); + + // copy labelMap to output label image + typename LabelMapToLabelImageFilterType::Pointer mapToImageFilter = LabelMapToLabelImageFilterType::New(); + mapToImageFilter->SetInput(m_LabelMap); + mapToImageFilter->Update(); + + OutputLabelIteratorType mapToImageIt(mapToImageFilter->GetOutput(), outputLabelImage->GetRequestedRegion()); + + outputLabelIt.GoToBegin(); + mapToImageIt.GoToBegin(); + + while(!outputLabelIt.IsAtEnd()) + { + outputLabelIt.Set(mapToImageIt.Get()); + ++outputLabelIt; + ++mapToImageIt; + } + } -- GitLab