Isosurface Generation using VTK




VTK Basics vtkImageReader Functions


In order to successfully read the file, the following functions should be called:


  • void SetFileName( const char * )

·        Use this function to specify the file to be read by the class.  As a side note, it is important to be careful to make your code portable across operating systems since Windows and Linux systems do not use the same directory naming structure and to make sure that the file actually exists.  For the case study in this tutorial, the file can be “hard-coded”; however, typically a programmer should “normalize” the path and check if the path exists.  Below is code that could be used in conjunction with SetFileName:





strOriginalFilename = './Female.raw'

strNormalizedPath = os.path.normpath(strOriginalFilename)


if (os.path.exists(strNormalizedPath)== 1):

   virReader = vtk.vtkImageReader()




Example 3.1: Normalizing a file path and checking for the existence of a file


·        void SetDataScalarType( int type )

·        One can set the datatype of each element in the file using this function.  With most image files, data types are typically an unsigned character or an unsigned short.  Remember, characters are 8-bit, and shorts are 16-bit.  For this tutorial, type would be vtk.VTK_UNSIGNED_SHORT since the file consists of 16-bit pixels.  Alternatively, instead of using this general function, one could instead use: SetDataScalarTypeToUnsignedShort().


·         void SetNumberOfScalarComponents( int )

·        This function specifies how many components make up a single pixel (traditional definition— picture element).  Typically there are two values that one would want to give for the parameter.  If the image is grayscale, specify 1 for the components.  If the image is in RGB, specify 3 for the components.


·         void SetDataByteOrderToLittleEndian()

·        This function forces the data to be read in as little endian format.  This is the ordering used by Windows PCs.  If the file was created in Linux, the opposite function would be called:  SetDataByteOrderToBigEndian().  Alternatively, there is a function where you can specify a constant instead of giving a very specific function.  The call for this function is: SetDataByteOrder( int ).  This call may not be necessary.


·         void SetFileDimensionality( int )

·        In order to specify whether the file is two-dimensional or three-dimensional, use this function with the parameter 2 or 3, respectively.  For this tutorial, the file contains volumetric data, so use 3 here.


·         void SetDataExtent( int,int, int,int, int,int )

·        This function specifies the dimensions of the file.  The way that the function’s parameter list appears, it is necessary to give the range of pixels beginning with 0.  The first pair of parameters represent the x-dimension, the second pair is for the y-dimension, and the third pair is for the z-dimension.  It should be noted that if it was desirable to extract some portion of the image, one would use another function instead of this function.  However it may be possible to simply do an extraction from the larger file by giving extents within their respective minimum and maximum values (the author does not know).  Regardless of whether it is a volumetric file or not, all six parameters must be used even if they are specified by 0,0.


·         void SetDataSpacing( float, float, float )

·        This function indicates the data spacing between each component.  Remember that the data type and number of scalar components will affect how parameters for this function are specified.  In most cases, it will be safe to have a spacing of 1 for all axes.


·         vtkImageData * GetOutput()

·        This function returns the output from this VTK object as the type vtkImageData.  Although it does not seem relevant now, it will be useful to remember that the superclass of vtkImageData is vtkDataSet because further down a pipeline, which will have this class as its source, it will be necessary to get the output of this class using this function to set the input of another object, namely vtkExtractVOI.



<<< Previous



Table of Contents


Next >>>

Filter Objects