freSpatialRegionOfInterestImageFilter.txx

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   F.R.E.E. - flexible registration evaluation engine
00004   Version:   v.1.0.0
00005   Date:      $Date: 2006/09/01 12:00:00 $
00006   Module:    $RCSfile: freSpatialRegionOfInterestImageFilter.txx,v $
00007   Language:  C++
00008 
00009 
00010 
00011   Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics,
00012   Institute for Medical Biometry and Informatics, University of Heidelberg,
00013   Germany). All rights reserved.
00014   See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm
00015   for details.
00016 
00017      This software is distributed WITHOUT ANY WARRANTY; without even 
00018      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00019      PURPOSE.  See the above copyright notices for more information.
00020 
00021 =========================================================================*/
00022 
00023 #ifndef _freSpatialRegionOfInterestImageFilter_txx
00024 #define _freSpatialRegionOfInterestImageFilter_txx
00025 
00026 #include "freSpatialRegionOfInterestImageFilter.h"
00027 #include "itkImageRegionIterator.h"
00028 #include "itkImageRegionConstIterator.h"
00029 #include "itkObjectFactory.h"
00030 #include "itkProgressReporter.h"
00031 #include "itkImage.h"
00032 
00033 namespace FREE
00034 {
00035 
00039 template <class TInputImage, class TOutputImage>
00040 SpatialRegionOfInterestImageFilter<TInputImage,TOutputImage>
00041 ::SpatialRegionOfInterestImageFilter() 
00042 {
00043   m_CropByInput = true;
00044   m_OutsideValue = 0;
00045 }
00046 
00047 template <class TInputImage, class TOutputImage>
00048 void
00049 SpatialRegionOfInterestImageFilter<TInputImage,TOutputImage>
00050 ::SetSpatialRegionOfInterest(SpatialObjectType* pROI)
00051 {
00052   itkDebugMacro("setting SpatialObject to " << pROI );
00053   if (this->m_SpatialObject != pROI)
00054     {
00055       this->m_SpatialObject = pROI;
00056       this->Modified();
00057     }
00058 }
00059 
00060 template <class TInputImage, class TOutputImage>
00061 typename SpatialRegionOfInterestImageFilter<TInputImage,TOutputImage>::SpatialObjectType*
00062 SpatialRegionOfInterestImageFilter<TInputImage,TOutputImage>
00063 ::GetSpatialRegionOfInterest()
00064 {
00065   itkDebugMacro("returning SpatialObject address " << this->m_SpatialObject );
00066   return this->m_SpatialObject.GetPointer();
00067 };
00068 
00069 template <class TInputImage, class TOutputImage>
00070 typename SpatialRegionOfInterestImageFilter<TInputImage,TOutputImage>::RegionType 
00071 SpatialRegionOfInterestImageFilter<TInputImage,TOutputImage>
00072 ::GenerateLargestSpatialRegion(const TInputImage* pInput)
00073 {
00074   if (this->m_SpatialObject.IsNull())
00075   {
00076     return pInput->GetLargestPossibleRegion();
00077   }
00078   else
00079   { //compute via bounding box of spatial object
00080     m_SpatialObject->ComputeBoundingBox();
00081     typename SpatialObjectType::BoundingBoxType::Pointer boundingBox = m_SpatialObject->GetBoundingBox();
00082     
00083     typename SpatialObjectType::BoundingBoxType::PointType minPoint = boundingBox->GetMinimum();
00084     typename SpatialObjectType::BoundingBoxType::PointType maxPoint = boundingBox->GetMaximum();
00085 
00086     IndexType minIndex;
00087     IndexType maxIndex;
00088     pInput->TransformPhysicalPointToIndex(minPoint,minIndex);
00089     pInput->TransformPhysicalPointToIndex(maxPoint,maxIndex);
00090 
00091     IndexType regionIndex;
00092     SizeType regionSize;
00093     for (unsigned int iIndex = 0; iIndex<ImageDimension; iIndex++)
00094     {
00095       regionIndex[iIndex] = minIndex[iIndex];
00096       regionSize[iIndex] = maxIndex[iIndex] - minIndex[iIndex];
00097     }
00098 
00099     RegionType region(regionIndex,regionSize);
00100     
00101     if (m_CropByInput) region.Crop(pInput->GetLargestPossibleRegion());
00102     
00103     return region;
00104   }
00105 }
00106 
00110 template <class TInputImage, class TOutputImage>
00111 void 
00112 SpatialRegionOfInterestImageFilter<TInputImage,TOutputImage>
00113 ::PrintSelf(std::ostream& os, itk::Indent indent) const
00114 {
00115   Superclass::PrintSelf(os,indent);
00116 
00117   os << indent << "OutsideValue:     " << m_OutsideValue << std::endl;
00118   os << indent << "SpatialObject:    " << m_SpatialObject << std::endl;
00119 }
00120 
00121 
00122 
00123 template <class TInputImage, class TOutputImage>
00124 void 
00125 SpatialRegionOfInterestImageFilter<TInputImage,TOutputImage>
00126 ::GenerateInputRequestedRegion()
00127 {
00128   // call the superclass' implementation of this method
00129   Superclass::GenerateInputRequestedRegion();
00130   
00131   // get pointer to the input 
00132   typename Superclass::InputImagePointer  inputPtr = 
00133     const_cast< TInputImage * >( this->GetInput() );
00134 
00135   if( inputPtr )
00136     {
00137     // request the region of interest
00138     RegionType spatialROI = this->GenerateLargestSpatialRegion(inputPtr);
00139     inputPtr->SetRequestedRegion( spatialROI );
00140     }
00141 }
00142 
00143 
00144 template <class TInputImage, class TOutputImage>
00145 void 
00146 SpatialRegionOfInterestImageFilter<TInputImage,TOutputImage>
00147 ::EnlargeOutputRequestedRegion(itk::DataObject *output)
00148 {
00149   // call the superclass' implementation of this method
00150   Superclass::EnlargeOutputRequestedRegion(output);
00151   
00152   // generate everything in the region of interest
00153   output->SetRequestedRegionToLargestPossibleRegion();
00154 }
00155 
00156 
00157 
00158 
00168 template <class TInputImage, class TOutputImage>
00169 void 
00170 SpatialRegionOfInterestImageFilter<TInputImage,TOutputImage>
00171 ::GenerateOutputInformation()
00172 {
00173   // do not call the superclass' implementation of this method since
00174   // this filter allows the input the output to be of different dimensions
00175  
00176   // get pointers to the input and output
00177   typename Superclass::OutputImagePointer      outputPtr = this->GetOutput();
00178   typename Superclass::InputImageConstPointer  inputPtr  = this->GetInput();
00179 
00180   if ( !outputPtr || !inputPtr)
00181     {
00182     return;
00183     }
00184 
00185   RegionType spatialROI = this->GenerateLargestSpatialRegion(inputPtr);
00186 
00187   // Set the output image size to the same value as the region of interest.
00188   RegionType region;
00189   IndexType  start;
00190   start.Fill(0);
00191 
00192   region.SetSize( spatialROI.GetSize() );
00193   region.SetIndex( start );
00194  
00195   // Copy Information without modification.
00196   outputPtr->CopyInformation( inputPtr );
00197 
00198   // Adjust output region
00199   outputPtr->SetLargestPossibleRegion(region);
00200 
00201   // Correct origin of the extracted region.
00202   IndexType roiStart( spatialROI.GetIndex() );
00203   typename Superclass::OutputImageType::PointType  outputOrigin;
00204   typedef itk::Image< ITK_TYPENAME TInputImage::PixelType,
00205     Superclass::InputImageDimension > ImageType;
00206   typename ImageType::ConstPointer imagePtr =
00207     dynamic_cast< const ImageType * >( inputPtr.GetPointer() );
00208   if ( imagePtr )
00209     {
00210     // Input image supports TransformIndexToContinuousPoint
00211     inputPtr->TransformIndexToPhysicalPoint( roiStart, outputOrigin);
00212     }
00213   else
00214     {
00215     // Generic type of image
00216     const typename Superclass::InputImageType::PointType&
00217       inputOrigin = inputPtr->GetOrigin();
00218 
00219     const typename Superclass::InputImageType::SpacingType&
00220       spacing = inputPtr->GetSpacing() ;
00221  
00222     for( unsigned int i=0; i<ImageDimension; i++)
00223       {
00224       outputOrigin[i] = inputOrigin[i] + roiStart[i] * spacing[i];
00225       }
00226     }
00227   
00228   outputPtr->SetOrigin( outputOrigin );
00229 
00230 }
00231 
00232 
00233 
00246 template <class TInputImage, class TOutputImage>
00247 void 
00248 SpatialRegionOfInterestImageFilter<TInputImage,TOutputImage>
00249 ::ThreadedGenerateData(const RegionType& outputRegionForThread,
00250                        int threadId)
00251 {
00252   itkDebugMacro(<<"Actually executing");
00253 
00254   // Get the input and output pointers
00255   typename Superclass::InputImageConstPointer  inputPtr  = this->GetInput();
00256   typename Superclass::OutputImagePointer      outputPtr = this->GetOutput();
00257 
00258   // support progress methods/callbacks
00259   itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
00260   
00261   RegionType spatialROI = this->GenerateLargestSpatialRegion(inputPtr);
00262 
00263   // Define the portion of the input to walk for this thread
00264   InputImageRegionType inputRegionForThread;
00265   inputRegionForThread.SetSize( outputRegionForThread.GetSize() );
00266 
00267   IndexType start;
00268   IndexType roiStart(    spatialROI.GetIndex()    );
00269   IndexType threadStart( outputRegionForThread.GetIndex() );
00270   for(unsigned int i=0; i<ImageDimension; i++)
00271     {
00272     start[i] = roiStart[i] + threadStart[i];
00273     }
00274 
00275   inputRegionForThread.SetIndex( start );
00276   
00277   // Define the iterators.
00278   typedef itk::ImageRegionIterator<TOutputImage> OutputIterator;
00279   typedef itk::ImageRegionConstIterator<TInputImage> InputIterator;
00280 
00281   OutputIterator outIt(outputPtr, outputRegionForThread);
00282   InputIterator inIt(inputPtr, inputRegionForThread);
00283 
00284   // walk the output region, and sample the input image
00285   while( !outIt.IsAtEnd() )
00286     {
00287       //here is the adition to itk::RegionOfInterestImageFilter
00288       IndexType index = inIt.GetIndex();
00289       PointType point;
00290       inputPtr->TransformIndexToPhysicalPoint(index,point);
00291       if (m_SpatialObject->IsInside(point))
00292       {
00293         // copy the input pixel to the output
00294         outIt.Set( inIt.Get());
00295       }
00296       else
00297       { //use outside value
00298         outIt.Set( m_OutsideValue);
00299       }
00300 
00301       ++outIt; 
00302       ++inIt; 
00303       progress.CompletedPixel();
00304     }
00305 }
00306 
00307 } // end namespace itk
00308 
00309 #endif

Generated at Sat Oct 13 17:36:27 2007 for f.r.e.e. - Flexible Registration and Evaluation Engine by doxygen 1.5.3 written by Dimitri van Heesch, © 1997-2000