3

My code reads a DICOM file, takes the pixel information to a numpy array then it modifies the numpy array. It uses lists because im trying to operate with multiple DICOM files at the same time. I havent found any information on how to take my modified numpy array and make it a DICOM file again so i can use it outside Python.

#IMPORT
import cv2
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
import SimpleITK as sitk  
from glob import glob
import pydicom as dicom

data_path = "C:\\Users\\oliva\\Desktop\\Py tesis\\dicom\\"
output_path = working_path = "C:\\Users\\oliva\\Desktop\\Py tesis\\dicom1\\"
path = glob(data_path + '/*.dcm')

#Checks if we are in the correct path
print ("Total of %d DICOM images.\nFirst 5 filenames:" % len(path))
print ('\n'.join(path[:14]))

data_set = []
for element in path:
    imagen=sitk.ReadImage(element) 
    #imagen = cv2.imread(element)
    array_imagen = sitk.GetArrayViewFromImage(imagen)
    array2_imagen=array_imagen[0] 
    imagen_array_norm = np.uint8(cv2.normalize(array2_imagen, None, 0, 255, cv2.NORM_MINMAX))
    data_set.append(imagen_array_norm)     
#Check
print(len(data_set))    
print(type(data_set[1]))
plt.imshow(data_set[4], cmap=plt.cm.gray)


#Equalization
data_set_eq = equal(data_set)
print(len(data_set_eq))    
print(type(data_set_eq[6]))
plt.imshow(data_set_eq[7], cmap=plt.cm.gray)

#Filtering
data_set_m = median(data_set)
print(len(data_set_m))    
print(type(data_set_m[6]))
plt.imshow(data_set_m[8], cmap=plt.cm.gray)




#Functions
def equal(data):
    data_set_eq = []
    for element in data_set:
    imagen_array_eq = cv2.equalizeHist(element)
    data_set_eq.append(imagen_array_eq)
    return data_set_eq

def median(data):
    data_set_m = []
    for element in data_set:
        imagen_array_m =cv2.medianBlur(element,5)
        data_set_m.append(imagen_array_m)
    return data_set_m

I would like some enlightenment on how to produce a DICOM file from my modified numpy array.

1 Answers1

2

You can convert the numpy array back to a SimpleITK image, and then write it out as Dicom. The code would look something like this:

for x in data_set:
    img = sitk.GetImageFromArray(x)
    sitk.WriteImage(img, "your_image_name_here.dcm")

From the file name suffix, SimpleITK knows to write Dicom.

Note that the filtering you are doing can be accomplished within SimpleITK. You don't really need to use OpenCV. Check out the following filters in SimpleITK: IntensityWindowingImageFilter, AdaptiveHistogramEqualizationFilter, and MedianImageFilter.

https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1IntensityWindowingImageFilter.html https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1AdaptiveHistogramEqualizationImageFilter.html https://itk.org/SimpleITKDoxygen/html/classitk_1_1simple_1_1MedianImageFilter.html

Dave Chen
  • 1,905
  • 1
  • 12
  • 18
  • 2
    Convert using .astype('uint16') if the following error occurs and this works perfectly. *itk::ERROR: GDCMImageIO(00000180CCE9B010): A Floating point buffer was passed but the stored pixel type was not specified.This is currently not supported* – Max Kuchenkiller Sep 16 '22 at 13:05