Commit 20720a49 authored by Manuel Grizonnet's avatar Manuel Grizonnet
Browse files

BUG: remove labelobject class from IJ (go to Review since 3.16)

parent bc1fac73
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkAutoCropLabelMapFilter.h,v $
Language: C++
Date: $Date: 2004/07/11 14:56:38 $
Version: $Revision: 1.4 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
Portions of this code are covered under the VTK copyright.
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.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 __itkAutoCropLabelMapFilter_h
#define __itkAutoCropLabelMapFilter_h
#include "itkChangeRegionLabelMapFilter.h"
namespace itk
{
/** \class AutoCropLabelMapFilter
* \brief Crop a LabelMap image to fit exactly the objects in the LabelMap
*
* The CropBorder can be used to add a border which will never be larger than
* the input image. To add a border of size independant of the input image,
* PadLabelMapFilter can be used.
*
* \author Gatan Lehmann. Biologie du Dveloppement et de la Reproduction, INRA de Jouy-en-Josas, France.
*
* \sa PadLabelMapFilter
* \ingroup ImageEnhancement MathematicalMorphologyImageFilters
*/
template <class TInputImage>
class ITK_EXPORT AutoCropLabelMapFilter : public ChangeRegionLabelMapFilter<TInputImage>
{
public:
/** Standard class typedefs. */
typedef AutoCropLabelMapFilter Self;
typedef ChangeRegionLabelMapFilter<TInputImage> Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
/** Run-time type information (and related methods). */
itkTypeMacro(AutoCropLabelMapFilter, ChangeRegionImageFilter);
/** Standard New method. */
itkNewMacro(Self);
/** Superclass typedefs. */
typedef typename Superclass::OutputImageType OutputImageType;
typedef typename Superclass::OutputImagePointer OutputImagePointer;
typedef typename Superclass::OutputImageRegionType OutputImageRegionType;
typedef typename Superclass::OutputImagePixelType OutputImagePixelType;
/** Some convenient typedefs. */
typedef TInputImage InputImageType;
typedef typename InputImageType::Pointer InputImagePointer;
typedef typename InputImageType::ConstPointer InputImageConstPointer;
typedef typename InputImageType::RegionType InputImageRegionType;
typedef typename InputImageType::PixelType InputImagePixelType;
typedef typename InputImageType::LabelObjectType LabelObjectType;
typedef typename InputImageType::PixelType PixelType;
typedef typename InputImageType::IndexType IndexType;
typedef typename InputImageType::SizeType SizeType;
typedef typename InputImageType::RegionType RegionType;
typedef TInputImage TOutputImage;
/** ImageDimension constants */
itkStaticConstMacro(InputImageDimension, unsigned int,
TInputImage::ImageDimension);
itkStaticConstMacro(OutputImageDimension, unsigned int,
TOutputImage::ImageDimension);
itkStaticConstMacro(ImageDimension, unsigned int,
TOutputImage::ImageDimension);
/**
* Set/Get the boder added to the mask before the crop. The default is 0 on
* all the axes.
*/
itkSetMacro(CropBorder, SizeType);
itkGetConstReferenceMacro(CropBorder, SizeType);
protected:
AutoCropLabelMapFilter();
~AutoCropLabelMapFilter() {};
virtual void GenerateOutputInformation();
private:
AutoCropLabelMapFilter(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
SizeType m_CropBorder;
TimeStamp m_CropTimeStamp;
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
#include "itkAutoCropLabelMapFilter.txx"
#endif
#endif
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkAutoCropLabelMapFilter.txx,v $
Language: C++
Date: $Date: 2004/07/11 14:56:39 $
Version: $Revision: 1.7 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
Portions of this code are covered under the VTK copyright.
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.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 _itkAutoCropLabelMapFilter_txx
#define _itkAutoCropLabelMapFilter_txx
#include "itkAutoCropLabelMapFilter.h"
namespace itk
{
template <class TInputImage>
AutoCropLabelMapFilter<TInputImage>
::AutoCropLabelMapFilter()
{
m_CropBorder.Fill( 0 );
}
template <class TInputImage>
void
AutoCropLabelMapFilter<TInputImage>
::GenerateOutputInformation()
{
const InputImageType * input = this->GetInput();
if( !(input->GetMTime() > m_CropTimeStamp) && !(this->GetMTime() > m_CropTimeStamp) )
{
// early exit, crop sizes already computed
// std::cout << "Don't recompute the output size again." << std::endl;
// std::cout << "LargestPossibleRegion: " << this->GetOutput()->GetLargestPossibleRegion() << std::endl;
// std::cout << "BufferedRegion: " << this->GetOutput()->GetBufferedRegion() << std::endl;
// std::cout << "RequestedRegion: " << this->GetOutput()->GetRequestedRegion() << std::endl;
return;
}
// update the input if needed
if( input->GetSource() )
{
ProcessObject * upstream = input->GetSource();
if (upstream)
{
// this->SetInput(NULL);
// std::cout << "Update the input (again?)." << std::endl;
upstream->Update();
// this->SetInput(input);
}
}
// Prefetch image region and size
InputImageRegionType cropRegion = input->GetLargestPossibleRegion();
// just find the bounding box of the objects
IndexType mins;
mins.Fill( NumericTraits< long >::max() );
IndexType maxs;
maxs.Fill( NumericTraits< long >::NonpositiveMin() );
// iterate over all the lines
typename InputImageType::LabelObjectContainerType container = this->GetInput()->GetLabelObjectContainer();
for( typename InputImageType::LabelObjectContainerType::const_iterator loit = container.begin();
loit != container.end();
loit++ )
{
const LabelObjectType * labelObject = loit->second;
typename LabelObjectType::LineContainerType::const_iterator lit;
const typename LabelObjectType::LineContainerType & lineContainer = labelObject->GetLineContainer();
for( lit = lineContainer.begin(); lit != lineContainer.end(); lit++ )
{
const IndexType & idx = lit->GetIndex();
unsigned long length = lit->GetLength();
// update the mins and maxs
for( int i=0; i<ImageDimension; i++)
{
if( idx[i] < mins[i] )
{
mins[i] = idx[i];
}
if( idx[i] > maxs[i] )
{
maxs[i] = idx[i];
}
}
// must fix the max for the axis 0
if( idx[0] + (long)length > maxs[0] )
{
maxs[0] = idx[0] + length - 1;
}
}
}
// final computation
SizeType regionSize;
for( int i=0; i<ImageDimension; i++ )
{
regionSize[i] = maxs[i] - mins[i] + 1;
}
cropRegion.SetIndex( mins );
cropRegion.SetSize( regionSize );
// pad by the crop border, but take care to not be larger than the largest
// possible region of the input image
cropRegion.PadByRadius( m_CropBorder );
cropRegion.Crop( input->GetLargestPossibleRegion() );
// finally set that region as the largest output region
this->SetRegion(cropRegion);
m_CropTimeStamp.Modified();
Superclass::GenerateOutputInformation();
// std::cout << "LargestPossibleRegion: " << this->GetOutput()->GetLargestPossibleRegion() << std::endl;
// std::cout << "BufferedRegion: " << this->GetOutput()->GetBufferedRegion() << std::endl;
// std::cout << "RequestedRegion: " << this->GetOutput()->GetRequestedRegion() << std::endl;
}
} // end namespace itk
#endif
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkBinaryImageToLabelMapFilter.h,v $
Language: C++
Date: $Date: 2007/02/18 20:03:03 $
Version: $Revision: 1.16 $
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 __itkBinaryImageToLabelMapFilter_h
#define __itkBinaryImageToLabelMapFilter_h
#include "itkLabelMap.h"
#include "itkLabelObject.h"
#include "itkImageToImageFilter.h"
#include "itkImage.h"
#include "itkConceptChecking.h"
#include <vector>
#include <map>
#include "itkProgressReporter.h"
#include "itkBarrier.h"
namespace itk
{
/**
* \class BinaryImageToLabelMapFilter
* \brief Label the connected components in a binary image and produce a collection of label objects
*
* BinaryImageToLabelMapFilter labels the objects in a binary image.
* Each distinct object is assigned a unique label.
* The final object labels start with 1 and are consecutive. Objects
* that are reached earlier by a raster order scan have a lower
* label.
*
* \author Gatan Lehmann. Biologie du Dveloppement et de la Reproduction, INRA de Jouy-en-Josas, France.
*
* \sa ConnectedComponentImageFilter, LabelImageToLabelMapFilter, LabelMap
*/
template <class TInputImage, class TOutputImage=LabelMap< LabelObject< unsigned long, TInputImage::ImageDimension > > >
class ITK_EXPORT BinaryImageToLabelMapFilter :
public ImageToImageFilter< TInputImage, TOutputImage >
{
public:
/**
* Standard "Self" & Superclass typedef.
*/
typedef BinaryImageToLabelMapFilter Self;
typedef ImageToImageFilter< TInputImage, TOutputImage > Superclass;
/**
* Types from the Superclass
*/
typedef typename Superclass::InputImagePointer InputImagePointer;
/**
* Extract some information from the image types. Dimensionality
* of the two images is assumed to be the same.
*/
typedef typename TOutputImage::PixelType OutputPixelType;
typedef typename TInputImage::PixelType InputPixelType;
itkStaticConstMacro(ImageDimension, unsigned int,
TOutputImage::ImageDimension);
itkStaticConstMacro(OutputImageDimension, unsigned int,
TOutputImage::ImageDimension);
itkStaticConstMacro(InputImageDimension, unsigned int,
TInputImage::ImageDimension);
/**
* Image typedef support
*/
typedef TInputImage InputImageType;
typedef typename TInputImage::IndexType IndexType;
typedef typename TInputImage::SizeType SizeType;
typedef typename TInputImage::OffsetType OffsetType;
typedef TOutputImage OutputImageType;
typedef typename TOutputImage::RegionType RegionType;
typedef typename TOutputImage::IndexType OutputIndexType;
typedef typename TOutputImage::SizeType OutputSizeType;
typedef typename TOutputImage::OffsetType OutputOffsetType;
typedef typename TOutputImage::PixelType OutputImagePixelType;
typedef std::list<IndexType> ListType;
/**
* Smart pointer typedef support
*/
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
/**
* Run-time type information (and related methods)
*/
itkTypeMacro(BinaryImageToLabelMapFilter, ImageToImageFilter);
/**
* Method for creation through the object factory.
*/
itkNewMacro(Self);
/**
* Set/Get whether the connected components are defined strictly by
* face connectivity or by face+edge+vertex connectivity. Default is
* FullyConnectedOff. For objects that are 1 pixel wide, use
* FullyConnectedOn.
*/
itkSetMacro(FullyConnected, bool);
itkGetConstReferenceMacro(FullyConnected, bool);
itkBooleanMacro(FullyConnected);
// only set after completion
itkGetConstReferenceMacro(ObjectCount, unsigned long);
// Concept checking -- input and output dimensions must be the same
itkConceptMacro(SameDimension,
(Concept::SameDimension<itkGetStaticConstMacro(InputImageDimension),
itkGetStaticConstMacro(OutputImageDimension)>));
/**
* Set/Get the value used as "background" in the output image.
* Defaults to NumericTraits<PixelType>::NonpositiveMin().
*/
itkSetMacro(BackgroundValue, OutputPixelType);
itkGetConstMacro(BackgroundValue, OutputPixelType);
/**
* Set/Get the value used as "foreground" in the output image.
* Defaults to NumericTraits<PixelType>::max().
*/
itkSetMacro(ForegroundValue, InputPixelType);
itkGetConstMacro(ForegroundValue, InputPixelType);
protected:
BinaryImageToLabelMapFilter()
{
m_FullyConnected = false;
m_ObjectCount = 0;
m_BackgroundValue = NumericTraits<OutputPixelType>::NonpositiveMin();
m_ForegroundValue = NumericTraits<InputPixelType>::max();
}
virtual ~BinaryImageToLabelMapFilter() {}
void PrintSelf(std::ostream& os, Indent indent) const;
/**
* Standard pipeline method.
*/
void BeforeThreadedGenerateData ();
void AfterThreadedGenerateData ();
void ThreadedGenerateData (const RegionType& outputRegionForThread, int threadId) ;
/** BinaryImageToLabelMapFilter needs the entire input. Therefore
* it must provide an implementation GenerateInputRequestedRegion().
* \sa ProcessObject::GenerateInputRequestedRegion(). */
void GenerateInputRequestedRegion();
/** BinaryImageToLabelMapFilter will produce all of the output.
* Therefore it must provide an implementation of
* EnlargeOutputRequestedRegion().
* \sa ProcessObject::EnlargeOutputRequestedRegion() */
void EnlargeOutputRequestedRegion(DataObject *itkNotUsed(output));
bool m_FullyConnected;
private:
BinaryImageToLabelMapFilter(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
OutputPixelType m_BackgroundValue;
InputPixelType m_ForegroundValue;
unsigned long m_ObjectCount;
// some additional types
typedef typename TOutputImage::RegionType::SizeType OutSizeType;
// types to support the run length encoding of lines
class runLength
{
public:
// run length information - may be a more type safe way of doing this
long int length;
typename InputImageType::IndexType where; // Index of the start of the run
unsigned long int label; // the initial label of the run
};
typedef std::vector<runLength> lineEncoding;
// the map storing lines
typedef std::vector<lineEncoding> LineMapType;
typedef std::vector<long> OffsetVec;
// the types to support union-find operations
typedef std::vector<unsigned long int> UnionFindType;
UnionFindType m_UnionFind;
UnionFindType m_Consecutive;
// functions to support union-find operations
void InitUnion(const unsigned long int size)
{
m_UnionFind = UnionFindType(size + 1);
}
void InsertSet(const unsigned long int label);
unsigned long int LookupSet(const unsigned long int label);
void LinkLabels(const unsigned long int lab1, const unsigned long int lab2);
unsigned long int CreateConsecutive();
//////////////////
bool CheckNeighbors(const OutputIndexType &A,
const OutputIndexType &B);
void CompareLines(lineEncoding &current, const lineEncoding &Neighbour);
void FillOutput(const LineMapType &LineMap,
ProgressReporter &progress);
void SetupLineOffsets(OffsetVec &LineOffsets);
void Wait()
{
long nbOfThreads = this->GetNumberOfThreads();
if( itk::MultiThreader::GetGlobalMaximumNumberOfThreads() != 0 )
{
nbOfThreads = std::min( this->GetNumberOfThreads(), itk::MultiThreader::GetGlobalMaximumNumberOfThreads() );
}
if( nbOfThreads > 1 )
{
m_Barrier->Wait();
}
}
typename std::vector< long > m_NumberOfLabels;
typename std::vector< long > m_FirstLineIdToJoin;
typename Barrier::Pointer m_Barrier;
#if !defined(CABLE_CONFIGURATION)
LineMapType m_LineMap;
#endif
};
} // end namespace itk
#ifndef ITK_MANUAL_INSTANTIATION
#if !defined(CABLE_CONFIGURATION)
#include "itkBinaryImageToLabelMapFilter.txx"
#endif
#endif
#endif
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkBinaryImageToLabelMapFilter.txx,v $
Language: C++
Date: $Date: 2007/02/20 15:28:29 $
Version: $Revision: 1.21 $
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 __itkBinaryImageToLabelMapFilter_txx
#define __itkBinaryImageToLabelMapFilter_txx
#include "itkBinaryImageToLabelMapFilter.h"
#include "itkNumericTraits.h"
// don't think we need the indexed version as we only compute the
// index at the start of each run, but there isn't a choice
#include "itkImageLinearConstIteratorWithIndex.h"
#include "itkConstShapedNeighborhoodIterator.h"
#include "itkImageRegionIterator.h"
#include "itkConnectedComponentAlgorithm.h"
namespace itk
{
template< class TInputImage, class TOutputImage >
void
BinaryImageToLabelMapFilter< TInputImage, TOutputImage >
::GenerateInputRequestedRegion()
{
// call the superclass' implementation of this method
Superclass::GenerateInputRequestedRegion();
// We need all the input.
InputImagePointer input = const_cast<InputImageType *>(this->GetInput());
if( !input )
{
return;
}
input->SetRequestedRegion( input->GetLargestPossibleRegion() );
}
template< class TInputImage, class TOutputImage >
void
BinaryImageToLabelMapFilter< TInputImage, TOutputImage>
::EnlargeOutputRequestedRegion(DataObject *)
{
this->GetOutput()
->SetRequestedRegion( this->GetOutput()->GetLargestPossibleRegion() );
}
template< class TInputImage, class TOutputImage >
void
BinaryImageToLabelMapFilter< TInputImage, TOutputImage >
::BeforeThreadedGenerateData()
{
typename TOutputImage::Pointer output = this->GetOutput();
typename TInputImage::ConstPointer input = this->GetInput();
output->SetBackgroundValue( m_BackgroundValue );
long nbOfThreads = this->GetNumberOfThreads();
if( itk::MultiThreader::GetGlobalMaximumNumberOfThreads() != 0 )
{
nbOfThreads = std::min( this->GetNumberOfThreads(), itk::MultiThreader::GetGlobalMaximumNumberOfThreads() );
}
// number of threads can be constrained by the region size, so call the SplitRequestedRegion
// to get the real number of threads which will be used
typename TOutputImage::RegionType splitRegion; // dummy region - just to call the following method
nbOfThreads = this->SplitRequestedRegion(0, nbOfThreads, splitRegion);
// std::cout << "nbOfThreads: " << nbOfThreads << std::endl;
// set up the vars used in the threads
m_NumberOfLabels.clear();
m_NumberOfLabels.resize( nbOfThreads, 0 );
m_Barrier = Barrier::New();
m_Barrier->Initialize( nbOfThreads );
long pixelcount = output->GetRequestedRegion().GetNumberOfPixels();
long xsize = output->GetRequestedRegion().GetSize()[0];
long linecount = pixelcount/xsize;
m_LineMap.resize( linecount );
m_FirstLineIdToJoin.resize( nbOfThreads - 1 );
}
template< class TInputImage, class TOutputImage >
void
BinaryImageToLabelMapFilter< TInputImage, TOutputImage >
::ThreadedGenerateData(const RegionType& outputRegionForThread,
int threadId)
{