[Insight-users] Modifications in the itkSpatialObjectToImageFilter.txx

Emiliano Beronich emiliano at veccsa.com
Tue Nov 1 14:33:57 EST 2005


Hi,

I have made some modifications in the itkSpatialObjectToImageFilter.txx 
class that I think it would be good to include in the 2.4 release of 
ITK. The modifications allow to calculate a specified region (instead of 
always the largest possible region) and add the method 
::GenerateOutputInformation(). There are a minor enhance of performance 
in the loop of the GenerateData() also.

Thanks
Emiliano Beronich
-------------- next part --------------
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkSpatialObjectToImageFilter.h,v $
  Language:  C++
  Date:      $Date: 2005/07/27 15:21:11 $
  Version:   $Revision: 1.10 $

  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 __itkSpatialObjectToImageFilter_h
#define __itkSpatialObjectToImageFilter_h

#include "itkImageSource.h"
#include "itkConceptChecking.h"

namespace itk
{
  
/** \class SpatialObjectToImageFilter
 * \brief Base class for filters that take a SpatialObject 
 *        as input and produce an image as output.
 *  By default, if the user does not specify the size of the output image,
 *  the maximum size of the object's bounding box is used. 
 *  The spacing of the image is given by the spacing of the input 
 *  Spatial object.
 */
template <class TInputSpatialObject, class TOutputImage>
class ITK_EXPORT SpatialObjectToImageFilter : public ImageSource<TOutputImage>
{
public:
  /** Standard class typedefs. */
  typedef SpatialObjectToImageFilter  Self;
  typedef ImageSource<TOutputImage>  Superclass;
  typedef SmartPointer<Self>  Pointer;
  typedef SmartPointer<const Self>  ConstPointer;
  typedef typename TOutputImage::SizeType   SizeType;
  typedef typename TOutputImage::PointType   PointType;
  typedef TOutputImage  OutputImageType;
  typedef typename OutputImageType::Pointer OutputImagePointer;
  typedef typename OutputImageType::ValueType  ValueType;
  typedef typename OutputImageType::SpacingType SpacingType;
  
  /** Method for creation through the object factory. */
  itkNewMacro(Self);
  
  /** Run-time type information (and related methods). */
  itkTypeMacro(SpatialObjectToImageFilter,ImageSource);

  /** Superclass typedefs. */
  typedef typename Superclass::OutputImageRegionType OutputImageRegionType;

  /** Some convenient typedefs. */
  typedef TInputSpatialObject InputSpatialObjectType;
  typedef typename InputSpatialObjectType::Pointer        InputSpatialObjectPointer;
  typedef typename InputSpatialObjectType::ConstPointer   InputSpatialObjectConstPointer;
  typedef typename TInputSpatialObject::ChildrenListType   ChildrenListType;

  /** ImageDimension constants */
  itkStaticConstMacro(ObjectDimension, unsigned int,
                      InputSpatialObjectType::ObjectDimension);
  itkStaticConstMacro(OutputImageDimension, unsigned int,
                      TOutputImage::ImageDimension);

  /** Set/Get the image input of this process object.  */
  virtual void SetInput( const InputSpatialObjectType *object);
  virtual void SetInput( unsigned int, const InputSpatialObjectType * object);
  const InputSpatialObjectType * GetInput(void);
  const InputSpatialObjectType * GetInput(unsigned int idx);

  /** Spacing (size of a pixel) of the output image. The
   * spacing is the geometric distance between image samples.
   * It is stored internally as double, but may be set from
   * float. \sa GetSpacing() */
  virtual void SetSpacing( const SpacingType& spacing);
  virtual void SetSpacing( const double* spacing);
  virtual void SetSpacing( const float* spacing);
  virtual const double* GetSpacing() const;

  /** Set/Get the value for pixels inside the spatial object. 
  * By default, this filter will return an image
  * that contains values from the spatial object specified as input. 
  * If this "inside" value is changed to a non-null value,
  * the output produced by this filter will be a mask with inside/outside values 
  * specified by the user. */
  itkSetMacro(InsideValue, ValueType);
  itkGetMacro(InsideValue, ValueType);

  /** Set/Get the value for pixels outside the spatial object.
  * By default, this filter will return an image
  * that contains values from the spatial object specified as input. 
  * If this "outside" value is changed to a non-null value,
  * the output produced by this filter will be a mask with inside/outside values
  * specified by the user. */
  itkSetMacro(OutsideValue, ValueType);
  itkGetMacro(OutsideValue, ValueType);

  /** The origin of the output image. The origin is the geometric
   * coordinates of the index (0,0,...,0).  It is stored internally
   * as double but may be set from float.
   * \sa GetOrigin() */
  virtual void SetOrigin( const PointType& origin);
  virtual void SetOrigin( const double* origin);
  virtual void SetOrigin( const float* origin);
  virtual const double * GetOrigin() const;

  /** The spatial object being transformed can be part of a hierarchy.
   * How deep in the hierarchy should we descend in generating the
   * image?  A ChildrenDepth of 0 means to only include the object
   * itself. */
  itkSetMacro(ChildrenDepth, unsigned int);
  itkGetMacro(ChildrenDepth, unsigned int);

  /** Set/Get Size */
  itkSetMacro(Size,SizeType);
  itkGetMacro(Size,SizeType);

  /** If UseObjectValue is set to true, then the filter uses
   *  the ValueAt() function instead of IsInside() */
  itkSetMacro(UseObjectValue,bool);
  itkGetMacro(UseObjectValue,bool);

  /** Apply changes to the output image information. */
  virtual void GenerateOutputInformation();


protected:
  SpatialObjectToImageFilter();
  ~SpatialObjectToImageFilter();

  virtual void GenerateData();


  SizeType     m_Size;
  double       m_Spacing[OutputImageDimension];
  double       m_Origin[OutputImageDimension];
  unsigned int m_ChildrenDepth;
  ValueType    m_InsideValue;
  ValueType    m_OutsideValue;
  bool         m_UseObjectValue;

  virtual void PrintSelf(std::ostream& os, Indent indent) const;

private:
  SpatialObjectToImageFilter(const Self&); //purposely not implemented
  void operator=(const Self&); //purposely not implemented


};

} // end namespace itk

#ifndef ITK_MANUAL_INSTANTIATION
#include "itkSpatialObjectToImageFilter.txx"
#endif

#endif
-------------- next part --------------
/*=========================================================================

Program:   Insight Segmentation & Registration Toolkit
Module:    $RCSfile: itkSpatialObjectToImageFilter.txx,v $
Language:  C++
Date:      $Date: 2005/08/29 20:06:17 $
Version:   $Revision: 1.20 $

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 _itkSpatialObjectToImageFilter_txx
#define _itkSpatialObjectToImageFilter_txx

#include "itkSpatialObjectToImageFilter.h"
#include <itkImageRegionIteratorWithIndex.h>

namespace itk
{

/** Constructor */
template <class TInputSpatialObject, class TOutputImage>
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::SpatialObjectToImageFilter()
{
  this->SetNumberOfRequiredInputs(1);
  m_ChildrenDepth = 1;
  m_Size.Fill(0);
  
  for (unsigned int i = 0; i < OutputImageDimension; i++)
    {
    m_Spacing[i] = 1.0;
    m_Origin[i] = 0.;
    }

  m_InsideValue = 0;
  m_OutsideValue = 0;
  m_UseObjectValue = false;
}

/** Destructor */
template <class TInputSpatialObject, class TOutputImage>
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::~SpatialObjectToImageFilter()
{
}


/** Set the Input SpatialObject */
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::SetInput(const InputSpatialObjectType *input)
{
  // Process object is not const-correct so the const_cast is required here
  this->ProcessObject::SetNthInput(0,
                                   const_cast< InputSpatialObjectType * >( input ) );
}


/** Connect one of the operands  */
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::SetInput( unsigned int index, const TInputSpatialObject * object )
{
  // Process object is not const-correct so the const_cast is required here
  this->ProcessObject::SetNthInput(index,
                                   const_cast< TInputSpatialObject *>( object ) );
}

template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::GenerateOutputInformation()
{
  unsigned int i;
  itkDebugMacro(<< "SpatialObjectToImageFilter::GenerateOutputInformation Start");

  // Get the input and output pointers
  const InputSpatialObjectType * InputObject  = this->GetInput();
  OutputImagePointer   OutputImage = this->GetOutput();

  // Generate the image
  typename OutputImageType::IndexType index;
  index.Fill(0);
  typename OutputImageType::RegionType region;

  // If the size of the output has been explicitly specified, the filter
  // will set the output size to the explicit size, otherwise the size from the spatial
  // object's bounding box will be used as default.

  bool specified = false;
  for (i = 0; i < OutputImageDimension; i++)
    {
    if (m_Size[i] != 0)
      {
      specified = true;
      break;
      }
    }

  if (specified)
    {
    region.SetSize( m_Size );
    }
  else
    {
    SizeType size;
    InputObject->ComputeBoundingBox();
    for(i=0;i<ObjectDimension;i++)
      {
      size[i] = (long unsigned int)(InputObject->GetBoundingBox()->GetMaximum()[i]
                                    - InputObject->GetBoundingBox()->GetMinimum()[i]);
      }
    region.SetSize( size );
    }
  region.SetIndex( index );

  specified = false;
  for (i = 0; i < OutputImageDimension; i++)
    {
    if (m_Spacing[i] != 0)
      {
      specified = true;
      break;
      }
    }

  OutputImage->SetLargestPossibleRegion( region);     //
  if (specified)
    {
    OutputImage->SetSpacing(this->m_Spacing);         // set spacing
    }
  else
    {
    OutputImage->SetSpacing(InputObject->GetIndexToObjectTransform()->GetScaleComponent());   // set spacing
    }

  OutputImage->SetOrigin(m_Origin);   //   and origin
  OutputImage->SetLargestPossibleRegion( region);     //
  itkDebugMacro(<< "SpatialObjectToImageFilter::GenerateOutputInformation End");
}

/** Get the input Spatial Object */
template <class TInputSpatialObject, class TOutputImage>
const typename SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>::InputSpatialObjectType *
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::GetInput(void)
{
  if (this->GetNumberOfInputs() < 1)
    {
    return 0;
    }

  return static_cast<const TInputSpatialObject * >
    (this->ProcessObject::GetInput(0) );
}
  
/** Get the input Spatial Object */
template <class TInputSpatialObject, class TOutputImage>
const typename SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>::InputSpatialObjectType *
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::GetInput(unsigned int idx)
{
  return static_cast< const TInputSpatialObject * >
    (this->ProcessObject::GetInput(idx));
}


//----------------------------------------------------------------------------
template <class TInputSpatialObject, class TOutputImage>
void 
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::SetSpacing(const SpacingType& spacing )
{
  unsigned int i; 
  for (i=0; i<TOutputImage::ImageDimension; i++)
    {
    if ( (double)spacing[i] != m_Spacing[i] )
      {
      break;
      }
    } 
  if ( i < TOutputImage::ImageDimension ) 
    { 
    for (i=0; i<TOutputImage::ImageDimension; i++)
      {
      m_Spacing[i] = spacing[i];
      }
    this->Modified();
    }
}

//----------------------------------------------------------------------------
template <class TInputSpatialObject, class TOutputImage>
void 
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::SetSpacing(const double* spacing)
{
  unsigned int i; 
  for (i=0; i<OutputImageDimension; i++)
    {
    if ( spacing[i] != m_Spacing[i] )
      {
      break;
      }
    } 
  if ( i < OutputImageDimension ) 
    { 
    for (i=0; i<OutputImageDimension; i++)
      {
      m_Spacing[i] = spacing[i];
      }
    }
}

template <class TInputSpatialObject, class TOutputImage>
void 
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::SetSpacing(const float* spacing)
{
  unsigned int i; 
  for (i=0; i<OutputImageDimension; i++)
    {
    if ( (double)spacing[i] != m_Spacing[i] )
      {
      break;
      }
    } 
  if ( i < OutputImageDimension ) 
    {
    for (i=0; i<OutputImageDimension; i++)
      {
      m_Spacing[i] = spacing[i];
      }
    }
}

template <class TInputSpatialObject, class TOutputImage>
const double * 
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::GetSpacing() const
{
  return m_Spacing;
}


//----------------------------------------------------------------------------
template <class TInputSpatialObject, class TOutputImage>
void 
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::SetOrigin(const PointType& origin)
{
  unsigned int i; 
  for (i=0; i<TOutputImage::ImageDimension; i++)
    {
    if ( (double)origin[i] != m_Origin[i] )
      {
      break;
      }
    } 
  if ( i < TOutputImage::ImageDimension ) 
    { 
    for (i=0; i<TOutputImage::ImageDimension; i++)
      {
      m_Origin[i] = origin[i];
      }
    this->Modified();
    }
}

//----------------------------------------------------------------------------
template <class TInputSpatialObject, class TOutputImage>
void 
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::SetOrigin(const double* origin)
{
  unsigned int i; 
  for (i=0; i<OutputImageDimension; i++)
    {
    if ( origin[i] != m_Origin[i] )
      {
      break;
      }
    } 
  if ( i < OutputImageDimension ) 
    { 
    for (i=0; i<OutputImageDimension; i++)
      {
      m_Origin[i] = origin[i];
      }
    this->Modified();
    }
}

template <class TInputSpatialObject, class TOutputImage>
void 
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::SetOrigin(const float* origin)
{
  unsigned int i; 
  for (i=0; i<OutputImageDimension; i++)
    {
    if ( (double)origin[i] != m_Origin[i] )
      {
      break;
      }
    } 
  if ( i < OutputImageDimension ) 
    { 
    for (i=0; i<OutputImageDimension; i++)
      {
      m_Origin[i] = origin[i];
      }
    this->Modified();
    }
}

template <class TInputSpatialObject, class TOutputImage>
const double * 
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::GetOrigin() const
{
  return m_Origin;
}

//----------------------------------------------------------------------------

/** Update */
template <class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::GenerateData(void)
{
//  unsigned int i;
  itkDebugMacro(<< "SpatialObjectToImageFilter::Update() called");

  // Get the input and output pointers
  const InputSpatialObjectType * InputObject  = this->GetInput();
  OutputImagePointer   OutputImage = this->GetOutput();

  typename OutputImageType::RegionType region;

  if (OutputImage->GetRequestedRegion( ).GetNumberOfPixels () == 0) {
    region = OutputImage->GetLargestPossibleRegion( );
    OutputImage->SetRequestedRegion( region );
  } else {
    region = OutputImage->GetRequestedRegion( );
  }
  OutputImage->SetBufferedRegion( region );           // set the region

  // If the spacing has been explicitly specified, the filter
  // will set the output spacing to that explicit spacing, otherwise the spacing from
  // the spatial object is used as default.

  OutputImage->Allocate();   // allocate the image

  typedef itk::ImageRegionIteratorWithIndex<OutputImageType> myIteratorType;

  myIteratorType it(OutputImage,region);

  itk::Point<double,ObjectDimension> point;

  if(m_UseObjectValue) {
    while(!it.IsAtEnd())
      {

      // ValueAt requires the point to be in physical coordinate i.e
      OutputImageType::IndexType Index = it.GetIndex();
      for(unsigned int i=0;i<ObjectDimension;i++)
        {
        point[i]=(Index[i]*m_Spacing[i])+m_Origin[i];
        }
      double val =0;

      InputObject->ValueAt(point,val,99999);
      if( m_OutsideValue != 0 )
        {
        if( val)
          {
            it.Set(static_cast<ValueType>(val));
          }
        else
          {
          it.Set(m_OutsideValue);
          }
        }
      else
        {
        it.Set(static_cast<ValueType>(val));
        }
      ++it;
      }
  } else {
    while(!it.IsAtEnd())
      {
      // ValueAt requires the point to be in physical coordinate i.e
      OutputImageType::IndexType Index = it.GetIndex();
      for(unsigned int i=0;i<ObjectDimension;i++)
        {
        point[i]=(Index[i]*m_Spacing[i])+m_Origin[i];
        }
      bool PointIsInside = InputObject->IsInside(point);
      if( PointIsInside )
        {
        it.Set(m_InsideValue);
        }
      else
        {
        it.Set(m_OutsideValue);
        }
      ++it;
      }
    }
  itkDebugMacro(<< "SpatialObjectToImageFilter::Update() finished");

} // end update function


template<class TInputSpatialObject, class TOutputImage>
void
SpatialObjectToImageFilter<TInputSpatialObject,TOutputImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
  Superclass::PrintSelf(os, indent);
  os << indent << "Size : " << m_Size << std::endl;
  os << indent << "Children depth : " << m_ChildrenDepth << std::endl;
  os << indent << "Inside Value : " << m_InsideValue << std::endl;
  os << indent << "Outside Value : " << m_OutsideValue << std::endl;
  if(m_UseObjectValue)
    {
    os << indent << "Using Object Value : ON" << std::endl;
    }
  else
    {
    os << indent << "Using Object Value : OFF" << std::endl;
    }
}



} // end namespace itk

#endif


More information about the Insight-users mailing list