[vtkusers] add a scalar to RGB values of a .vtk file

shirani kannangara mangalika673 at gmail.com
Tue Jan 15 01:33:36 EST 2013


HI,
I want to add a scalar value to RGB values of a .vtk file.(Here I have
written VHP abdomen data(.mhd) set in .vtk format using ITK andVTK and it
is working properly). My next attempt is to add a scalar to RGB values of
the .vtk file and use imageIterator to sweep data.For that I have used VTK
Examples CXX

VTK/Examples/CXX/Broken/IO/ImageReader2

VTK/Examples/CXX/ImageData/IterateImage Data


My codes are as follows

#include <vtkSmartPointer.h>
#include <vtkImageReader2.h>
#include <vtkImageData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkImageActor.h>
#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkImageData.h>

int main(int argc, char *argv[])
{
  // Verify command line arguments
  /*if(argc < 2)
    {
    std::cout << "Usage: " << argv[0]
              << " InputFilename" << std::endl;
    return EXIT_FAILURE;
    }*/
  // Parse command line arguments
  //std::string inputFilename =("D:\\rgbabdomen\\Debug\\output.vtk");

  // Read file
  vtkSmartPointer<vtkImageReader2> reader =
    vtkSmartPointer<vtkImageReader2>::New();
  reader->SetFileName(("D:\\rgbabdomen\\Debug\\output.vtk"));
  reader->SetDataScalarTypeToUnsignedChar();
  reader->Update();

//int main(int, char *[])

  // Create an image data
  vtkSmartPointer<vtkImageData> imageData =
    vtkSmartPointer<vtkImageData>::New();

  imageData->reader(GetOutput);




  // Specify the size of the image data
  imageData->SetDimensions(675,401,450);
#if VTK_MAJOR_VERSION <= 5
  imageData->SetNumberOfScalarComponents(GetNumberOfScalars+1);
  imageData->SetScalarTypeToInt();


#else
  imageData->AllocateScalars(VTK_DOUBLE,1);
#endif

  int* dims = imageData->GetDimensions();
  // int dims[3]; // can't do this

  std::cout << "Dims: " << " x: " << dims[0] << " y: " << dims[1] << " z: "
<< dims[2] << std::endl;

  //std::cout << "Number of points: " << imageData->GetNumberOfPoints() <<
std::endl;
  std::cout << "Number of cells: " << imageData->GetNumberOfCells() <<
std::endl;

  // Fill every entry of the image data with "3.0"
  for (int z = 0; z < dims[2]; z++)
    {
    for (int y = 0; y < dims[1]; y++)
      {
      for (int x = 0; x < dims[0]; x++)
        {
        double* pixel =
static_cast<double*>(imageData->GetScalarPointer(x,y,z));
        pixel[0] = 3.0;
        }
      }
    }

  // Retrieve the entries from the image data and print them to the screen
  for (int z = 0; z < dims[2]; z++)
    {
    for (int y = 0; y < dims[1]; y++)
      {
      for (int x = 0; x < dims[0]; x++)
        {
        double* pixel =
static_cast<double*>(imageData->GetScalarPointer(x,y,z));
        // do something with v
        std::cout << pixel[0] << " ";
        }
      std::cout << std::endl;
      }
    std::cout << std::endl;
    }

  // Access the data linearly
  /*
    vtkSmartPointer<vtkImageData> image =
    vtkSmartPointer<vtkImageData>::New();
  image->SetExtent(0,1,0,1,0,0);
  image->SetScalarTypeToInt();
  image->SetNumberOfScalarComponents(1);

  int* pixel;

  pixel = static_cast<int*>(image->GetScalarPointer(0,0,0));
  pixel[0] = 1;

  pixel = static_cast<int*>(image->GetScalarPointer(1,0,0));
  pixel[0] = 2;

  pixel = static_cast<int*>(image->GetScalarPointer(0,1,0));
  pixel[0] = 3;

  pixel = static_cast<int*>(image->GetScalarPointer(1,1,0));
  pixel[0] = 4;

  vtkIntArray* scalars =
vtkIntArray::SafeDownCast(image->GetPointData()->GetArray("ImageScalars"));
  std::cout << "Scalars has " << scalars->GetNumberOfComponents() << "
components" << std::endl;
  std::cout << "Scalars has " << scalars->GetNumberOfTuples() << " tuples"
<< std::endl;

  for(vtkIdType i = 0; i < scalars->GetNumberOfTuples(); i++)
    {
    std::cout << scalars->GetValue(i) << std::endl;
    }
  */
  return EXIT_SUCCESS;
}



  /*
// Create an actor
  vtkSmartPointer<vtkImageActor> actor =
    vtkSmartPointer<vtkImageActor>::New();
  actor->SetInput(reader->GetOutput());

  // Setup renderer
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(actor);
  renderer->ResetCamera();

  // Setup render window
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  // Setup render window interactor
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  vtkSmartPointer<vtkInteractorStyleImage> style =
    vtkSmartPointer<vtkInteractorStyleImage>::New();

  renderWindowInteractor->SetInteractorStyle(style);

  // Render and start interaction
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindowInteractor->Initialize();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}*/

There are three errors
1>------ Build started: Project: ImageReader2, Configuration: Debug Win32
------
1>Compiling...
1>imagereader.cxx
1>.\imagereader.cxx(40) : error C2039: 'reader' : is not a member of
'vtkImageData'
1>        D:\research2\vtk-5.10.0\VTK\Filtering\vtkImageData.h(35) : see
declaration of 'vtkImageData'
1>.\imagereader.cxx(40) : error C2065: 'GetOutput' : undeclared identifier
1>.\imagereader.cxx(48) : error C2065: 'GetNumberOfScalars' : undeclared
identifier
1>Build log was saved at
"file://d:\dup2\ImageReader2.dir\Debug\BuildLog.htm"
1>ImageReader2 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========

CCan you please go through my codes and show me the correct way of doing
it.I am waiting for  a quick reply from you.

regards
Shirani
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130115/1ecc22fe/attachment.htm>


More information about the vtkusers mailing list