Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save CT or X-ray data to HDF5 files? #3

Open
anxingle opened this issue Jul 17, 2020 · 7 comments
Open

How to save CT or X-ray data to HDF5 files? #3

anxingle opened this issue Jul 17, 2020 · 7 comments

Comments

@anxingle
Copy link

It is really great work of this repository.

I just wonder could you provide the detail code which convert dicom or nifti file( or any file format) to HDF5 format. It is really helpful for me to convert my X-rays to CT scans. Many thanks.

@Flaick
Copy link

Flaick commented Jul 31, 2020

I run this code on my own dataset and think this will help you.
Firstly, you need to convert the CT images (.dicom or .nii) into the numpy array. This step you can found many materials online. For example, I use simpleITK to extract numpy array from .nii file.
Then you can create a .h5 file to store that. Note that the .h5 file is just a dictionary type and you can simply save that with
f = h5py.File('HDF5_FILE.h5','w') f['ct'] = your_ct_numpy_data f['xray1'] = your_xray_numpy_data f.close()

If you want to discuss more, my email is [email protected]. I am also studying from scratch.

@anxingle
Copy link
Author

anxingle commented Aug 1, 2020

@Flaick Thanks for your kindly reply. I just wonder how to deal with spacing and origin, or just extract raw Numpy array from nifti to h5 file?
Most importantly, how to extract the raw NumPy array from the HDF5 file and save to Nifti file (so I can display in 3D slicer or ITK-SNAP).

@Flaick
Copy link

Flaick commented Aug 2, 2020

Hi, it is simple to extract raw numpy array from .h5 file. Use the code in the following,
hdf5 = h5py.File(file_path, 'r')
ct_data = np.asarray(hdf5['ct'])
x_ray1 = np.asarray(hdf5['xray1'])
x_ray2 = np.asarray(hdf5['xray2'])
To save the ct_data above into nii, you can use this code
out = sitk.GetImageFromArray(ct_data)
sitk.WriteImage(out,'simpleitk_save.nii.gz')

@sainatarajan
Copy link

@Flaick Can you provide the structure of the dataset directory that you used to train this model? Do I need to have a separate hdf5 file for each patient data?

@yinyin-llll
Copy link

Why i use those command but the computer don't correspond.
image

@malonzia
Copy link

malonzia commented Nov 4, 2023

Hi, I am a freshman in this field. If you know how to adjust the width and center appropriately when we transfer the nii to numpy,please send to [email protected]. Thanks.

@WesleyWu42
Copy link

Why i use those command but the computer don't correspond. image

use python instead of python3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants