Search code examples
pythonnumpypixel

python dicom -- getting pixel value from dicom file


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?


Solution

  • 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.