diff --git a/Code/BasicFilters/otbLabelImageRegionMergingFilter.h b/Code/BasicFilters/otbLabelImageRegionMergingFilter.h new file mode 100644 index 0000000000000000000000000000000000000000..5a7aa321dd0c345cb43173f32accb991aaade4dc --- /dev/null +++ b/Code/BasicFilters/otbLabelImageRegionMergingFilter.h @@ -0,0 +1,139 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __otbLabelImageRegionMergingFilter_h +#define __otbLabelImageRegionMergingFilter_h + +#include "otbImage.h" +#include "otbVectorImage.h" +#include "itkImageToImageFilter.h" + +namespace otb +{ + +/** \class LabelImageRegionMergingFilter + * + * + * This class merges regions in the input label image according to the input + * image of spectral values and the RangeBandwidth parameter. + * + * + * \ingroup ImageSegmentation + * \ingroup ImageEnhancement + */ +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage = TInputLabelImage> +class ITK_EXPORT LabelImageRegionMergingFilter + : public itk::ImageToImageFilter<TInputLabelImage, TOutputLabelImage> +{ +public: + /** Standard class typedef */ + typedef LabelImageRegionMergingFilter Self; + typedef itk::ImageToImageFilter<TInputLabelImage, TOutputLabelImage> Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; + typedef double RealType; + + /** Type macro */ + itkTypeMacro(LabelImageRegionMergingFilter, ImageToImageFilter); + itkNewMacro(Self); + + /** Template parameters typedefs */ + + typedef TInputLabelImage InputLabelImageType; + typedef typename InputLabelImageType::PixelType InputLabelType; + + typedef TInputLabelImage InputImageType; + typedef typename InputImageType::Pointer InputImagePointerType; + typedef typename InputImageType::PixelType InputPixelType; + typedef typename InputImageType::IndexType InputIndexType; + typedef typename InputImageType::SizeType InputSizeType; + typedef typename InputImageType::IndexValueType InputIndexValueType; + typedef typename InputImageType::PointType PointType; + typedef typename InputImageType::RegionType RegionType; + typedef typename InputImageType::SizeType SizeType; + + typedef TInputSpectralImage InputSpectralImageType; + + typedef TOutputLabelImage OutputLabelImageType; + typedef typename OutputLabelImageType::PixelType OutputLabelType; + + typedef TOutputLabelImage OutputImageType; + typedef typename OutputImageType::Pointer OutputImagePointerType; + typedef typename OutputImageType::PixelType OutputPixelType; + typedef typename OutputImageType::RegionType OutputRegionType; + + + /** Setters / Getters */ + itkSetMacro(RangeBandwidth, RealType); + itkGetMacro(RangeBandwidth, RealType); + + /** Returns the const image of region labels */ + const OutputLabelImageType * GetLabelOutput() const; + /** Returns the image of region labels */ + OutputLabelImageType * GetLabelOutput(); + + void SetInputLabelImage( const InputLabelImageType * labelImage); + void SetInputSpectralImage( const InputSpectralImageType * spectralImage); + InputLabelImageType * GetInputLabelImage(); + InputSpectralImageType * GetInputSpectralImage(); + +protected: + + virtual void BeforeThreadedGenerateData(); + + /** LabelImageRegionMergingFilter can be implemented as a multithreaded filter. + * Therefore, this implementation provides a ThreadedGenerateData() + * routine which is called for each processing thread. The output + * image data is allocated automatically by the superclass prior to + * calling ThreadedGenerateData(). ThreadedGenerateData can only + * write to the portion of the output image specified by the + * parameter "outputRegionForThread" + * + * \sa ImageToImageFilter::ThreadedGenerateData(), + * ImageToImageFilter::GenerateData() */ + void ThreadedGenerateData(const OutputRegionType& outputRegionForThread, + int threadId ); + + virtual void AfterThreadedGenerateData(); + + /** Constructor */ + LabelImageRegionMergingFilter(); + + /** Destructor */ + virtual ~LabelImageRegionMergingFilter(); + + /** PrintSelf method */ + virtual void PrintSelf(std::ostream& os, itk::Indent indent) const; + +private: + LabelImageRegionMergingFilter(const Self &); //purposely not implemented + void operator =(const Self&); //purposely not implemented + + /** Range bandwidth */ + RealType m_RangeBandwidth; + + /** Number of components per pixel in the input image */ + unsigned int m_NumberOfComponentsPerPixel; +}; + +} // end namespace otb + +#ifndef OTB_MANUAL_INSTANTIATION +#include "otbLabelImageRegionMergingFilter.txx" +#endif + +#endif diff --git a/Code/BasicFilters/otbLabelImageRegionMergingFilter.txx b/Code/BasicFilters/otbLabelImageRegionMergingFilter.txx new file mode 100644 index 0000000000000000000000000000000000000000..1d6a229ddda63c17496a3595d3912879e253d83f --- /dev/null +++ b/Code/BasicFilters/otbLabelImageRegionMergingFilter.txx @@ -0,0 +1,143 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ + +#ifndef __otbLabelImageRegionMergingFilter_txx +#define __otbLabelImageRegionMergingFilter_txx + +#include "otbLabelImageRegionMergingFilter.h" +#include "itkProgressReporter.h" + + +namespace otb +{ +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::LabelImageRegionMergingFilter() +{ + + this->SetNumberOfRequiredInputs( 2 ); + +} + +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +void +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::SetInputLabelImage( const TInputLabelImage * labelImage) +{ + // Process object is not const-correct so the const casting is required. + this->SetNthInput(0, const_cast<TInputLabelImage *>( labelImage )); +} + +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +void +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::SetInputSpectralImage( const TInputSpectralImage * spectralImage) +{ + // Process object is not const-correct so the const casting is required. + this->SetNthInput(1, const_cast<TInputSpectralImage *>( spectralImage )); +} + +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +TInputLabelImage * +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::GetInputLabelImage() +{ + return dynamic_cast<TInputLabelImage*>(itk::ProcessObject::GetInput(0)); +} + +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +TInputSpectralImage * +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::GetInputSpectralImage() +{ + return dynamic_cast<TInputSpectralImage*>(itk::ProcessObject::GetInput(1)); +} + + +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::~LabelImageRegionMergingFilter() +{ + +} + +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +TOutputLabelImage * +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::GetLabelOutput() +{ + if (this->GetNumberOfOutputs() < 1) + { + return 0; + } + return static_cast<OutputLabelImageType *>(this->itk::ProcessObject::GetOutput(0)); +} + +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +const TOutputLabelImage * +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::GetLabelOutput() const +{ + if (this->GetNumberOfOutputs() < 1) + { + return 0; + } + return static_cast<OutputLabelImageType *>(this->itk::ProcessObject::GetOutput(0)); +} + + +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +void +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::BeforeThreadedGenerateData() +{ + typename InputSpectralImageType::Pointer spectralImage = this->GetInputSpectralImage(); + + m_NumberOfComponentsPerPixel = spectralImage->GetNumberOfComponentsPerPixel(); + +} + + +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +void +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::ThreadedGenerateData(const OutputRegionType& outputRegionForThread, int threadId) +{ +} + + +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +void +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::AfterThreadedGenerateData() +{ +} + + +template <class TInputLabelImage, class TInputSpectralImage, class TOutputLabelImage> +void +LabelImageRegionMergingFilter<TInputLabelImage, TInputSpectralImage, TOutputLabelImage> +::PrintSelf(std::ostream& os, itk::Indent indent) const +{ + Superclass::PrintSelf(os, indent); + os << indent << "Range bandwidth: " << m_RangeBandwidth << std::endl; + } + +} // end namespace otb + +#endif