zxhproj v 2.2
zxhproj

zxhLibFunctions.h

00001 
00002 /*=========================================================================
00003 
00004   Program:   ZXH Registration Software
00005   Author:    Xiahai Zhuang
00006   Module:    $RCSfle: zxhLibFunctions.h    $
00007   Language:  C++
00008   Date:      $Date: From  2004-01 $
00009   Version:   $Revision: 1.0, 2.0 $
00010 
00011 =========================================================================*/
00012 #ifndef zxhLibFunctions_h
00013 #define zxhLibFunctions_h
00014 
00015 #include <iostream>
00016 #include <fstream>
00017 #include <math.h>
00018 
00019 #ifdef HAS_VTK
00020 #include "vtkImageData.h"
00021 #include "vtkImageThreshold.h"
00022 #include "vtkImageEuclideanDistance.h"
00023 #include "vtkImageProgressIterator.h"
00024 #endif
00025 
00026 #include "zxh.h"
00027 #include "zxhImageData.h"
00028 #include "zxhImageGipl.h"
00029 //#include "zxhImageModelingBase.h"
00030 
00031 namespace zxh
00032 {
00033 #ifdef HAS_VTK
00034 
00036 template <class pixeltype, class vtktype>
00037 bool    ConvertToVtkData( zxhImageDataT<pixeltype> * pInImage,
00038                          vtkImageData * pOutImage, int vtkDataType, vtktype type)
00039 {
00040     if( pOutImage == 0 )
00041         pOutImage = vtkImageData::New() ;
00042     int size[] = {1,1,1,1} ;
00043     float sp[] = {1,1,1,1} ;
00044     pInImage->GetImageSize( size[0], size[1], size[2], size[3] );
00045     pInImage->GetImageSpacing( sp[0], sp[1], sp[2], sp[3] ) ;
00047     double spacing[ImageDimensionMax] = { sp[0], sp[1], sp[2], sp[3] };
00048     pOutImage->SetDimensions( size );
00049     int ext[ImageDimensionMax*2] = { 0,size[0]-1, 0,size[1]-1, 0,size[2]-1, 0,size[3]-1 } ;
00050     pOutImage->SetExtent( ext );
00051     double org[]={0,0,0,0} ;
00052     pOutImage->SetOrigin( org );
00053     pOutImage->SetSpacing( spacing ) ;
00054     pOutImage->SetNumberOfScalarComponents( 1 ) ;
00055 
00056     switch ( vtkDataType )
00057     {
00058     case GIPL_CHAR:
00059         pOutImage->SetScalarTypeToChar();
00060         break;
00061     case GIPL_U_CHAR:
00062         pOutImage->SetScalarTypeToUnsignedChar();
00063         break;
00064     case GIPL_SHORT:
00065         pOutImage->SetScalarTypeToShort();
00066         break;
00067     case GIPL_U_SHORT:
00068         pOutImage->SetScalarTypeToUnsignedShort();
00069         break;
00070     case GIPL_FLOAT:
00071         pOutImage->SetScalarTypeToFloat();
00072         break;
00073     case GIPL_U_INT:
00074         pOutImage->SetScalarTypeToUnsignedInt();
00075         break;
00076     case GIPL_INT:
00077         pOutImage->SetScalarTypeToInt();
00078         break;
00079     case GIPL_DOUBLE:
00080         pOutImage->SetScalarTypeToDouble();
00081         break;
00082     default:
00083         std::cerr<<"warning: unknown data type set to float\n " ;
00084         pOutImage->SetScalarTypeToFloat();
00085         break;
00086     }
00087     pOutImage->Update() ;
00088     for(int t = ext[6]; t <= ext[7]; t++)
00089     for(int z = ext[4]; z <= ext[5]; z++)
00090     {
00091         for(int y = ext[2]; y <= ext[3]; y++)
00092         {
00093             for(int x = ext[0]; x <= ext[1]; x++)
00094             {
00095                 int coo[]={x,y,z,t};
00096                 vtktype* pFpixel = static_cast<vtktype*>( pOutImage->GetScalarPointer( coo ) ) ;
00097                 *pFpixel = static_cast<vtktype>( pInImage->GetPixelGreyscale( x,y,z,t ) ) ;
00098             }
00099         }
00100     }
00101     return true ;
00102 };
00104 template <class pixeltype, class vtktype>
00105 bool    VtkDataConvertTo( vtkImageData * pInImage, vtktype type,
00106                           zxhImageDataT<pixeltype>* pOutImage, int dimension )
00107 {
00108     if( pOutImage == 0 )
00109         pOutImage = new zxhImageDataT<pixeltype>() ;
00110     int vtksize[ImageDimensionMax] = {1,1,1,1} ;
00111     double vtkspacing[ImageDimensionMax] = {1,1,1,1} ;
00112     pInImage->GetDimensions( vtksize );
00113     pInImage->GetSpacing( vtkspacing ) ;
00114 
00115     int size[ImageDimensionMax] = {vtksize[0], vtksize[1], vtksize[2], vtksize[3] } ;
00116     float spacing[ImageDimensionMax] = {vtkspacing[0], vtkspacing[1], vtkspacing[2], vtkspacing[3] } ;
00117     zxhImageInfo imageinfo ;
00118     pOutImage->NewImage( dimension, size, spacing, &imageinfo ) ;
00119 
00120     int *ext = pInImage->GetExtent();
00121     //for(int t = ext[6]; t <= ext[7]; t++)
00122     int t=0; //only for 3D
00123     for(int z = ext[4]; z <= ext[5]; z++)
00124     {
00125         for(int y = ext[2]; y <= ext[3]; y++)
00126         {
00127             for(int x = ext[0]; x <= ext[1]; x++)
00128             {
00129                 int coo[]={x,y,z,t} ;
00130                 vtktype* pFpixel = static_cast<vtktype*>( pInImage->GetScalarPointer( coo ) ) ;
00131                 pOutImage->SetPixelByGreyscale( x,y,z,t, static_cast<pixeltype>( *pFpixel ) ) ;
00132             }
00133         }
00134     }
00135     return true ;
00136 };
00137 
00138 #endif
00139 bool    vtkEuclideanDistanceTransform(
00140                       zxhImageData      * pImage,
00141                       zxhImageDataT<float>* pDistance,
00142                       int   iForegroundFrom, int    iForegroundTo, // inclusive
00143                       bool  OnForeground = true,
00144                       bool  ReturnSquare = false ) ;
00145 
00146 
00147 void    ComputeNormalVectorUsingDistanceTransform( zxhImageData * pImageRegion,
00148                                                   zxhImageDataT<float>*pNV );
00149 
00150 
00151 }//endof namespace zxh
00152 
00153 #endif //zxhLibFunctions_h
00154 
00155 
00156 
 All Classes Namespaces Functions Variables Typedefs