Commit f2d257b2 authored by Jordi Inglada's avatar Jordi Inglada
Browse files

ADD: Added classes for label object representation and manipulation

parent abf1d0d3
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkAttributeKeepNObjectsLabelMapFilter.h,v $
Language: C++
Date: $Date: 2006/03/28 19:59:05 $
Version: $Revision: 1.6 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 __itkAttributeKeepNObjectsLabelMapFilter_h
#define __itkAttributeKeepNObjectsLabelMapFilter_h
#include "itkInPlaceLabelMapFilter.h"
#include "itkAttributeLabelObject.h"
namespace itk {
/** \class AttributeKeepNObjectsLabelMapFilter
* \brief keep N objects according to their attribute
*
* AttributeKeepNObjectsLabelMapFilter keep the N objects in a label collection image
* with the highest (or lowest) attribute value. The attribute is provide by an
* attribute accessor given in template parameter.
*
* \author Gatan Lehmann. Biologie du Dveloppement et de la Reproduction, INRA de Jouy-en-Josas, France.
*
* \ingroup ImageEnhancement MathematicalMorphologyImageFilters
*/
template<class TImage, class TAttributeAccessor=
typename Functor::AttributeLabelObjectAccessor< typename TImage::LabelObjectType > >
class ITK_EXPORT AttributeKeepNObjectsLabelMapFilter :
public InPlaceLabelMapFilter<TImage>
{
public:
/** Standard class typedefs. */
typedef AttributeKeepNObjectsLabelMapFilter Self;
typedef InPlaceLabelMapFilter<TImage>
Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
/** Some convenient typedefs. */
typedef TImage ImageType;
typedef typename ImageType::Pointer ImagePointer;
typedef typename ImageType::ConstPointer ImageConstPointer;
typedef typename ImageType::PixelType PixelType;
typedef typename ImageType::IndexType IndexType;
typedef typename ImageType::LabelObjectType LabelObjectType;
typedef TAttributeAccessor AttributeAccessorType;
typedef typename AttributeAccessorType::AttributeValueType AttributeValueType;
/** ImageDimension constants */
itkStaticConstMacro(ImageDimension, unsigned int,
TImage::ImageDimension);
/** Standard New method. */
itkNewMacro(Self);
/** Runtime information support. */
itkTypeMacro(AttributeKeepNObjectsLabelMapFilter,
InPlaceLabelMapFilter);
#ifdef ITK_USE_CONCEPT_CHECKING
/** Begin concept checking */
/* itkConceptMacro(InputEqualityComparableCheck,
(Concept::EqualityComparable<InputImagePixelType>));
itkConceptMacro(IntConvertibleToInputCheck,
(Concept::Convertible<int, InputImagePixelType>));
itkConceptMacro(InputOStreamWritableCheck,
(Concept::OStreamWritable<InputImagePixelType>));*/
/** End concept checking */
#endif
/**
* Set/Get the ordering of the objects. By default, the ones with the
* highest value are kept. Turming ReverseOrdering to true make this filter
* keep the objects with the smallest values
*/
itkSetMacro( ReverseOrdering, bool );
itkGetConstReferenceMacro( ReverseOrdering, bool );
itkBooleanMacro( ReverseOrdering );
/**
* Set/Get the number of objects to keep
*/
itkSetMacro( NumberOfObjects, unsigned long );
itkGetConstReferenceMacro( NumberOfObjects, unsigned long );
protected:
AttributeKeepNObjectsLabelMapFilter();
~AttributeKeepNObjectsLabelMapFilter() {};
void GenerateData();
void PrintSelf(std::ostream& os, Indent indent) const;
class ReverseComparator
{
public:
bool operator()( const typename LabelObjectType::Pointer & a, const typename LabelObjectType::Pointer & b )
{
return accessor( a ) < accessor( b );
}
AttributeAccessorType accessor;
};
class Comparator
{
public:
bool operator()( const typename LabelObjectType::Pointer & a, const typename LabelObjectType::Pointer & b )
{
return accessor( a ) > accessor( b );
}
AttributeAccessorType accessor;
};
private:
AttributeKeepNObjectsLabelMapFilter(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
bool m_ReverseOrdering;
unsigned long m_NumberOfObjects;
} ; // end of class
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
#include "itkAttributeKeepNObjectsLabelMapFilter.txx"
#endif
#endif
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkAttributeKeepNObjectsLabelMapFilter.txx,v $
Language: C++
Date: $Date: 2005/08/23 15:09:03 $
Version: $Revision: 1.6 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 __itkAttributeKeepNObjectsLabelMapFilter_txx
#define __itkAttributeKeepNObjectsLabelMapFilter_txx
#include "itkAttributeKeepNObjectsLabelMapFilter.h"
#include "itkProgressReporter.h"
namespace itk {
template <class TImage, class TAttributeAccessor>
AttributeKeepNObjectsLabelMapFilter<TImage, TAttributeAccessor>
::AttributeKeepNObjectsLabelMapFilter()
{
m_ReverseOrdering = false;
m_NumberOfObjects = 1;
}
template <class TImage, class TAttributeAccessor>
void
AttributeKeepNObjectsLabelMapFilter<TImage, TAttributeAccessor>
::GenerateData()
{
// Allocate the output
this->AllocateOutputs();
ImageType * output = this->GetOutput();
typedef typename ImageType::LabelObjectContainerType LabelObjectContainerType;
const LabelObjectContainerType & labelObjectContainer = output->GetLabelObjectContainer();
typedef typename std::vector< typename LabelObjectType::Pointer > VectorType;
ProgressReporter progress( this, 0, 2 * labelObjectContainer.size() );
// get the label objects in a vector, so they can be sorted
VectorType labelObjects;
labelObjects.reserve( labelObjectContainer.size() );
for( typename LabelObjectContainerType::const_iterator it = labelObjectContainer.begin();
it != labelObjectContainer.end();
it++ )
{
labelObjects.push_back( it->second );
progress.CompletedPixel();
}
// instantiate the comparator and sort the vector
if( m_NumberOfObjects < labelObjectContainer.size() )
{
typename VectorType::iterator end = labelObjects.begin() + m_NumberOfObjects;
if( m_ReverseOrdering )
{
ReverseComparator comparator;
std::nth_element( labelObjects.begin(), end, labelObjects.end(), comparator );
}
else
{
Comparator comparator;
std::nth_element( labelObjects.begin(), end, labelObjects.end(), comparator );
}
// progress.CompletedPixel();
// and remove the last objects of the map
for( typename VectorType::const_iterator it = end;
it != labelObjects.end();
it++ )
{
output->RemoveLabelObject( *it );
progress.CompletedPixel();
}
}
}
template <class TImage, class TAttributeAccessor>
void
AttributeKeepNObjectsLabelMapFilter<TImage, TAttributeAccessor>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "ReverseOrdering: " << m_ReverseOrdering << std::endl;
os << indent << "NumberOfObjects: " << m_NumberOfObjects << std::endl;
}
}// end namespace itk
#endif
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkLabelObject.h,v $
Language: C++
Date: $Date: 2005/01/21 20:13:31 $
Version: $Revision: 1.6 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 __itkAttributeLabelObject_h
#define __itkAttributeLabelObject_h
#include "itkLabelObject.h"
namespace itk
{
namespace Functor {
template< class TLabelObject >
class ITK_EXPORT AttributeLabelObjectAccessor
{
public:
typedef TLabelObject LabelObjectType;
typedef typename LabelObjectType::AttributeValueType AttributeValueType;
inline const AttributeValueType operator()( const LabelObjectType * labelObject )
{
return labelObject->GetAttribute();
}
inline void operator()( LabelObjectType * labelObject, AttributeValueType value )
{
labelObject->SetAttribute( value );
}
};
}
/** \class AttributeLabelObject
* \brief A LabelObject with a generic attribute
*
* \author Gatan Lehmann. Biologie du Dveloppement et de la Reproduction, INRA de Jouy-en-Josas, France.
*
*\sa LabelObject, ShapeLabelObject, StatisticsLabelObject
*
* \ingroup DataRepresentation
*/
template < class TLabel, unsigned int VImageDimension, class TAttributeValue >
class ITK_EXPORT AttributeLabelObject : public LabelObject< TLabel, VImageDimension >
{
public:
/** Standard class typedefs */
typedef AttributeLabelObject Self;
typedef LabelObject< TLabel, VImageDimension > Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
typedef WeakPointer<const Self> ConstWeakPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(AttributeLabelObject, LabelObject);
typedef LabelMap< Self > LabelMapType;
itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
typedef typename Superclass::IndexType IndexType;
typedef TLabel LabelType;
typedef typename Superclass::LineType LineType;
typedef typename Superclass::LengthType LengthType;
typedef typename Superclass::LineContainerType LineContainerType;
typedef TAttributeValue AttributeValueType;
void SetAttribute( const AttributeValueType & v )
{
m_Attribute = v;
}
const AttributeValueType & GetAttribute() const
{
return m_Attribute;
}
AttributeValueType GetAttribute()
{
return m_Attribute;
}
virtual void CopyDataFrom( const Self * src )
{
Superclass::CopyDataFrom( src );
m_Attribute = src->m_Attribute;
}
protected:
AttributeLabelObject()
{
// how to initialize the attribute ?
}
void PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf( os, indent );
os << indent << "Attribute: " << m_Attribute << std::endl;
}
private:
AttributeLabelObject(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
AttributeValueType m_Attribute;
};
} // end namespace itk
#endif
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkAttributeOpeningLabelMapFilter.h,v $
Language: C++
Date: $Date: 2006/03/28 19:59:05 $
Version: $Revision: 1.6 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 __itkAttributeOpeningLabelMapFilter_h
#define __itkAttributeOpeningLabelMapFilter_h
#include "itkInPlaceLabelMapFilter.h"
#include "itkAttributeLabelObject.h"
namespace itk {
/** \class AttributeOpeningLabelMapFilter
* \brief remove the objects according to the value of their attribute
*
* AttributeOpeningLabelMapFilter removes the objects in a label collection image
* with an attribute value smaller or greater than a threshold called Lambda.
* The attribute is provide by an attribute accessor given in template parameter.
*
* \author Gatan Lehmann. Biologie du Dveloppement et de la Reproduction, INRA de Jouy-en-Josas, France.
*
* \sa AttributeLabelObject
* \ingroup ImageEnhancement MathematicalMorphologyImageFilters
*/
template<class TImage, class TAttributeAccessor=
typename Functor::AttributeLabelObjectAccessor< typename TImage::LabelObjectType > >
class ITK_EXPORT AttributeOpeningLabelMapFilter :
public InPlaceLabelMapFilter<TImage>
{
public:
/** Standard class typedefs. */
typedef AttributeOpeningLabelMapFilter Self;
typedef InPlaceLabelMapFilter<TImage>
Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
/** Some convenient typedefs. */
typedef TImage ImageType;
typedef typename ImageType::Pointer ImagePointer;
typedef typename ImageType::ConstPointer ImageConstPointer;
typedef typename ImageType::PixelType PixelType;
typedef typename ImageType::IndexType IndexType;
typedef TAttributeAccessor AttributeAccessorType;
typedef typename AttributeAccessorType::AttributeValueType AttributeValueType;
/** ImageDimension constants */
itkStaticConstMacro(ImageDimension, unsigned int,
TImage::ImageDimension);
/** Standard New method. */
itkNewMacro(Self);
/** Runtime information support. */
itkTypeMacro(AttributeOpeningLabelMapFilter,
InPlaceLabelMapFilter);
#ifdef ITK_USE_CONCEPT_CHECKING
/** Begin concept checking */
/* itkConceptMacro(InputEqualityComparableCheck,
(Concept::EqualityComparable<InputImagePixelType>));
itkConceptMacro(IntConvertibleToInputCheck,
(Concept::Convertible<int, InputImagePixelType>));
itkConceptMacro(InputOStreamWritableCheck,
(Concept::OStreamWritable<InputImagePixelType>));*/
/** End concept checking */
#endif
/**
* Set/Get the threshold used to keep or remove the objects.
*/
itkGetConstMacro(Lambda, AttributeValueType);
itkSetMacro(Lambda, AttributeValueType);
/**
* Set/Get the ordering of the objects. By default, the objects with
* an attribute value smaller than Lamba are removed. Turning ReverseOrdering
* to true make this filter remove the object with an attribute value greater
* than Lambda instead.
*/
itkGetConstMacro( ReverseOrdering, bool );
itkSetMacro( ReverseOrdering, bool );
itkBooleanMacro( ReverseOrdering );
protected:
AttributeOpeningLabelMapFilter();
~AttributeOpeningLabelMapFilter() {};
void GenerateData();
void PrintSelf(std::ostream& os, Indent indent) const;
private:
AttributeOpeningLabelMapFilter(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
AttributeValueType m_Lambda;
bool m_ReverseOrdering;
} ; // end of class
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
#include "itkAttributeOpeningLabelMapFilter.txx"
#endif
#endif
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkAttributeOpeningLabelMapFilter.txx,v $
Language: C++
Date: $Date: 2005/08/23 15:09:03 $
Version: $Revision: 1.6 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 __itkAttributeOpeningLabelMapFilter_txx
#define __itkAttributeOpeningLabelMapFilter_txx
#include "itkAttributeOpeningLabelMapFilter.h"
#include "itkProgressReporter.h"
namespace itk {
template <class TImage, class TAttributeAccessor>
AttributeOpeningLabelMapFilter<TImage, TAttributeAccessor>
::AttributeOpeningLabelMapFilter()
{
m_Lambda = NumericTraits< AttributeValueType >::Zero;
m_ReverseOrdering = false;
}
template <class TImage, class TAttributeAccessor>
void
AttributeOpeningLabelMapFilter<TImage, TAttributeAccessor>
::GenerateData()
{
// Allocate the output
this->AllocateOutputs();
ImageType * output = this->GetOutput();
AttributeAccessorType accessor;
const typename ImageType::LabelObjectContainerType & labelObjectContainer = output->GetLabelObjectContainer();
ProgressReporter progress( this, 0, labelObjectContainer.size() );
typename ImageType::LabelObjectContainerType::const_iterator it = labelObjectContainer.begin();
while( it != labelObjectContainer.end() )
{
typedef typename ImageType::LabelObjectType LabelObjectType;
typename LabelObjectType::LabelType label = it->first;
LabelObjectType * labelObject = it->second;
if( ( !m_ReverseOrdering && accessor( labelObject ) < m_Lambda )
|| ( m_ReverseOrdering && accessor( labelObject ) > m_Lambda ) )
{
// must increment the iterator before removing the object to avoid invalidating the iterator
it++;
output->RemoveLabel( label );
}
else
{
it++;
}
progress.CompletedPixel();
}
}
template <class TImage, class TAttributeAccessor>
void
AttributeOpeningLabelMapFilter<TImage, TAttributeAccessor>
::PrintSelf(std::ostream &os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "ReverseOrdering: " << m_ReverseOrdering << std::endl;
os << indent << "Lambda: " << static_cast<typename NumericTraits<AttributeValueType>::PrintType>(m_Lambda) << std::endl;
}
}// end namespace itk
#endif
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkAttributeRelabelImageFilter.h,v $
Language: C++
Date: $Date: 2006/03/28 19:59:05 $
Version: $Revision: 1.6 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even