[vtkusers] A Brief Guide on ---- How to create an image from scratch in VTK

Deepak Roy cdeepakroy at yahoo.com
Fri Dec 9 13:47:12 EST 2005


  hello everyone, 
   
  Here is a brief guide on creating an image fro scratch in memory in VTK.
   
  It wasnt obvious to me at the first look, but finally it dint take much time to figure it out....
   
  I am sure there would be many other ways to do this.............But here is the way i did it......... 
   
  If anyone knows any alternative ways, please to add them to this thread. I know you could also do it with vtkImageCache and vtkStrcuturePoints.
   
  Here is how i solved the problem:
   
  vtkImageData is the basic data representation form for images in VTK.
   
  So if you want to create an image from scratch, you create an object of the vtkImageData.
   
  Set the image parameters first as shown in the code below. For example, If you want an RGB image set the number of scalar components to be 3.
   
  Then call allocate scalars which allocates the scalars of the PointData of vtkImageData is also derived from vtkDataSet it inherits the basic structure of a VTK data set. 
   
  Then you set the scalar values as needed.
   
  Below is a small snippet of code that does it. 
   
  **************************************************************************************************
   
  vtkImageData *pZImage = vtkImageData::New();
  pZImage->SetExtent( 0 , wx , 0 , wy , 0 , 0 );
  pZImage->SetOrigin( 0.0 , 0.0 , 0.0 );
  pZImage->SetSpacing( 1.0 , 1.0 , 1.0 );
  pZImage->SetScalarTypeToUnsignedChar();
  pZImage->SetNumberOfScalarComponents( 1 );
  pZImage->AllocateScalars();
   
  for( int i = 0 ; i < wx ; i++ )
  {
    for( int j = 0 ; j < wy ; j++ )
  {
    int index = i * j;
   
  // get scalar pointer to current pixel
  unsigned char *zPtr = (unsigned char *) pZImage->GetScalarPointer( i , j , 0 );
   
  // set scalar value accordingly
  *zPtr = 128;  
   
  } 
  }
   
  *************************************************************************************************
   
  Regards,
   
  Deepak Roy



			
---------------------------------
Yahoo! Shopping
 Find Great Deals on Holiday Gifts at Yahoo! Shopping 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20051209/2dabe9d6/attachment.htm>


More information about the vtkusers mailing list