convert itk (nifti file) to polydata image (.vtk) - python

I am working with MRI database with niftiformat (.nii) but I want to convert them to polydata images .vtk.
I used many different methods: like miconv, itksnap, I download vtk, itk, and itkvtkglue...
But no one give me a good results.
So please help me.

If I understand correctly nii files are volumetric data so you cannot just "convert" it to polygonal data unless you create an isosurface out of it, try:
from vtkplotter import load
vol = load('myfile.nii') #vtkVolume
vol.isosurface().show()
# or convert to vtk format:
vol.write('newfile.vti')

Related

How to save matplotlib plot to a hdf5 file?

im processing measured data and in a following process i create plots of them. (To be more specific its about to display orbits of a spinning shaft.) Thus i will create many plots and i want to safe them to a hdf5 file. The "workaround" i know is:
saving the file to a .jgp
reading it with opencv
writing that array to the hdf5
This works well, but will create a mess in my working dictionary.
Here is a Code example if someone wants to just safe one ore two plots:
fig.savefig(some_plot.jpg) # saves the plot to working discretionary as 'some_plot.jpg'
image = cv2.imread(some_plot.jpg) # reads the created .jpg as array
g = f.create_dataset(some_path, data=image) # creates dataset in the hdf5 and safes the
image array to it
# add !!important!! image attributes , i got these from another forum but if you convert a
# image in hdf5 and read the attributes you can create them by yourself
# futher information is in the hdf5 documentation (hdfgroup.org --> HDF5 Image and Palette
Specification)
g.attrs.create('CLASS', 'IMAGE', dtype='S6')
g.attrs.create('IMAGE_MINMAXRANGE', [0, 255], dtype=np.uint8)
g.attrs.create('IMAGE_SUBCLASS', 'IMAGE_TRUECOLOR', dtype='S16')
g.attrs.create('IMAGE_VERSION', '1.2', dtype='S4')
g.attrs.create('INTERLACE_MODE', 'INTERLACE_PIXEL', dtype='S16')
So now my problem is that this code creates a .jpg image for every plot, which will make a mess. Is there a way of converting the picture to a numpy arrray without the need of saving it as a image. (I could delete the picture.jpg after it got saved to the hdf5, but i want to avoid that.) matplotlib.pyplot has a inbuild imread()-function that i will try in the future to safe the use of opencv. I hope someone knows a solution to save plot images directly to hdf5.
Best regards
Marius

"imagio.imsave" vs "imageio.core.util.Array.tofile"

I am expanding my limited Python knowledge by converting some MATLAB image analysis code to Python. I am following Image manipulation and processing using Numpy and Scipy. The code in Section 2.6.1 saves an image using both imageio.imsave and face.tofile, where type(face)=<class 'imageio.core.util.Array>'.
I am trying to understand why there are two ways to export an image. I tried web-searching tofile, but got numpy.ndarray.tofile. It's very sparse, and doesn't seem to be specific to images. I also looked for imageio.core.util.Array.tofile, but wasn't able to find anything.
Why are there two ways to export files? And why does imageio.core.util.Array.tofile seem to be un-findable online?
The difference is in what the two functions write in the file.
imageio.imsave() saves a conventional image, like a picture or photo, in JPEG/PNG format that can be viewed with an image viewer like GIMP, feh, eog, Photoshop or MSxPaint.
tofile() saves in a Numpy-compatible format that only Numpy (and a small number of other Python tools) use.

3D visualization of .dicom files with ipyvolume

i'm trying to visualize a set of .dicom files using pydicom and ipyvolume.
I used pydicom to read files and then sorted them by their location and turned the slices into a 3D array. I could draw a 3D model of the data using ipyvolume.pylab.plot_isosurface() although I'm not sure if this is the right way of visualizing medical images (it's all solid pixels with the same opacity and color). I've also tried ipyvolume.pylab.volshow() but that did not work.
Is there a right way to visualize medical images with ipyvolume? or this is just not the right library for that?
DICOM file does not have 'voxel' data so you can't simply plot a dicom in 3D view. You should estimate voxel data using slices of a dicom series. after that, using a 3D modeling algorithm such as Marching Cubes you can extract final 3D model. Take a look at CTU.
I haven't used ipyvolume, but looking at the documentation it ought to be able to visualize DICOM image sets.
If you want to try another package, I use SimpleITK to load DICOM images and itkwidgets do volume visualization in a Jupyter notebook.
Here's a simple notebook that load a DICOM series and displays it:
import SimpleITK as sitk
import itkwidgets
# Get the DICOM file names in the current directory
names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames('.')
# Read the DICOM series
reader = sitk.ImageSeriesReader()
reader.SetFileNames(names)
img = reader.Execute()
itkwidgets.view(img)
If the directory has more than one DICOM series in it, GetGDCMSeriesFileNames has a seriesID parameter you can give it to specify which series to load.

Raw image Editing, Rotating and Saving back as Raw

I am working on bayer raw(.raw format) image domain where I need to edit the pixels according to my needs(applying affine matrix) and save them back .raw format.so There are two sub-problems.
I am able to edit pixels but can save them back as .raw
I am using a robust library called rawpy that allows me to read pixel values as numpy array, while I try to save them back I am unable to persist the value
rawImage = rawpy.imread('Filename.raw') // this gives a rawpy object
rawData = rawImage.raw_image //this gives pixels as numpy array
.
.//some manipulations performed on rawData, still a numpy array
.
imageio.imsave('newRaw.raw', rawData)
This doesn't work, throws error unknown file type. Is there a way to save such files in .raw format.
Note: I have tried this as well:-
rawImageManipulated = rawImage
rawImageManipulated.raw_image[:] = rawData[:] //this copies the new
data onto the rawpy object but does not save or persists the values
assigned.
Rotating a bayer image - I know rawpy does not handle this, nor does any other API or Library acc to my knowledge. The existing image rotation Apis of opencv and pillow alter the sub-pixels while rotating. How do I come to know? After a series of small rotations(say,30 degrees of rotation 12 times) when I get back to a 360 degree of rotation the sub-pixels are not the same when compared using a hex editor.
Are there any solutions to these issues? Am I going in the wrong direction? Could you please guide me on this. I am currently using python i am open to solutions in any language or stack. Thanks
As far as I know, no library is able to rotate an image directly in the Bayer pattern format (if that's what you mean), for good reasons. Instead you need to convert to RGB, and back later. (If you try to process the Bayer pattern image as if it was just a grayscale bitmap, the result of rotation will be a disaster.)
Due to numerical issues, accumulating rotations spoils the image and you will never get the original after a full turn. To minimize the loss, perform all rotations from the original, with increasing angles.

How to turn CT segmentation into 3d model in python

I have this numpy array of slices of segments of the liver CT(ground truths). I want to export them into a viewable format in tools like blender. The slices are white and black, 0-255. Anything other than liver is black, I want the liver to be viewed in 3d.
The slices are in top view. I used this code in kaggle to view them but just in jupyter https://www.kaggle.com/akh64bit/full-preprocessing-tutorial/data. It can be any way to visualize them.
You may try transform your arrays to DICOM format as mentioned before in stackoverflow: Create pydicom file from numpy array
Than you can easily visualize DICOM images in various platforms!
For new folks stumbling upon this question that are looking to convert pixels / voxels to an STL file or files, this Python workflow has worked for me:
Load stack of images as a 3D NumPy array using imageio.imread().
Segment the foreground from the background using one of the many segmentation algorithms from the scikit-image submodule skimage.segmentation, creating a 3D binary image.
Use the marching cubes algorithm from the scikit-image submodule skimage.measure to convert the voxels of interest to a list of faces defined by vertices on the surface of the volume.
Use numpy-stl to create an stl.Mesh object from the list of faces and vertices (as done in this example) then save the mesh with stl.Mesh.save().
As a bonus, you can use the Python package for the Open3D library to open & view multiple STL files!

Categories

Resources