freROI.h

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: freROI.h,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 __freROI_h
00024 #define __freROI_h
00025 
00026 #include "freElementals.h"
00027 #include "freImageTypes.h"
00028 #include "freImageIO.h"
00029 
00030 #include "itkMaskImageFilter.h"
00031 #include "itkImageMaskSpatialObject.h"
00032 #include "itkRegionOfInterestImageFilter.h"
00033 
00034 namespace FREE
00035 {
00036 
00047 template<unsigned int VImageDimension>
00048 class ImageROIBase : public itk::LightObject
00049 {
00050 public:
00052     typedef ImageROIBase  Self;
00053     typedef itk::LightObject Superclass;
00054     typedef itk::SmartPointer<Self>        Pointer;
00055     typedef itk::SmartPointer<const Self>  ConstPointer;
00056 
00057     typedef typename ImageTypes<VImageDimension>::PointType PointType;
00058     typedef typename ImageTypes<VImageDimension>::InternalImageType ImageType;
00059     typedef typename ImageType::Pointer ImagePointer;
00060     typedef typename ImageType::RegionType RegionType;
00061     typedef typename ImageType::SpacingType SpacingType;
00062 
00063     typedef itk::SpatialObject<VImageDimension> SpatialObjectType;
00064     typedef typename SpatialObjectType::Pointer SpatialObjectPointer;
00065 
00067     itkTypeMacro( ImageROIBase, itk::LightObject );
00068     itkNewMacro(Self);
00069 
00070     static unsigned int GetImageDimension()
00071     {
00072         return VImageDimension;
00073     };
00074 
00079     ScalarType GetOriginPoint(const unsigned int& iDimension) const
00080     {
00081         if (iDimension<VImageDimension)
00082             return m_dOrigin[iDimension];
00083         else
00084             return 0.0;
00085     };
00086 
00092     void SetOriginPoint(const ScalarType& dPoint, const unsigned int& iDimension)
00093     {
00094         if (iDimension<VImageDimension)
00095         {
00096             m_dOrigin[iDimension] = dPoint;
00097         }
00098     };
00099 
00104     ScalarType GetSize(const unsigned int& iDimension) const
00105     {
00106         if (iDimension<VImageDimension)
00107             return m_dSize[iDimension];
00108         else
00109             return 0.0;
00110     };
00111 
00116     void SetSize(const ScalarType& dSize, const unsigned int& iDimension)
00117     {
00118         if (iDimension<VImageDimension)
00119         {
00120             m_dSize[iDimension] = dSize;
00121         }
00122     };
00123 
00124     virtual void Reset()
00125     {
00126         for (unsigned int i = 0; i < VImageDimension; i++)
00127         {
00128             m_dOrigin[i] = 0.0;
00129             m_dSize[i] = 0.0;
00130         }
00131     };
00132 
00138     virtual SpatialObjectPointer GetSpatialObject(const ImageType* pImage)
00139     {
00140         SpatialObjectPointer result = SpatialObjectType::New();
00141         result->SetSpacing(pImage->GetSpacing().GetDataPointer());
00142         result->SetLargestPossibleRegion(GetITKImageRegion(pImage->GetSpacing()));
00143         return result;
00144     };
00145 
00152     RegionType GetITKImageRegion(const SpacingType& rSpacing)
00153     {
00154         typename RegionType::SizeType size;
00155         typename RegionType::IndexType index;
00156 
00157         for (unsigned int iDim=0; iDim<VImageDimension; iDim++)
00158         {
00159             size[iDim] = GetSize(iDim)/rSpacing[iDim];
00160             index[iDim] = GetOriginPoint(iDim)/rSpacing[iDim];
00161         };
00162 
00163         return RegionType(index,size);
00164     };
00165 
00173     ImagePointer ExtractImageByROI(const ImageType* pImage)
00174     {
00175         ImagePointer extractImg;
00176         ExtractImageByROI<ImageType>(pImage,extractImg);
00177         return extractImg;
00178     }
00179 
00188     template< class TImage >
00189     void ExtractImageByROI(const TImage* pImage, typename TImage::Pointer& rExtractedImage)
00190     {
00191         //Extract the needed part of the image
00192         typedef itk::RegionOfInterestImageFilter< TImage, TImage > ImageExtractorType;
00193 
00194         typename ImageExtractorType::Pointer extractor = ImageExtractorType::New();
00195 
00196         RegionType region = GetITKImageRegion(pImage->GetSpacing());
00197 
00198         if (!region.Crop(pImage->GetLargestPossibleRegion()))
00199         {// region was out of bound, so set size to zero
00200             typename RegionType::SizeType size;
00201             size.Fill(0);
00202             region.SetSize(size);
00203         };
00204 
00205         extractor->SetRegionOfInterest(region);
00206         extractor->SetInput(pImage);
00207         extractor->Update();
00208 
00209         rExtractedImage = extractor->GetOutput();
00210     }
00211 
00222     virtual ImagePointer ReadyImageByROI(const ImageType* pImage)
00223     {
00224         return ExtractImageByROI(pImage);
00225     }
00226 
00239     virtual bool PointWithinROI(PointType point)
00240     {
00241         for(unsigned int i=0; i<VImageDimension; i++)
00242         {
00243             if( point[i] < m_dOrigin[i] )
00244             {
00245                 return false;
00246             }
00247             if( point[i] >= m_dOrigin[i] + m_dSize[i] )
00248             {
00249                 return false;
00250             }
00251         }
00252         return true;
00253     }
00254 
00255     virtual ~ImageROIBase()
00256     { }
00257     ;
00258 
00259 protected:
00260     ImageROIBase()
00261     {
00262         Reset();
00263     };
00264 
00265     ScalarType m_dSize[VImageDimension];
00266     ScalarType m_dOrigin[VImageDimension];
00267 }
00268 ; //class ImageROIBase
00269 
00283 template<unsigned int VImageDimension>
00284 class ImageMaskedROI : public ImageROIBase<VImageDimension>
00285 {
00286 public:
00288     typedef ImageMaskedROI  Self;
00289     typedef ImageROIBase<VImageDimension> Superclass;
00290     typedef itk::SmartPointer<Self>        Pointer;
00291     typedef itk::SmartPointer<const Self>  ConstPointer;
00292 
00293     typedef typename ImageTypes<VImageDimension>::MaskType MaskType;
00294     typedef typename MaskType::Pointer MaskPointer;
00295 
00296     typedef typename Superclass::PointType PointType;
00297     typedef typename Superclass::ImageType ImageType;
00298     typedef typename Superclass::ImagePointer ImagePointer;
00299     typedef typename Superclass::RegionType RegionType;
00300     typedef typename Superclass::SpacingType SpacingType;
00301 
00302     typedef typename Superclass::SpatialObjectType SpatialObjectType;
00303     typedef typename Superclass::SpatialObjectPointer SpatialObjectPointer;
00304 
00306     itkTypeMacro( ImageMaskedROIBase, ImageROIBase );
00307     itkNewMacro(Self);
00308 
00309     virtual void Reset()
00310     {
00311         for (unsigned int i = 0; i < VImageDimension; i++)
00312         {
00313             this->m_dOrigin[i] = 0.0;
00314             this->m_dSize[i] = 0.0;
00315         }
00316     };
00317 
00319     const MaskType* GetMask() const
00320     {
00321         return m_smpMask;
00322     };
00323     MaskType* GetMask()
00324     {
00325         return m_smpMask;
00326     };
00327     MaskPointer& GetMaskSmartPointer()
00328     {
00329         return m_smpMask;
00330     };
00331 
00333     void SetMask(MaskType* pMask)
00334     {
00335         m_smpMask = pMask;
00336     };
00337 
00340     void SetMask(const std::string& sMaskFile)
00341     {
00342         typedef ImageReader<LoadingPixelType,typename MaskType::PixelType,VImageDimension> ImageReaderType;
00343         ImageReaderType ImageReader;
00344         ImageReader.SetFileName(sMaskFile);
00345         try
00346         {
00347             m_smpMask = ImageReader.GetOutput();
00348         }
00349         catchAllNPassMacro("Error; while loading mask from file: " << sMaskFile);
00350 
00351         m_sMaskFile = sMaskFile;
00352 
00353         //TODO entscheiden, ob hier auch gleich geladen werden soll, bzw. wann dies geschehen soll beim Build oder actualize
00354         //passieren. zum anpassen Maske a ROI sollte bild in gre der ROI erzeugt werden und dann itkConstantPadImageFilter
00355         //zum kopieren benutzt werden.
00356     };
00357 
00359     std::string GetMaskFileName() const
00360     {
00361         return m_sMaskFile;
00362     };
00363 
00366     void SetMaskFileName(const std::string& sMaskFile)
00367     {
00368         m_sMaskFile = sMaskFile;
00369     };
00370 
00371     void SetMaskImage(const bool& bMaskImage)
00372     {
00373         this->m_bMaskReadyImage = bMaskImage;
00374     };
00375     bool GetMaskImage()
00376     {
00377         return this->m_bMaskReadyImage;
00378     };
00379 
00380     void SetMaskAtROIOrigin(const bool& bSameOrigin)
00381     {
00382         this->m_bMaskAtROIOrigin = bSameOrigin;
00383     };
00384     bool GetMaskAtROIOrigin()
00385     {
00386         return this->m_bMaskAtSameOrigin;
00387     };
00388 
00394     virtual SpatialObjectPointer GetSpatialObject(const ImageType* pImage)
00395     {
00396         typedef itk::ImageMaskSpatialObject<VImageDimension> MaskSpatialObjectType;
00397         typename MaskSpatialObjectType::Pointer spatialObject = MaskSpatialObjectType::New();
00398         spatialObject->SetImage(m_smpMask);
00399         SpatialObjectPointer result = static_cast<SpatialObjectType*>(spatialObject.GetPointer());
00400         return result;
00401     };
00402 
00413     virtual ImagePointer ReadyImageByROI(const ImageType* pImage)
00414     {
00415         ImagePointer tempImage = ExtractImageByROI(pImage);
00416 
00417         if (m_smpMask.IsNotNull())
00418         { //Mask the Image
00419             MaskPointer tempMask;
00420             Superclass::template ExtractImageByROI<MaskType>(m_smpMask,tempMask);
00421 
00422             typedef itk::MaskImageFilter< ImageType, MaskType, ImageType > MaskFilterType;
00423             typename MaskFilterType::Pointer maskFilter = MaskFilterType::New();
00424 
00425             maskFilter->SetInput1(tempImage);
00426             maskFilter->SetInput2(tempMask);
00427             maskFilter->Update();
00428             tempImage = maskFilter->GetOutput();
00429         }
00430         return tempImage;
00431     }
00432 
00445     virtual bool PointWithinROI(PointType point)
00446     {
00447         if (!Superclass::PointWithinROI(point))
00448             return false;
00449 
00450         if (m_smpMask.IsNull())
00451             return true;
00452 
00453         typename MaskType::IndexType index;
00454         if (m_smpMask->TransformPhysicalPointToIndex(point, index))
00455             return m_smpMask->GetPixel(index)>0;
00456 
00457         return false;
00458     }
00459 
00460 protected:
00461     ImageMaskedROI()
00462     {
00463         Reset();
00464     };
00465 
00466     MaskPointer m_smpMask;
00467     std::string m_sMaskFile; //File of the mask, will only be used when mask is not set.
00468 }
00469 ; //class ImageMaskedROI
00470 
00480 template<class TROI>
00481 class ROICoupleBase : public itk::LightObject
00482 {
00483 public:
00485     typedef ROICoupleBase  Self;
00486     typedef itk::LightObject Superclass;
00487     typedef itk::SmartPointer<Self>        Pointer;
00488     typedef itk::SmartPointer<const Self>  ConstPointer;
00489 
00490     typedef TROI ROIType;
00491     typedef typename ROIType::Pointer ROIPointer;
00492     typedef typename ROIType::PointType PointType;
00493     typedef typename ROIType::ImageType ImageType;
00494     typedef typename ROIType::ImagePointer ImagePointer;
00495     typedef typename ROIType::RegionType RegionType;
00496     typedef typename ROIType::SpacingType SpacingType;
00497 
00498     typedef typename ROIType::SpatialObjectType SpatialObjectType;
00499     typedef typename ROIType::SpatialObjectPointer SpatialObjectPointer;
00500 
00501 
00503     itkTypeMacro( ROICoupleBase, itk::LightObject );
00504     itkNewMacro(Self);
00505 
00506     static unsigned int GetImageDimension()
00507     {
00508         return ROIType::GetImageDimension();
00509     };
00510 
00511     const ROIType& Fixed() const
00512     {
00513         return *m_smpFixedROI;
00514     };
00515     ROIType& Fixed()
00516     {
00517         return *m_smpFixedROI;
00518     };
00519 
00520     const ROIType& Moving() const
00521     {
00522         return *m_smpMovingROI;
00523     };
00524     ROIType& Moving()
00525     {
00526         return *m_smpMovingROI;
00527     };
00528 
00529 
00530 protected:
00531     ROICoupleBase()
00532     {
00533         m_smpFixedROI = ROIType::New();
00534         m_smpMovingROI = ROIType::New();
00535         m_smpMovingROI->Reset();
00536         m_smpMovingROI->Reset();
00537     };
00538 
00539     virtual ~ROICoupleBase()
00540     {}
00541     ;
00542 
00543 private:
00544     ROIPointer m_smpFixedROI;
00545     ROIPointer m_smpMovingROI;
00546 }
00547 ; //class ROICoupleBase
00548 
00558 template<unsigned int VImageDimension>
00559 class ImageROICoupleBase : public ROICoupleBase< ImageROIBase<VImageDimension> >
00560 {
00561 public:
00563     typedef ImageROICoupleBase  Self;
00564     typedef ROICoupleBase< ImageROIBase<VImageDimension> > Superclass;
00565     typedef itk::SmartPointer<Self>        Pointer;
00566     typedef itk::SmartPointer<const Self>  ConstPointer;
00567 
00569     itkTypeMacro( ImageROICoupleBase, ROICoupleBase );
00570     itkNewMacro(Self);
00571 
00572 protected:
00573     ImageROICoupleBase()
00574     {  }
00575     ;
00576 
00577     virtual ~ImageROICoupleBase()
00578     {  }
00579     ;
00580 }
00581 ; //class ImageROICoupleBase
00582 
00592 template<unsigned int VImageDimension>
00593 class ImageMaskedROICouple : public ROICoupleBase< ImageMaskedROI<VImageDimension> >
00594 {
00595 public:
00597     typedef ImageMaskedROICouple  Self;
00598     typedef ROICoupleBase< ImageMaskedROI<VImageDimension> > Superclass;
00599     typedef itk::SmartPointer<Self>        Pointer;
00600     typedef itk::SmartPointer<const Self>  ConstPointer;
00601 
00603     itkTypeMacro( ImageMaskedROICouple, ImageROICoupleBase );
00604     itkNewMacro(Self);
00605 
00606 protected:
00607     ImageMaskedROICouple()
00608     {  }
00609     ;
00610 
00611     virtual ~ImageMaskedROICouple()
00612     {  }
00613     ;
00614 }
00615 ; //class ImageMaskedROICouple
00616 
00617 }  // namespace free
00618 
00619 #endif

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