I am trying to do a project. Unfortuantely I can read the dicom files, but when I try to access pixel_array attribute and plot it, it is throwing error.
def readDcm(self):
#path of the decomFolder
print("Path to the DICOM directory is: ",self.dcmPath)
PathDicom = self.dcmPath
lstFilesDCM = [] # creating an empty list
for dirName, subdirList, fileList in os.walk(PathDicom):
for filename in fileList:
if ".dcm" in filename.lower(): # checking whether the file's DICOM
lstFilesDCM.append(os.path.join(dirName,filename))
print(len(lstFilesDCM))
for filename in lstFilesDCM:
currentDcm = pydicom.read_file(lstFilesDCM[0])
dcm_data = currentDcm.PixelData
pixeldata= currentDcm.pixel_array
the error is:
File "C:\Anaconda3\lib\site-packages\pydicom\pixel_data_handlers\pillow_handler.py", line 199, in get_pixeldata
raise NotImplementedError(e.strerror)
NotImplementedError: None
Any suggestion would be nice. Thank you in advance.
Solved Solution:
def load_scan(path):
slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))
pos1 = slices[int(len(slices)/2)].ImagePositionPatient[2]
pos2 = slices[(int(len(slices)/2)) + 1].ImagePositionPatient[2]
diff = pos2 - pos1
if diff > 0:
slices = np.flipud(slices)
try:
slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
except:
slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
for s in slices:
s.SliceThickness = slice_thickness
return slices
I sorted the dicom files according to the axis which was in increasing order by using ImagePositionPatient attribute. Which solved my problem. I was able to access pixel_array attribute and plot. The code is added to original question. I add it below too.
def load_scan(path):
slices = [dicom.read_file(path + '/' + s) for s in os.listdir(path)]
slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))
pos1 = slices[int(len(slices)/2)].ImagePositionPatient[2]
pos2 = slices[(int(len(slices)/2)) + 1].ImagePositionPatient[2]
diff = pos2 - pos1
if diff > 0:
slices = np.flipud(slices)
try:
slice_thickness = np.abs(slices[0].ImagePositionPatient[2] - slices[1].ImagePositionPatient[2])
except:
slice_thickness = np.abs(slices[0].SliceLocation - slices[1].SliceLocation)
for s in slices:
s.SliceThickness = slice_thickness
return slices
code source:https://www.programcreek.com/python/example/97517/dicom.read_file