/*========================================================================= Program: Insight Segmentation & Registration Toolkit Module: $RCSfile: itkLabelCollectionImageToBinaryImageFilter.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 __itkLabelCollectionImageToBinaryImageFilter_txx #define __itkLabelCollectionImageToBinaryImageFilter_txx #include "itkLabelCollectionImageToBinaryImageFilter.h" #include "itkNumericTraits.h" #include "itkProgressReporter.h" #include "itkImageRegionConstIterator.h" #include "itkImageRegionIterator.h" namespace itk { template LabelCollectionImageToBinaryImageFilter ::LabelCollectionImageToBinaryImageFilter() { m_BackgroundValue = NumericTraits::NonpositiveMin(); m_ForegroundValue = NumericTraits::max(); } template void LabelCollectionImageToBinaryImageFilter ::GenerateInputRequestedRegion() { // call the superclass' implementation of this method Superclass::GenerateInputRequestedRegion(); // We need all the input. InputImagePointer input = const_cast(this->GetInput()); if ( !input ) { return; } input->SetRequestedRegion( input->GetLargestPossibleRegion() ); } template void LabelCollectionImageToBinaryImageFilter ::EnlargeOutputRequestedRegion(DataObject *) { this->GetOutput() ->SetRequestedRegion( this->GetOutput()->GetLargestPossibleRegion() ); } template void LabelCollectionImageToBinaryImageFilter ::BeforeThreadedGenerateData() { m_Barrier = Barrier::New(); m_Barrier->Initialize( this->GetNumberOfThreads() ); Superclass::BeforeThreadedGenerateData(); } template void LabelCollectionImageToBinaryImageFilter ::ThreadedGenerateData( const OutputImageRegionType& outputRegionForThread, int threadId ) { OutputImageType * output = this->GetOutput(); const InputImageType * input = this->GetInput(); if( !input->GetUseBackground() ) { // no need to run the full process: the image will be foreground only ProgressReporter progress( this, threadId, outputRegionForThread.GetNumberOfPixels() ); ImageRegionIterator< OutputImageType > oIt( output, outputRegionForThread ); for( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt ) { oIt.Set( m_ForegroundValue ); progress.CompletedPixel(); } return; } // fill the output with background value - they will be overiden with the foreground value // later, if there is some objects if( this->GetNumberOfInputs() == 2 ) { // fill the background with the background values from the background image ImageRegionConstIterator< OutputImageType > bgIt( this->GetBackgroundImage(), outputRegionForThread ); ImageRegionIterator< OutputImageType > oIt( output, outputRegionForThread ); for( oIt.GoToBegin(), bgIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt, ++bgIt ) { const OutputImagePixelType & bg = bgIt.Get(); if( bg != m_ForegroundValue ) { oIt.Set( bg ); } else { oIt.Set( m_BackgroundValue ); } } } else { // fill the background with the background value ImageRegionIterator< OutputImageType > oIt( output, outputRegionForThread ); for( oIt.GoToBegin(); !oIt.IsAtEnd(); ++oIt ) { oIt.Set( m_BackgroundValue ); } } // wait for the other threads to complete that part m_Barrier->Wait(); // and delegate to the superclass implementation to use the thread support for the label objects Superclass::ThreadedGenerateData( outputRegionForThread, threadId ); } template void LabelCollectionImageToBinaryImageFilter ::ThreadedGenerateData( LabelObjectType * labelObject ) { OutputImageType * output = this->GetOutput(); typename InputImageType::LabelObjectType::LineContainerType::const_iterator lit; typename InputImageType::LabelObjectType::LineContainerType lineContainer = labelObject->GetLineContainer(); for( lit = lineContainer.begin(); lit != lineContainer.end(); lit++ ) { IndexType idx = lit->GetIndex(); unsigned long length = lit->GetLength(); for( int i=0; iSetPixel( idx, m_ForegroundValue ); idx[0]++; } } } }// end namespace itk #endif