mhe / pynrrd

Simple pure-python module for reading and writing nrrd files.
https://pynrrd.readthedocs.io/
MIT License
116 stars 50 forks source link

Converting NRRD file to Dicom file #115

Closed NonlinearNimesh closed 2 years ago

NonlinearNimesh commented 2 years ago

Hi I have a .nrrd file and I want to convert that image into Dicom file, Can anyone help me in doing that

for now i tried doing like NRRD -> GIF -> JPG -> Dicom but the final results was not good, can any one please help me to do the same.

This is the sample file on which i am trying the conversion

https://drive.google.com/file/d/1SII7xVgUhPAH5Bkzg68dF4CAXawsWk9g/view?usp=sharing

Thanks

addisonElliott commented 2 years ago

This is a bit off-topic for this repository, but I'll allow it anyway. When you load an NRRD file in this library, it turns into a Numpy array of data that you can do whatever you want with.

You can certainly go the NRRD-to-image-to-DICOM route, but I'd advocate against it. If it were me, I'd just use pydicom and create the DICOM file in Python. See here for an example where they create a DICOM file from scratch: https://pydicom.github.io/pydicom/dev/old/writing_files.html

You'd just have to modify that example to send the NRRD data instead. Also, there's probably some code in mapping the NRRD metadata to DICOM.

NonlinearNimesh commented 2 years ago

Hi Thanks for the response, But in the CT nrrd file there are 504 instances and I needed to convert each and every instance in dicom which I am not able to using the way you mentioned

addisonElliott commented 2 years ago

I haven't worked with DICOM in any meaningful way in a few years, so take this with a grain of salt.

I recall that DICOM only supported 2D images. In the research lab I worked in, we had cardiac MRI cine's which were 4 dimensional (3D images of the heart across time). The DICOM files were actually a folder containing many 2D slices. Each 2D slice contains information about where they're located. Any DICOM viewer will load all DICOM files in that folder and peice together the data.

In our lab, we went the other way with the data. The data came in as DICOM and I used pydicom to load that data and turn it into NRRD because I felt it was easier to work with.

Long story short, you have to split the data up into 2D slices and save each slice individually in a DICOM file. You'll want to make sure you set the patient, study, and series IDs appropriately. That info is how DICOM viewers know what data belongs together.

Welcome to the confusing world of DICOM 😄

NonlinearNimesh commented 2 years ago

So like I have NRRD file of size (512,512,504) means There are 504 instances of (512, 512) size image, and what you are trying to say is I pick each instance like (512, 512, 1), (512, 512, 1), (512, 512, 1) till 504 and convert the same into dicom.

NonlinearNimesh commented 2 years ago

i am trying to convert NRRD file to dicom using JPG for example NRRD -> JGP -> DICOM but when i try to convert the NRRD to jpg, output image is noisey Below is my implementation


filename = "/content/drive/MyDrive/CT.nrrd"
readdata, header = nrrd.read(filename)
print(readdata.shape) # (512, 512, 504)

#Not converting pixel to a jpg image 

from PIL import Image
import numpy as np
for i in range(504):
  if i == 200:
    img = np.array(readdata[:,:,i]) # for now i was trying to convert instance number 200 to jpg
    print(img.shape)
    print(np.amax(img))
    #img = (np.maximum(img, 0) / img.max()) * 255.0
    img = np.amax(img) - img
    img = Image.fromarray(np.uint8(img))
    img.save(f'/content/testrgb{i}.png')
    break

But image was unacceptable, you can reproduce the output by using above mentioned data link.

Can you please help in this .................
addisonElliott commented 2 years ago

img = np.amax(img) - img That's not right.

You want this line img = (np.maximum(img, 0) / img.max()) * 255.0. My guess is that gives you a terrible contrast image because you're scaling from the max value. The NRRD datatype is short, so try dividing by 32767 or 65535 instead and see if that result is better.

Additionally, I'd try specifying the mode in Image.fromarray to verify you have the right mode.

Overall, your question is getting into image processing and is unrelated to NRRD and this library. I'm going to close this now. Best of luck.