VtkImage 3d to 2d

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->SetDimensions(ext_dim, ext_dim, ext_dim);
m_vtkImage->SetSpacing(vtkspacing );
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 =
writer->SetFileName( outputFilename.c_str() );
writer->SetInputConnection( imageSource->GetOutputPort() );

and before that :

vtkSmartPointer imageSource =
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 !


Would you like to save slices of a 3D volume as 2D images so that you can use them train a neural network?

Not to train a neural network.
I want to save slice of a 3D volume as 2D images to program a small feature that will create a video when the program is executed.
This video will be composed of a sequence of images (2D images so) which will allow us to see the evolution of the burn of a tumor.

Thanks for your answer !

You can use vtkImageReslice filter to extract an arbitrary 2D slice from a volume.

Note that there are VTK-based application frameworks that already provide basic features, such as extracting slices and recording/replaying data. They are built so that you can easily customize it (show only features that you need) and extend it. For medical applications, check out 3D Slicer and MITK - they provide many more features that are useful for building an advanced ablation monitoring system. See www.slicerigt.org for a few examples of how 3D Slicer can be customized to build image-guided therapy systems that are successfully used in the operating room.

Thanks for your answer I’m going to check that.

But now if I take a 2D slice of a 3D volume and then I save this slice in JPEG/PNG, it is alright with one vtk image.
In my case, I’m going to have 600 vtk image per sec during 30min so for one minute I’m going to create 600 vtk image and it’s heavy I think.

Doesn’t exist a function or a library that takes a slice of a 3D matrix and transform directly in 2D JPEG/PNG, without create a vtk image each iteration ?
Because each iteration the 3D matrix change so if each iteration I create a vtk image it’s heavy I think.

Thanks for your answer

600 images per second is extremely high frame rate. What imaging modality provides you this? How large is one frame?

Compression takes time, so for high-speed imaging applications you would not use JPEG or PNG, but you would dump the raw data to memory or disk as fast as you can and would take care of compression later (or in background threads).