From: dsarrut Date: Wed, 30 Jun 2010 06:01:07 +0000 (+0000) Subject: auto crop of binary image X-Git-Tag: v1.2.0~594 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=13ddd066e351ce733c75d38d0fb018bb6dba34b9;p=clitk.git auto crop of binary image --- diff --git a/itk/clitkAutoCropFilter.h b/itk/clitkAutoCropFilter.h new file mode 100644 index 0000000..b294efb --- /dev/null +++ b/itk/clitkAutoCropFilter.h @@ -0,0 +1,91 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html + ======================================================================-====*/ + +#ifndef CLITKAUTOCROPFILTER_H +#define CLITKAUTOCROPFILTER_H + +#include + +namespace clitk { + + //-------------------------------------------------------------------- + /* + Perform auto crop on a Label Image (with Background defined) + */ + //-------------------------------------------------------------------- + + template + class ITK_EXPORT AutoCropFilter: + public itk::ImageToImageFilter { + + public: + /** Standard class typedefs. */ + typedef AutoCropFilter Self; + typedef itk::ImageToImageFilter Superclass; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(AutoCropFilter, ImageToImageFilter); + + /** Some convenient typedefs. */ + typedef TImageType ImageType; + typedef typename ImageType::ConstPointer ImageConstPointer; + typedef typename ImageType::Pointer ImagePointer; + typedef typename ImageType::PixelType ImagePixelType; + typedef typename ImageType::RegionType ImageRegionType; + typedef long LabelType; + + /** Connect one of the operands for pixel-wise addition */ + void SetInput( const ImageType * image); + + // LabelImage information (BG) + void SetBackgroundValue(ImagePixelType p); + + /** ImageDimension constants */ + itkStaticConstMacro(ImageDimension, unsigned int, TImageType::ImageDimension); + + protected: + AutoCropFilter(); + virtual ~AutoCropFilter() {} + + virtual void GenerateOutputInformation(); + virtual void GenerateData(); + + ImagePixelType m_BackgroundValue; + ImageRegionType m_Region; + ImagePointer m_labeImage; + + private: + AutoCropFilter(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + + }; // end class + //-------------------------------------------------------------------- + +} // end namespace clitk +//-------------------------------------------------------------------- + +#ifndef ITK_MANUAL_INSTANTIATION +#include "clitkAutoCropFilter.txx" +#endif + +#endif diff --git a/itk/clitkAutoCropFilter.txx b/itk/clitkAutoCropFilter.txx new file mode 100644 index 0000000..46e3587 --- /dev/null +++ b/itk/clitkAutoCropFilter.txx @@ -0,0 +1,141 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.fr/ + - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - BSD See included LICENSE.txt file + - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html + ======================================================================-====*/ + +#ifndef CLITKAUTOCROPFILTER_TXX +#define CLITKAUTOCROPFILTER_TXX + +// clitk +#include "clitkCommon.h" +#include "clitkSegmentationUtils.h" + +// itk +#include "itkAutoCropLabelMapFilter.h" +#include "itkStatisticsLabelObject.h" +#include "itkLabelImageToLabelMapFilter.h" +#include "itkLabelMapToLabelImageFilter.h" +#include "itkRegionOfInterestImageFilter.h" + +namespace clitk { + + //-------------------------------------------------------------------- + template + AutoCropFilter:: + AutoCropFilter():itk::ImageToImageFilter() { + this->SetNumberOfRequiredInputs(1); + m_BackgroundValue = 0; + } + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void + AutoCropFilter:: + SetInput(const TImageType * image) { + // Process object is not const-correct so the const casting is required. + this->SetNthInput(0, const_cast( image )); + } + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void + AutoCropFilter:: + SetBackgroundValue(ImagePixelType p) { + m_BackgroundValue = p; + } + //-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- + template + void + AutoCropFilter:: + GenerateOutputInformation() { + + // Superclass + // do not call the superclass' implementation of this method since + // this filter allows the input the output to be of different dimensions + // Superclass::GenerateOutputInformation(); + + // Get input pointers + ImageConstPointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); + + // Get output pointer + ImagePointer output = this->GetOutput(0); + + // Convert to LabelMap + static const unsigned int Dim = ImageType::ImageDimension; + // typedef unsigned long LabelType; // unsigned long needed (!!??) + typedef itk::StatisticsLabelObject< LabelType, Dim > LabelObjectType; + typedef itk::LabelMap< LabelObjectType > LabelMapType; + typedef itk::LabelImageToLabelMapFilter ImageToMapFilterType; + typename ImageToMapFilterType::Pointer imageToLabelFilter = ImageToMapFilterType::New(); + imageToLabelFilter->SetBackgroundValue(m_BackgroundValue); + imageToLabelFilter->SetInput(input); + + // AutoCrop + typedef itk::AutoCropLabelMapFilter AutoCropFilterType; + typename AutoCropFilterType::Pointer autoCropFilter = AutoCropFilterType::New(); + autoCropFilter->SetInput(imageToLabelFilter->GetOutput()); + + // Convert to LabelImage + typedef itk::LabelMapToLabelImageFilter MapToImageFilterType; + typename MapToImageFilterType::Pointer labelToImageFilter = MapToImageFilterType::New(); + labelToImageFilter->SetInput(autoCropFilter->GetOutput()); + + // Go ! (needed) + labelToImageFilter->Update(); + m_labeImage = labelToImageFilter->GetOutput(); + + // Update the output size + m_Region = m_labeImage->GetLargestPossibleRegion(); + output->SetLargestPossibleRegion(m_Region); + output->SetRequestedRegion(m_Region); + output->SetBufferedRegion(m_Region); + output->SetRegions(m_Region); + } + //-------------------------------------------------------------------- + + //-------------------------------------------------------------------- + template + void + AutoCropFilter:: + GenerateData() { + // Get input pointers + ImageConstPointer input = dynamic_cast(itk::ProcessObject::GetInput(0)); + + // Extract the region + typedef itk::RegionOfInterestImageFilter CropFilterType; + m_labeImage->SetRequestedRegion(m_labeImage->GetLargestPossibleRegion()); + typename CropFilterType::Pointer cropFilter = CropFilterType::New(); + cropFilter->SetInput(m_labeImage); + cropFilter->SetRegionOfInterest(m_Region); + + // Go ! + cropFilter->Update(); + + // Get (graft) output (SetNthOutput does not fit here because of Origin). + this->GraftOutput(cropFilter->GetOutput()); + } + //-------------------------------------------------------------------- + +}//end clitk + +#endif //#define CLITKAUTOCROPFILTER