Hi,
I am testing loading a series of rgb images as a volume using the ImageStacksModule, cropped them with an ROI, and export them as a new series of rgb images in the format of tiff or png.
An example image is here:
So far, I can successfully export a slice of image from a volume by first extracting the numpy array of an image from a vector volume. I then use this numpy array to create a new vector volume node vectorVolume
that only contains one image.
After that, I can use vectorVolume.GetImageData()
to extract the vtkImageData
of the vectorVolume
, which now contains only 1 image. This allows me export it using vtkTIFFWriter()
or vtkPNGWriter()
, which can only take the vtkImageData
object.
However, for every image in the original volume, I have to create a new volume with just one image to export it.
Therefore, I’m also thinking about directly output each image from the vtkImageData of the volume acquired from vectorVolumeNode.GetImageData() so that I don’t have to build a new volume for each image.
I could not find a way to do it. For an experiment, I tried to directly pass the numpy array of an image into vtkImageData object following the example here: How to convert 3D numpy array to vtk and save the .vtk file? - #3 by user4
image = imageArray[0, :, :, :] #Get the first image array from slicer.util.arrayFromVolume(vectorVolume)
shape = image.shape
vtype = vtk.util.numpy_support.get_vtk_array_type(image.dtype)
flat_img_array = image.flatten()
vtk_arr = vtk.util.numpy_support.numpy_to_vtk(num_array=flat_img_array, deep=True, array_type=vtype)
imgVTK = vtk.vtkImageData()
imgVTK.GetPointData().SetScalars(vtk_arr)
imgVTK.SetDimensions(shape)
I then exported the imgVTK as PNG:
w_png = vtk.vtkPNGWriter()
w_png.SetInputData(imgVTK)
w_png.GetInput()
w_png.SetFileName('tmp/test.png')
w_png.Write()
However, all I got is an image with pixels re-arranged.
Is there any way I can directly extract each image from vectorVolumeNode.GetImageData()
as a single vtkImageData
object and export it using vtkTIFFWriter
without building a new volume for each image?
Thank you!