Hello everyone,

I work on a project and I have a problem.

I need to save an image 2d in JPEG or PNG.

The problem is :

- I have a 3d matrix
- I put the numbers of this matrix in a vtkImage (so this vtkImage is in 3d format)

=> I want to transform a slice of the 3d matrix of floats in 2d jpeg/png image directly.

=> I can do also transform the entire 3d matrix of floats into vtkImage 3d and then extract a 2d slice from the vtkImage into 2d jpeg but it’s going to be heavy because after I need to do that for each iteration

But now, I just want to test for one iteration.

I searched but I don’t know how to do… I read PNGWriter and JPEGWriter but it doesn’t work for my image :

m_vtkImage = vtkImageData::New();

m_vtkImage->Initialize();

m_vtkImage->SetDimensions(ext_dim, ext_dim, ext_dim);

m_vtkImage->SetSpacing(vtkspacing );

m_vtkImage->SetOrigin(vtkorigin);

m_vtkImage->AllocateScalars(VTK_FLOAT,1);

and in my triple loop for : (because 3d matrix)

m_vtkImage->SetScalarComponentFromFloat ( i,j,k,0,(p3D_2[i][j][k]) );

This is not my code (I work with teachers on this project) and it works and create an image.

Now, I want this image : m_vtkImage and take a slice of this image and transform that on JPEG/PNG

If we look the JPEGWriter , here the link : https://lorensen.github.io/VTKExamples/site/Cxx/IO/JPEGWriter/

We can see this :

vtkSmartPointer writer =

vtkSmartPointer::New();

writer->SetFileName( outputFilename.c_str() );

writer->SetInputConnection( imageSource->GetOutputPort() );

writer->Write();

and before that :

vtkSmartPointer imageSource =

vtkSmartPointer::New();

imageSource->SetExtent( extent );

imageSource->SetScalarTypeToUnsignedChar(); // vtkJPEGWriter only accepts unsigned char input

imageSource->SetNumberOfScalarComponents( 3 );

Naively I tested this code with my variable but it’s doesnt work on the line : writer->SetInputConnection( imageSource->GetOutputPort() );

I am new with VTK so I don’t understand very well the SetInputConnection and GetOutputPort, what type of variable I need to give etc.

I think I need to transform my image in 2d and then I can put it in the SetInputConnection right ?

In all cases, I don’t know if vtk propose a function to take a 2d slice of an image 3d.

Thanks for your help !

AmBou99