About casting from vtkImageData* to vtkDataObject*

Hi.
I’m trying to write an simple example of vtk. I want to read nii file and directly write it to another file.
My code:

vtkSmartPointer < vtkNIFTIImageReader > NiftReader = vtkSmartPointer < vtkNIFTIImageReader > ::New();
NiftReader->SetFileName ( argv[1] ) ;
NiftReader->Update() ;
vtkSmartPointer < vtkNIFTIImageWriter > Niftwriter = vtkSmartPointer < vtkNIFTIImageWriter > ::New();
Niftwriter->SetFileName ( argv[2] );
Niftwriter->AddInputData(NiftReader->GetOutput()) ;
Niftwriter->Write();

I don’t know why it always shows no known conversion between those types.
Function GetOutput() will return an vtkImageData*
Function AddInputData can accept anvtkDataObject*
Also, class vtkImageData is inherited from vtkDataObject
In addition, I can’t even use dynamic_cast like this auto ptr = dynamic_cast<vtkDataObject*> (NiftReader->GetOutput())

I don’t know why.

Hi @Sterben_01 ,

Should work out of the box without conversion. What is the error ?

Best,

It is something like this:

In function ‘int main(int, char**)’:
error: no matching function for call to ‘vtkNIFTIImageWriter::AddInputData(vtkImageData*)’
   52 |   NiftWriter->AddInputData (NiftReader->GetOutput()) ;
      |                                                    ^
In file included from /usr/include/vtk-6.3/vtkImageReader2.h:42,
                 from /usr/include/vtk-6.3/vtkNIFTIImageReader.h:36,
                 from /home/zhaoz22/Desktop/FINAL/src/Butterworth.cxx:2:
/usr/include/vtk-6.3/vtkImageAlgorithm.h:73:16: note: candidate: ‘virtual void vtkImageAlgorithm::AddInputData(vtkDataObject*)’
   73 |   virtual void AddInputData(vtkDataObject *);
      |                ^~~~~~~~~~~~
/usr/include/vtk-6.3/vtkImageAlgorithm.h:73:29: note:   no known conversion for argument 1 from ‘vtkImageData*’ to ‘vtkDataObject*’
   73 |   virtual void AddInputData(vtkDataObject *);
      |                             ^~~~~~~~~~~~~~~
/usr/include/vtk-6.3/vtkImageAlgorithm.h:74:16: note: candidate: ‘virtual void vtkImageAlgorithm::AddInputData(int, vtkDataObject*)’
   74 |   virtual void AddInputData(int, vtkDataObject*);
      |                ^~~~~~~~~~~~
/usr/include/vtk-6.3/vtkImageAlgorithm.h:74:16: note:   candidate expects 2 arguments, 1 provided
make[2]: *** [CMakeFiles/Butterworth.dir/build.make:63: CMakeFiles/Butterworth.dir/Butterworth.cxx.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/Butterworth.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

SetInputData instead of AddInputData.

1 Like

It doesn’t work. I think I might missing some include?

/home/zhaoz22/Desktop/FINAL/src/Butterworth.cxx: In function ‘int main(int, char**)’:
/home/zhaoz22/Desktop/FINAL/src/Butterworth.cxx:52:52: error: no matching function for call to ‘vtkNIFTIImageWriter::SetInputData(vtkImageData*)’
   52 |   NiftWriter->SetInputData (NiftReader->GetOutput()) ;
      |                                                    ^
In file included from /usr/include/vtk-6.3/vtkImageReader2.h:42,
                 from /usr/include/vtk-6.3/vtkNIFTIImageReader.h:36,
                 from /home/zhaoz22/Desktop/FINAL/src/Butterworth.cxx:2:
/usr/include/vtk-6.3/vtkImageAlgorithm.h:58:8: note: candidate: ‘void vtkImageAlgorithm::SetInputData(vtkDataObject*)’
   58 |   void SetInputData(vtkDataObject *);
      |        ^~~~~~~~~~~~
/usr/include/vtk-6.3/vtkImageAlgorithm.h:58:21: note:   no known conversion for argument 1 from ‘vtkImageData*’ to ‘vtkDataObject*’
   58 |   void SetInputData(vtkDataObject *);
      |                     ^~~~~~~~~~~~~~~
/usr/include/vtk-6.3/vtkImageAlgorithm.h:59:8: note: candidate: ‘void vtkImageAlgorithm::SetInputData(int, vtkDataObject*)’
   59 |   void SetInputData(int, vtkDataObject*);
      |        ^~~~~~~~~~~~
/usr/include/vtk-6.3/vtkImageAlgorithm.h:59:8: note:   candidate expects 2 arguments, 1 provided
make[2]: *** [CMakeFiles/Butterworth.dir/build.make:63: CMakeFiles/Butterworth.dir/Butterworth.cxx.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/Butterworth.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

I included following header files:

#include <iostream>
#include <vtkNIFTIImageReader.h>
#include <vtkNIFTIImageWriter.h>
#include <vtkSmartPointer.h>
#include <vtkImageReader.h>
#include <vtkImageWriter.h>

Indeed, you need #include <vtkImageData.h>

1 Like

everything works fine. Thank you!