Read DICOM files in Pytorch

I am looking for how can I read a b8 file in Pytorch. Any comment will be appreciated. My images are cardiac ultrasound images which do have frames and converted from DICOM to b8 data.

1 Like

pydicom can read a lot of DICOM formats. Unfortunately, I’m not familiar with the b8 format.
Could you try pydicom or provide a sample file?

1 Like

@ptrblck thanks, I will try pydicom. My images are DICOM but they way it saved might be different. So it is file format that I did convert it to b8 file. How can I upload the images here? it seems is allowed for jpg, png,…

You could upload it somewhere else, e.g. dropbox etc.
Could you provide a doc on the b8 format?
Does it refer to the number of channels for your US captures?

@ptrblck the link includes the two image (b8 format and file format which is a DICOM image), and there is two Matlab script which reads the b8 file that will help to understand the b8 format.
It is referring to the header of the image which includes the size of the image (h,w), it’s channel, number of frames,… any help would appreciate.
Dropbox link

It looks like it’s working out of the box:

ds = pydicom.read_file(path)
data = ds.pixel_array
print(data.shape)
> (505, 600, 800, 3)

You could use this method to load the data as numpy arrays.
If that’s working for you, you could implement this logic into a Dataset.

1 Like

@ptrblck I am getting an error like this, do I need to open the file first?

fp = open(fp, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '\\Documents\\img.dcm'

Are you trying to load the DICOM or b8 image?
I’ve tested the DICOM and it worked with the code snippet directly.
In case it helps, I’m using pydicom_1.2.0.

The b8 data couldn’t be read directly and I think you would need to decode it manually.

1 Like

@ptrblck I am loading the DICOM file. Yes, I did install the same pydicom version. the error is complaining about no such file or director. I am new in Pytorch; maybe I am making a mistake about the path

ds = pydicom.read_file('\Documents\ImgUS.dcm')
data = ds.pixel_array
print(data.shape)

It looks like you are using a Windows machine.
Could you try to use the absolute path, i.e. something like C:\\users\\<YOUR_USER_NAME>\\Documents\\ImgUS.dcm?
Alternatively, relative paths should also work, of the file location is not too complicated to navigate to.

1 Like

@ptrblck, I did try. I got the same error. Yes, I am using a Windows machine.

Just for the sake of debugging, could you copy the file into your current working directory, where your python script is located, and try:

path = './ImgUS.dcm'
pydicom.read_file(path)
1 Like

@ptrblck thanks you. I did the same, and I got an error, but when I removed the .dcm extension from the path, it could read the image.
Then, I did try with the previous path and without .dcm, but couldn’t navigate to the file.
Does it always mean my dataset should be in in the same path of my python script?

No, you can place the data in wherever you want to, as long as the path can be reached.
If the previous path is still not working you might want to check for typos by adding the folders one by one and checking, if the current path exists:

import os
print(os.path.exists('C:\\'))
print(os.path.exists('C:\\Users\\'))
print(os.path.exists('C:\\Users\\YOUR_NAME\\'))
...

However, it’s good you could load the file with pydicom! :wink:

1 Like

@ptrblck Thanks a lot :slight_smile:

1 Like

@ptrblck just to let you know I did check the path as you recommend but it is not about the path. I did check the path step by step. It is about the image because the image is DICOM but the extension of the image is “file”.
when I did

import os
print(os.path.exists('C:\\Users\\Neda\\Documents\\imgg'))

it could read the image while it cannot read imgg.dcm

1 Like