Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
COVID19-Pari/dicom_decompressor.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
96 lines (57 sloc)
3.12 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pydicom as PDCM | |
import numpy as np | |
import cv2 as cv | |
import glob | |
import os | |
import shutil | |
def conversion(Path): | |
dicom_image = PDCM.read_file(Path) | |
#getting rows, columns, instance and window size info from dicom file tag | |
#the number inside the parenthesis is associated with the tag name varibale | |
rows = dicom_image.get(0x00280010).value #obtain number of rows from the tag | |
columns = dicom_image.get(0x00280011).value #obtain number of colums from the tag | |
dicom_instance = int(dicom_image.get(0x00200013).value) #get slice instance from the tag | |
Window_Center = int(dicom_image.get(0x00281050).value) #window dimentions | |
Window_Width = int(dicom_image.get(0x00281051).value) | |
window_high = int(Window_Center + Window_Width / 2) | |
window_low = int(Window_Center - Window_Width / 2) | |
#rescale intercept and slope for hu | |
if (dicom_image.get(0x00281052) is None): | |
Rescale_Intercept = 0 | |
else: | |
Rescale_Intercept = int(dicom_image.get(0x00281052).value) | |
if (dicom_image.get(0x00281053) is None): | |
Rescale_Slope = 1 | |
else: | |
Rescale_Slope = int(dicom_image.get(0x00281053).value) | |
updated_image = np.zeros((rows, columns), np.uint8) #duplicating the rows and columns with zeros | |
pxl_arr = dicom_image.pixel_array | |
for each_row in range(0, rows): | |
for each_col in range(0, columns): | |
val = pxl_arr[each_row][each_col] | |
rescaled_val = val * Rescale_Slope + Rescale_Intercept | |
if (rescaled_val > window_high): #if the intensity is > than the window max size | |
updated_image[each_row][each_col] = 255 | |
elif (rescaled_val < window_low): #if the intensity is < than the window min size | |
updated_image[each_row][each_col] = 0 | |
else: | |
updated_image[each_row][each_col] = int(((rescaled_val - window_low) / (window_high - window_low)) * 255) #Normalize the intensities | |
return updated_image, dicom_instance | |
def load_all_dcm(): #selecting dcm extension type only | |
files = glob.glob("./covidimages/**/*.DCM", recursive = True) #getting all the dcm images | |
return files | |
def ignore_dcm(): #ignore the dcm extension type, so we are left with png only | |
shutil.copytree("covidimages", "finaloutput", ignore = shutil.ignore_patterns('*dcm')) | |
if __name__ == "__main__": #first we run the conversion to get png img next to each xray dcm image, then we ignore the dcm file extension so we are left with just png images | |
img_path = load_all_dcm() | |
counter = 0 | |
for each_row in range(0, len(img_path)): | |
Output_Image, dicom_instance = conversion(img_path[each_row]) | |
print(img_path[each_row]) | |
path_slice = img_path[each_row][:-4:] #-4 or more, use it when actually woring with the covid images | |
cv.imwrite((path_slice) + '_image' + str(counter) + '.png', Output_Image) | |
counter = counter + 1 | |
print("done with the conversion") | |
print("starting") | |
ignore_dcm() | |
print("Done") | |