Commit b0728663 authored by Cédric Traizet's avatar Cédric Traizet
Browse files

ENH : the iterative call of the persistent decorator is now done in a filter

No related merge requests found
Showing with 93 additions and 85 deletions
+93 -85
...@@ -66,14 +66,10 @@ public: ...@@ -66,14 +66,10 @@ public:
//typedef otb::StreamingStatisticsImageFilter<LabelImageType> StatisticsImageFilterType; //typedef otb::StreamingStatisticsImageFilter<LabelImageType> StatisticsImageFilterType;
typedef otb::StreamingStatisticsMapFromLabelImageFilter<ImageType, LabelImageType> StatisticsMapFromLabelImageFilterType; typedef otb::StreamingStatisticsMapFromLabelImageFilter<ImageType, LabelImageType> StatisticsMapFromLabelImageFilterType;
typedef otb::LabelImageSmallRegionMergingFilter<LabelImageType, ImageType> LabelImageSmallRegionMergingFilterType; typedef otb::LabelImageSmallRegionMergingFilter<LabelImageType> LabelImageSmallRegionMergingFilterType;
typedef itk::ImageRegionConstIterator<LabelImageType> LabelImageIterator;
typedef itk::ImageRegionConstIterator<ImageType> ImageIterator;
typedef itk::ChangeLabelImageFilter<LabelImageType,LabelImageType> ChangeLabelImageFilterType; typedef itk::ChangeLabelImageFilter<LabelImageType,LabelImageType> ChangeLabelImageFilterType;
typedef otb::TileImageFilter<LabelImageType> TileImageFilterType;
itkNewMacro(Self); itkNewMacro(Self);
itkTypeMacro(Merging, otb::Application); itkTypeMacro(Merging, otb::Application);
...@@ -163,17 +159,13 @@ private: ...@@ -163,17 +159,13 @@ private:
meanValues.push_back(meanValueMap[i]); meanValues.push_back(meanValueMap[i]);
} }
// Merge small segments
auto regionMergingFilter = LabelImageSmallRegionMergingFilterType::New(); auto regionMergingFilter = LabelImageSmallRegionMergingFilterType::New();
regionMergingFilter->SetInputLabelImage( labelIn ); regionMergingFilter->SetInput( labelIn );
regionMergingFilter->SetLabelPopulation( labelPopulation ); regionMergingFilter->SetLabelPopulation( labelPopulation );
regionMergingFilter->SetLabelStatistic( meanValues ); regionMergingFilter->SetLabelStatistic( meanValues );
for (unsigned int size = 1 ; size < minSize ; size++) regionMergingFilter->SetMinSize( minSize);
{ regionMergingFilter->Update();
regionMergingFilter->SetSize( size );
regionMergingFilter->Update();
}
//Relabelling //Relabelling
auto changeLabelFilter = ChangeLabelImageFilterType::New(); auto changeLabelFilter = ChangeLabelImageFilterType::New();
......
...@@ -29,15 +29,21 @@ namespace otb ...@@ -29,15 +29,21 @@ namespace otb
/** \class PersistentLabelImageSmallRegionMergingFilter /** \class PersistentLabelImageSmallRegionMergingFilter
* *
* This class merges regions in the input label image according to the input * This class can be used to merge each segments of a given size in a label image to
* image of spectral values and the RangeBandwidth parameter. * the connected segment with the closest radiometry.
* * This persistent filter should be used as template parameter of a PersistentFilterStreamingDecorator
* It computes from an input label image an equivalence table
* that gives for each pixel, the corresponding label in the merged image.
* The merged image can then be computed using a ChangeLabelImageFilter.
*
* This filter can be updated several times for different values of size, the output
* equivalence table will be the results of all computations.
* *
* \ingroup ImageSegmentation * \ingroup ImageSegmentation
* *
* \ingroup OTBConversion * \ingroup OTBConversion
*/ */
template <class TInputLabelImage, class TInputSpectralImage > template <class TInputLabelImage>
class ITK_EXPORT PersistentLabelImageSmallRegionMergingFilter class ITK_EXPORT PersistentLabelImageSmallRegionMergingFilter
: public PersistentImageFilter<TInputLabelImage, TInputLabelImage> : public PersistentImageFilter<TInputLabelImage, TInputLabelImage>
{ {
...@@ -157,104 +163,92 @@ private: ...@@ -157,104 +163,92 @@ private:
/** \class LabelImageSmallRegionMergingFilter /** \class LabelImageSmallRegionMergingFilter
* *
* * This filter computes from a label image an equivalence table that gives for each pixel,
* This class merges regions in the input label image according to the input * the corresponding label in the merged image.
* image of spectral values and the RangeBandwidth parameter. * It uses a PersistentFilterStreamingDecorator templated over a PersistentLabelImageSmallRegionMergingFilter
* * to merge the segments recursively from segment of size 1 to segment of a sized specified
* * by a parameter.
* The merged image can then be computed using a ChangeLabelImageFilterType.
*
* \ingroup ImageSegmentation * \ingroup ImageSegmentation
* *
* \ingroup OTBConversion * \ingroup OTBConversion
*/ */
template <class TInputLabelImage, class TInputSpectralImage> template <class TInputLabelImage>
class ITK_EXPORT LabelImageSmallRegionMergingFilter : class ITK_EXPORT LabelImageSmallRegionMergingFilter
public PersistentFilterStreamingDecorator<PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> > : public itk::ImageToImageFilter<TInputLabelImage, TInputLabelImage>
{ {
public: public:
/** Standard Self typedef */ /** Standard Self typedef */
typedef LabelImageSmallRegionMergingFilter Self; typedef LabelImageSmallRegionMergingFilter Self;
typedef PersistentFilterStreamingDecorator typedef itk::ImageToImageFilter<TInputLabelImage, TInputLabelImage> Superclass;
<PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> > Superclass; typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<const Self> ConstPointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Type macro */ /** Type macro */
itkNewMacro(Self); itkNewMacro(Self);
/** Creation through object factory macro */ /** Creation through object factory macro */
itkTypeMacro(LabelImageSmallRegionMergingFilter, PersistentFilterStreamingDecorator); itkTypeMacro(LabelImageSmallRegionMergingFilter, itk::ImageToImageFilter);
typedef PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> PersistentFilterType; // Small region merging filter typedefs
typedef typename PersistentFilterType::InputLabelImageType InputLabelImageType; typedef PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage > PersistentLabelImageSmallRegionMergingFilterType;
typedef typename PersistentFilterType::LabelPopulationType LabelPopulationType; typedef PersistentFilterStreamingDecorator < PersistentLabelImageSmallRegionMergingFilterType > LabelImageSmallRegionMergingFilterType;
typedef typename PersistentFilterType::LabelStatisticType LabelStatisticType;
typedef typename PersistentFilterType::LUTType LUTType;
/** Sets the input image where the value of a pixel is the region id */ typedef typename PersistentLabelImageSmallRegionMergingFilterType::LabelPopulationType LabelPopulationType;
void SetInputLabelImage( const InputLabelImageType * labelImage) typedef typename PersistentLabelImageSmallRegionMergingFilterType::LabelStatisticType LabelStatisticType;
{ typedef typename PersistentLabelImageSmallRegionMergingFilterType::LUTType LUTType;
this->GetFilter()->SetInput( labelImage );
}
/** Returns input label image */
InputLabelImageType * GetInputLabelImage()
{
return this->GetFilter()->GetInput();
}
/** Set size of segments to be merged */
void SetSize(unsigned int size)
{
this->GetFilter()->SetSize( size );
}
/** Get size of segments to be merged */
unsigned int GetSize()
{
return this->GetFilter()->GetSize();
}
/** Set/Get size of polygon to be merged */
itkGetMacro(MinSize , unsigned int);
itkSetMacro(MinSize , unsigned int);
/** Set the Label population map */ /** Set the Label population map */
void SetLabelPopulation( LabelPopulationType const & labelPopulation ) void SetLabelPopulation( LabelPopulationType const & labelPopulation )
{ {
this->GetFilter()->SetLabelPopulation( labelPopulation ); m_SmallRegionMergingFilter->GetFilter()->SetLabelPopulation( labelPopulation );
} }
/** Get the Label population map */ /** Get the Label population map */
LabelPopulationType const & GetLabelPopulation( ) const LabelPopulationType const & GetLabelPopulation( ) const
{ {
return this->GetFilter()->GetLabelPopulation(); return m_SmallRegionMergingFilter->GetFilter()->GetLabelPopulation();
} }
/** Set the Label statistic map */ /** Set the Label statistic map */
void SetLabelStatistic( LabelStatisticType const & labelStatistic ) void SetLabelStatistic( LabelStatisticType const & labelStatistic )
{ {
this->GetFilter()->SetLabelStatistic( labelStatistic ); m_SmallRegionMergingFilter->GetFilter()->SetLabelStatistic( labelStatistic );
} }
/** Get the Label statistic map */ /** Get the Label statistic map */
LabelStatisticType const & GetLabelStatistic( ) const LabelStatisticType const & GetLabelStatistic( ) const
{ {
return this->GetFilter()->GetLabelStatistic(); return m_SmallRegionMergingFilter->GetFilter()->GetLabelStatistic();
} }
LUTType const & GetLUT() const /** Get the Label statistic map */
LUTType const & GetLUT( ) const
{ {
return this->GetFilter()->GetLUT(); return m_SmallRegionMergingFilter->GetFilter()->GetLUT();
} }
protected: protected:
/** Constructor */ /** Constructor */
LabelImageSmallRegionMergingFilter() {} LabelImageSmallRegionMergingFilter();
/** Destructor */ /** Destructor */
~LabelImageSmallRegionMergingFilter() override {} ~LabelImageSmallRegionMergingFilter() override {}
/** Generate Data method (Update LabelImageSmallRegionMergingFilterType recursively) */
void GenerateData();
private: private:
LabelImageSmallRegionMergingFilter(const Self &) = delete; LabelImageSmallRegionMergingFilter(const Self &) = delete;
void operator =(const Self&) = delete; void operator =(const Self&) = delete;
typename LabelImageSmallRegionMergingFilterType::Pointer m_SmallRegionMergingFilter;
unsigned int m_MinSize;
}; };
} // end namespace otb } // end namespace otb
......
...@@ -30,22 +30,22 @@ ...@@ -30,22 +30,22 @@
#include <time.h> #include <time.h>
namespace otb namespace otb
{ {
template <class TInputLabelImage, class TInputSpectralImage> template <class TInputLabelImage >
PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
::PersistentLabelImageSmallRegionMergingFilter() : m_Size(1) ::PersistentLabelImageSmallRegionMergingFilter() : m_Size(1)
{ {
} }
template <class TInputLabelImage, class TInputSpectralImage> template <class TInputLabelImage >
PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
::~PersistentLabelImageSmallRegionMergingFilter() ::~PersistentLabelImageSmallRegionMergingFilter()
{ {
} }
template <class TInputLabelImage, class TInputSpectralImage> template <class TInputLabelImage >
void void
PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
::Reset() ::Reset()
{ {
m_NeighboursMapsTmp.clear(); m_NeighboursMapsTmp.clear();
...@@ -53,9 +53,9 @@ PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralIma ...@@ -53,9 +53,9 @@ PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralIma
} }
template <class TInputLabelImage, class TInputSpectralImage> template <class TInputLabelImage >
void void
PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
::Synthetize() ::Synthetize()
{ {
NeigboursMapType neighboursMap; NeigboursMapType neighboursMap;
...@@ -142,10 +142,10 @@ PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralIma ...@@ -142,10 +142,10 @@ PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralIma
} }
} }
template <class TInputLabelImage, class TInputSpectralImage> template <class TInputLabelImage >
typename PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage>::InputLabelType typename PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >::InputLabelType
PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
::FindCorrespondingLabel( typename PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> ::FindCorrespondingLabel( typename PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
::InputLabelType label) ::InputLabelType label)
{ {
auto correspondingLabel = m_LUT[label]; auto correspondingLabel = m_LUT[label];
...@@ -159,17 +159,17 @@ PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralIma ...@@ -159,17 +159,17 @@ PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralIma
template <class TInputLabelImage, class TInputSpectralImage> template <class TInputLabelImage >
void void
PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
::GenerateOutputInformation() ::GenerateOutputInformation()
{ {
Superclass::GenerateOutputInformation(); Superclass::GenerateOutputInformation();
} }
template <class TInputLabelImage, class TInputSpectralImage> template <class TInputLabelImage >
void void
PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
::ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType threadId ) ::ThreadedGenerateData(const RegionType& outputRegionForThread, itk::ThreadIdType threadId )
{ {
using IteratorType = itk::ImageRegionConstIterator< TInputLabelImage >; using IteratorType = itk::ImageRegionConstIterator< TInputLabelImage >;
...@@ -210,14 +210,36 @@ PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralIma ...@@ -210,14 +210,36 @@ PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralIma
} }
} }
template <class TInputLabelImage, class TInputSpectralImage> template <class TInputLabelImage >
void void
PersistentLabelImageSmallRegionMergingFilter<TInputLabelImage, TInputSpectralImage> PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
::PrintSelf(std::ostream& os, itk::Indent indent) const ::PrintSelf(std::ostream& os, itk::Indent indent) const
{ {
Superclass::PrintSelf(os, indent); Superclass::PrintSelf(os, indent);
} }
template <class TInputLabelImage >
LabelImageSmallRegionMergingFilter< TInputLabelImage >
::LabelImageSmallRegionMergingFilter() : m_MinSize(1)
{
m_SmallRegionMergingFilter = LabelImageSmallRegionMergingFilterType::New();
}
template <class TInputLabelImage >
void
LabelImageSmallRegionMergingFilter< TInputLabelImage >
::GenerateData()
{
auto labelImage = this->GetInput();
m_SmallRegionMergingFilter->GetFilter()->SetInput( labelImage );
for (unsigned int size = 1; size < m_MinSize; size++)
{
m_SmallRegionMergingFilter->GetFilter()->SetSize( size) ;
m_SmallRegionMergingFilter->Update();
}
}
} // end namespace otb } // end namespace otb
......
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