17 #ifndef __itkLabelMapMaskImageFilter_txx
18 #define __itkLabelMapMaskImageFilter_txx
21 #include "itkNumericTraits.h"
28 template <
class TInputImage,
class TOutputImage>
32 this->SetNumberOfRequiredInputs(2);
33 m_Label = NumericTraits< InputImagePixelType >::One;
34 m_BackgroundValue = NumericTraits< OutputImagePixelType >::Zero;
37 m_CropBorder.Fill( 0 );
40 template <
class TInputImage,
class TOutputImage>
46 Superclass::GenerateInputRequestedRegion();
52 input->SetRequestedRegion( input->GetLargestPossibleRegion() );
55 template <
class TInputImage,
class TOutputImage>
65 if( !(input->GetMTime() > m_CropTimeStamp) && !(this->GetMTime() > m_CropTimeStamp) )
76 Superclass::GenerateOutputInformation();
79 if( input->GetSource())
97 if( input->GetBackgroundValue() != m_Label )
104 itkWarningMacro( <<
"Cropping according to background label is no yet implemented. The full image will be used." );
111 mins.Fill( NumericTraits< long >::max() );
113 maxs.Fill( NumericTraits< long >::NonpositiveMin() );
114 typename InputImageType::LabelObjectContainerType container = this->GetInput()->GetLabelObjectContainer();
115 for(
typename InputImageType::LabelObjectContainerType::const_iterator loit = container.begin();
116 loit != container.end();
119 if( loit->first != m_Label )
121 typename LabelObjectType::LineContainerType::const_iterator lit;
122 typename LabelObjectType::LineContainerType & lineContainer = loit->second->GetLineContainer();
124 for( lit = lineContainer.begin(); lit != lineContainer.end(); lit++ )
127 unsigned long length = lit->GetLength();
130 for(
int i=0; i<ImageDimension; i++)
132 if( idx[i] < mins[i] )
136 if( idx[i] > maxs[i] )
142 if( idx[0] + (
long)length > maxs[0] )
144 maxs[0] = idx[0] + length - 1;
152 for(
int i=0; i<ImageDimension; i++ )
154 regionSize[i] = maxs[i] - mins[i] + 1;
156 cropRegion.SetIndex( mins );
157 cropRegion.SetSize( regionSize );
163 if( input->GetBackgroundValue() == m_Label )
170 itkWarningMacro( <<
"Cropping according to background label is no yet implemented. The full image will be used." );
177 const LabelObjectType * labelObject = input->GetLabelObject( m_Label );
178 typename LabelObjectType::LineContainerType::const_iterator lit;
179 const typename LabelObjectType::LineContainerType & lineContainer = labelObject->GetLineContainer();
181 mins.Fill( NumericTraits< long >::max() );
183 maxs.Fill( NumericTraits< long >::NonpositiveMin() );
185 for( lit = lineContainer.begin(); lit != lineContainer.end(); lit++ )
188 unsigned long length = lit->GetLength();
191 for(
int i=0; i<ImageDimension; i++)
193 if( idx[i] < mins[i] )
197 if( idx[i] > maxs[i] )
203 if( idx[0] + (
long)length > maxs[0] )
205 maxs[0] = idx[0] + length - 1;
210 for(
int i=0; i<ImageDimension; i++ )
212 regionSize[i] = maxs[i] - mins[i] + 1;
214 cropRegion.SetIndex( mins );
215 cropRegion.SetSize( regionSize );
222 cropRegion.PadByRadius( m_CropBorder );
223 cropRegion.Crop( input->GetLargestPossibleRegion() );
226 this->GetOutput()->SetLargestPossibleRegion( cropRegion );
228 m_CropTimeStamp.Modified();
234 Superclass::GenerateOutputInformation();
243 template <
class TInputImage,
class TOutputImage>
249 ->SetRequestedRegion( this->GetOutput()->GetLargestPossibleRegion() );
253 template <
class TInputImage,
class TOutputImage>
259 m_Barrier->Initialize( this->GetNumberOfThreads() );
261 Superclass::BeforeThreadedGenerateData();
266 template <
class TInputImage,
class TOutputImage>
277 if( ( input->GetBackgroundValue() == m_Label ) ^ m_Negated )
286 ++featureIt, ++outputIt )
288 outputIt.
Set( featureIt.
Get() );
297 outputIt.
Set( m_BackgroundValue );
304 if( input->GetBackgroundValue() == m_Label )
307 Superclass::ThreadedGenerateData( outputRegionForThread, threadId );
314 const LabelObjectType * labelObject = this->GetLabelMap()->GetLabelObject( m_Label );
315 ProgressReporter progress(
this, 0, labelObject->GetLineContainer().size() );
319 typename InputImageType::LabelObjectType::LineContainerType::const_iterator lit;
320 const typename InputImageType::LabelObjectType::LineContainerType & lineContainer = labelObject->GetLineContainer();
322 for( lit = lineContainer.begin(); lit != lineContainer.end(); lit++ )
325 unsigned long length = lit->GetLength();
326 for(
unsigned int i=0; i<length; i++)
328 output->SetPixel( idx, input2->GetPixel( idx ) );
339 bool testIdxIsInside = m_Crop && ( input->GetBackgroundValue() == m_Label ) ^ m_Negated;
340 RegionType outputRegion = output->GetLargestPossibleRegion();
342 typename InputImageType::LabelObjectType::LineContainerType::const_iterator lit;
343 const typename InputImageType::LabelObjectType::LineContainerType & lineContainer = labelObject->GetLineContainer();
345 for( lit = lineContainer.begin(); lit != lineContainer.end(); lit++ )
348 unsigned long length = lit->GetLength();
349 for(
unsigned int i=0; i<length; i++)
351 if( !testIdxIsInside || outputRegion.IsInside( idx ) )
353 output->SetPixel( idx, m_BackgroundValue );
357 progress.CompletedPixel();
366 template<
class TInputImage,
class TOutputImage>
381 bool testIdxIsInside = m_Crop && ( input->GetBackgroundValue() == m_Label ) ^ m_Negated;
382 RegionType outputRegion = output->GetLargestPossibleRegion();
385 typename InputImageType::LabelObjectType::LineContainerType::const_iterator lit;
386 typename InputImageType::LabelObjectType::LineContainerType & lineContainer = labelObject->GetLineContainer();
388 for( lit = lineContainer.begin(); lit != lineContainer.end(); lit++ )
391 unsigned long length = lit->GetLength();
392 for(
unsigned int i=0; i<length; i++)
394 if( !testIdxIsInside || outputRegion.IsInside( idx ) )
396 output->SetPixel( idx, m_BackgroundValue );
409 typename InputImageType::LabelObjectType::LineContainerType::const_iterator lit;
410 typename InputImageType::LabelObjectType::LineContainerType & lineContainer = labelObject->GetLineContainer();
412 for( lit = lineContainer.begin(); lit != lineContainer.end(); lit++ )
415 unsigned long length = lit->GetLength();
416 for(
unsigned int i=0; i<length; i++)
418 output->SetPixel( idx, input2->GetPixel( idx ) );
426 template<
class TInputImage,
class TOutputImage>
431 Superclass::PrintSelf(os,indent);
433 os << indent <<
"Label: " <<
static_cast<typename NumericTraits<LabelType>::PrintType
>(m_Label) << std::endl;
434 os << indent <<
"BackgroundValue: " <<
static_cast<typename NumericTraits<OutputImagePixelType>::PrintType
>(m_BackgroundValue) << std::endl;
435 os << indent <<
"Negated: " << m_Negated << std::endl;
436 os << indent <<
"Crop: " << m_Crop << std::endl;
437 os << indent <<
"CropBorder: " << m_CropBorder << std::endl;