Reputation: 387
I was trying to get the pixel values of a dicom file in python using the dicom library.
But it returns only an array with zeros.
My code was like this:
import dicom
import numpy
ds=pydicom.read_file("sample.dcm")
print(ds.pixel_array)
and the results is
[[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
...,
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]]
Do you have any idea how to get the values of the pixels?
Upvotes: 2
Views: 4530
Reputation: 546
One way you can visualize the image/dose that you're looking for is with the DicomRTTool (https://github.com/brianmanderson/Dicom_RT_and_Images_to_Mask)
You can install it with
pip install DicomRTTool
Then, use the code as follows to pull image/dose/structure information
from DicomRTTool.ReaderWriter import DicomReaderWriter, sitk
Dicom_path = r'.some_path_to_dicom'
Dicom_reader = DicomReaderWriter(get_dose_output=True)
Dicom_reader.walk_through_folders(Dicom_path) # This will parse through all DICOM present in the folder and subfolders
Dicom_reader.get_images()
Dicom_reader.get_dose()
image_sitk_handle = Dicom_reader.dicom_handle
image_numpy = sitk.GetArrayFromImage(image_sitk_handle)
dose_handle = Dicom_reader.dose_handle
dose_numpy = sitk.GetArrayFromImage(dose_handle)
You can then view the numpy array in whatever visualizer you prefer. I personally like Plot_And_Scroll_Images https://github.com/brianmanderson/Plot_And_Scroll_Images
from Plot_Scroll_Images import plot_scroll_Image
plot_scroll_Image(dose_numpy)
Hope this helps
Upvotes: 0
Reputation: 769
There should be values. If you use an IDE (i.g. Spyder) you may see it in the Variables Manager.
Upvotes: 0
Reputation: 2298
import dicom
import dicom_numpy
dataset = dicom.read_file('sample.dcm')
pixels, ijk_to_xyz = dicom_numpy.combine_slices([dataset])
Here pixels
will contain the properly scaled pixel values, and ijk_to_xyz
is a 4x4 affine matrix mapping the indices to the DICOM patient coordinate system.
This code snippet uses dicom-numpy, a higher-level abstraction built on top of pydicom.
It handles the most common use case when extracting pixel values from a set of DICOM files:
We use it on all our projects where we need to extract image data from DICOM files into a NumPy ndarray.
Upvotes: -1
Reputation: 1358
The code as written should work. It is possible that the beginning and end of the array are truly zeros.
It is usually more meaningful to look at something in the middle of the image.
E.g.:
midrow = ds.Rows // 2
midcol = ds.Columns // 2
print(ds.pixel_array[midrow-20:midrow+20, midcol-20:midcol+20])
If it is truly zeros everywhere, then that is either the true image or some kind of bug. Perhaps reading with a dicom viewer can help if that hasn't been tried.
Upvotes: 2