cmtkScalarImage.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2010 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2011 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2752 $
00026 //
00027 //  $LastChangedDate: 2011-01-17 11:33:31 -0800 (Mon, 17 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkScalarImage_h_included_
00034 #define __cmtkScalarImage_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <Base/cmtkMacros.h>
00039 #include <Base/cmtkTypes.h>
00040 #include <Base/cmtkTypedArray.h>
00041 #include <Base/cmtkRegion.h>
00042 #include <Base/cmtkFixedVector.h>
00043 #include <Base/cmtkMatrix3x3.h>
00044 #include <Base/cmtkInterpolator.h>
00045 
00046 #include <cassert>
00047 #include <vector>
00048 
00049 #include <System/cmtkSmartPtr.h>
00050 
00051 namespace
00052 cmtk
00053 {
00054 
00057 
00060 class ScalarImage
00061 {
00063   cmtkGetSetMacro(int,NumberOfFrames);
00064 
00066   cmtkGetSetMacro(TypedArray::SmartPtr,PixelData);
00067 
00069   cmtkGetSetMacro2Array(Types::Coordinate,PixelSize);
00070 
00072   cmtkGetSetMacro(Types::Coordinate,FrameToFrameSpacing);
00073 
00074 public:
00076   typedef ScalarImage Self;
00077 
00079   typedef SmartPointer<Self> SmartPtr;
00080 
00082   typedef SmartConstPointer<Self> SmartConstPtr;
00083 
00085   typedef Region<2,int> RegionType;
00086 
00088   typedef RegionType::IndexType IndexType;
00089 
00091   typedef FixedVector<3,Types::Coordinate> SpaceVectorType;
00092 
00094   ScalarImage();
00095 
00097   ScalarImage( const int dimsx, const int dimsy, const int numberOfFrames = 1 );
00098 
00110   ScalarImage( const ScalarImage& source, const int* roiFrom = NULL, const int* roiTo = NULL );
00111 
00114   ScalarImage( const ScalarImage& other, const Self::RegionType& roi );  
00115 
00117   virtual ~ScalarImage() {}
00118 
00120   virtual void SetDims( const Self::IndexType& dims )
00121   {
00122     this->m_Dims = dims;
00123   }
00124 
00126   const Self::IndexType GetDims() const
00127   {
00128     return this->m_Dims;
00129   }
00130 
00133   virtual ScalarImage* InterpolateFrom
00134   ( const ScalarImage* grid, const CoordinateMatrix3x3* matrix, const cmtk::Interpolators::InterpolationEnum interpolation = cmtk::Interpolators::LINEAR )
00135     const;
00136 
00138   void SetROI( const Self::RegionType& roi ) 
00139   {
00140     ROI = roi;
00141     HasROI = true;
00142   }
00143 
00145   void UnsetROI() 
00146   {
00147     HasROI = false;
00148   }
00149 
00151   virtual ScalarImage* GetCropped() const 
00152   { 
00153     if ( HasROI )
00154       return new ScalarImage( *this, ROI );
00155     else
00156       return new ScalarImage( *this );
00157   }
00158   
00160   void CreatePixelData( const ScalarDataType dtype ) 
00161   {
00162     this->m_PixelData = TypedArray::SmartPtr( TypedArray::Create( dtype, this->m_Dims[0] * this->m_Dims[1] * this->m_NumberOfFrames ) );
00163   }
00164 
00167   Self::SpaceVectorType m_ImageOrigin;
00168 
00170   void SetImageOrigin( const Self::SpaceVectorType& imageOrigin ) 
00171   {
00172     this->m_ImageOrigin = imageOrigin;
00173   }
00174 
00176   Self::SpaceVectorType GetImageOrigin( const int frame = 0 ) const;
00177 
00180   cmtkGetSetMacro(Self::SpaceVectorType,ImageDirectionX);
00181 
00184   cmtkGetSetMacro(Self::SpaceVectorType,ImageDirectionY);
00185 
00189   cmtkGetSetMacro(Types::Coordinate,ImageSlicePosition);
00190 
00195   cmtkGetSetMacro(Types::Coordinate,ImageTiltAngle);
00196 
00198   int GetNumberOfPixels() const 
00199   { 
00200     return this->m_Dims[0] * this->m_Dims[1];
00201   }
00202 
00204   Types::DataItem GetPixelAt( const int i, const int j ) const 
00205   {
00206     Types::DataItem value;
00207     if ( this->m_PixelData->Get( value, i + this->m_Dims[0] * j ) ) 
00208       return value;
00209     return 0;
00210   }
00211 
00216   bool GetPixelAt( Types::DataItem& value, const Types::Coordinate i, const Types::Coordinate j ) const;
00217 
00222   bool GetPixelAtCubic( Types::DataItem& value, const Types::Coordinate i, const Types::Coordinate j ) const;
00223 
00225   void SetPixelAt( const int i, const int j, const Types::DataItem data ) 
00226   {
00227     this->m_PixelData->Set( data, i + this->m_Dims[0] * j );
00228   }
00229 
00231   virtual ScalarImage* Clone() const;
00232 
00242   virtual ScalarImage* Clone( const bool clonePixelData );
00243 
00245   ScalarImage* Downsample( const int factorX, int factorY = 0, ScalarImage *const target = NULL ) const;
00246 
00253   virtual void ApplyBinaryMask( const ScalarImage* maskImage, const bool invert = false );
00254 
00269   TypedArray::SmartPtr GetMedianFiltered( const byte range ) const;
00270 
00272   ScalarImage* ApplyMedianFilter( const byte range ) 
00273   {
00274     this->SetPixelData( TypedArray::SmartPtr( this->GetMedianFiltered( range ) ) );
00275     return this;
00276   }
00277   
00283   TypedArray::SmartPtr GetGaussFiltered( const Types::Coordinate stdDev ) const;
00284 
00286   ScalarImage* ApplyGaussFilter( const Types::Coordinate stdDev ) 
00287   {
00288     this->SetPixelData( TypedArray::SmartPtr( this->GetGaussFiltered( stdDev ) ) );
00289     return this;
00290   }
00291 
00297   TypedArray::SmartPtr GetSobel2DFiltered() const;
00298 
00300   ScalarImage* ApplySobel2DFilter() 
00301   {
00302     this->SetPixelData( TypedArray::SmartPtr( this->GetSobel2DFiltered() ) );
00303     return this;
00304   }
00305 
00309   TypedArray::SmartPtr GetLaplace2DFiltered() const;
00310 
00312   ScalarImage* ApplyLaplace2DFilter() 
00313   {
00314     this->SetPixelData( TypedArray::SmartPtr( this->GetLaplace2DFiltered() ) );
00315     return this;
00316   }
00317   
00321   TypedArray::SmartPtr GetSobelFiltered( const bool horizontal, const bool absolute = false ) const;
00322   
00324   ScalarImage* ApplySobelFilter( const bool horizontal, const bool absolute = false ) 
00325   {
00326     this->SetPixelData( TypedArray::SmartPtr( this->GetSobelFiltered( horizontal, absolute ) ) );
00327     return this;
00328   }
00330 
00332   void Mirror( const bool horizontal, const bool vertical );
00333 
00335   void AdjustAspect( const bool interpolate = false );
00336 
00338   void AdjustToIsotropic( const Types::Coordinate pixelSize, const bool interpolate = false );
00339 
00345   void ProjectPixel( const Self::SpaceVectorType& v, int& i, int& j ) const;
00346   
00348   ScalarImage& operator-=( const ScalarImage& );
00349 
00351   virtual void Print() const;
00352 
00353 private:
00355   Self::IndexType m_Dims;
00356 
00358   Self::RegionType ROI;
00359 
00361   bool HasROI;
00362 
00363   // Return filtered image data using client-provided symmetric kernels.
00364   TypedArray::SmartPtr GetFilteredData( const std::vector<Types::DataItem>& filterX, const std::vector<Types::DataItem>& filterY ) const;
00365 
00367   void AdjustAspectY( const bool interpolate = false );
00368 
00370   void AdjustAspectX( const bool interpolate = false );
00371 
00373   friend ScalarImage* operator- ( const ScalarImage&, const ScalarImage& );
00374 };
00375 
00377 
00378 #define CMTK_SCALARIMAGE_NOCLONEDATA false
00379 
00380 #define CMTK_SCALARIMAGE_CLONEDATA true
00381 
00382 #define CMTK_SCALARIMAGE_HORIZONTAL true
00383 
00384 #define CMTK_SCALARIMAGE_VERTICAL false
00385 
00386 #define CMTK_SCALARIMAGE_ABSOLUTE true
00387 
00388 #define CMTK_SCALARIMAGE_SIGNED false
00389 
00391 
00393 
00394 } // namespace cmtk
00395 
00396 #endif // #ifndef __cmtkScalarImage_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines