[vtkusers] VtkImageData 3D setScalar and save to nifti not work

Nicolas loiseau_nicolas at hotmail.fr
Fri Mar 8 09:51:18 EST 2019


Hello everyone, sorry for my english i'm a french user.

I have a problem with SetScalarComponentFromDouble in vtkImageData, when I
save a vtlImage in png file with dimensions x,y,0 that work nice, 
 imageData->SetScalarComponentFromDouble(x,y,0,0, 128) give me a gray png.

But when I want to add a z dimension and it to nifti file there is no color,
only black on .nii file.
imageData->SetScalarComponentFromDouble(x,y,z,0, 128);//R

I put my code here : 

#include <vtkSmartPointer.h>
#include <vtkDoubleArray.h>
#include <vtkImageData.h>
#include <vtkPointData.h>
#include <vtkPNGWriter.h>
#include <vtkImageCast.h>
#include <vtkNIFTIImageWriter.h>

int main(int, char *[])
{
  // Create an image data
  vtkSmartPointer<vtkImageData> imageData =
vtkSmartPointer<vtkImageData>::New();

  // Specify the size of the image data
  imageData->SetDimensions(120,121,122);
  imageData->AllocateScalars(VTK_UNSIGNED_CHAR,1);

  int* dims = imageData->GetDimensions();
  std::cout << "Dims: " << " x: " << dims[0] << " y: " << dims[1] << " z: "
<< dims[2] << std::endl;

  for (int x = 0; x < dims[0]; x++)
  {
    for (int y = 0; y < dims[1]; y++)
    {
      for (int z = 0; z < dims[2]; z++)
      {
        // double* pixel =
static_cast<double*>(imageData->GetScalarPointer(x,y,0));
        // std::cout << pixel[0] << "  ";
        // pixel[0] = 128;
        // pixel[1] = 0;
        imageData->SetScalarComponentFromDouble(x,y,z,0, 128);//R

      }
    }
  }


  vtkSmartPointer<vtkNIFTIImageWriter> writer =
vtkSmartPointer<vtkNIFTIImageWriter>::New();
  writer->SetFileName("label_7235_test.nii.gz");
  writer->SetInputData(imageData);
  writer->Write();

  // This should also work:
  // min = vtkImageData->GetOutput()->GetScalarRange()[0] and max =
GetScalarRange()[1].
  return EXIT_SUCCESS;
}




--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html


More information about the vtkusers mailing list