-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdicom_reader.py
82 lines (76 loc) · 3.2 KB
/
dicom_reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from __future__ import print_function
import matplotlib.pyplot as plt
import SimpleITK as sitk
import numpy as np
import sys, os
import png
import cv2
cwd = os.getcwd()
count = 0
for folder, subdirList, fileList in os.walk(cwd):
adc_count = 0
t2_count =0
if folder[-9:-6] == 'ADC':
patient = folder.replace(cwd,'')[1:15]
print(count)
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(folder)
reader.SetFileNames(dicom_names)
image = reader.Execute()
array = np.array(sitk.GetArrayFromImage(image))
count += 1
for i in range(array.shape[0]):
fname = os.path.join(cwd,folder,patient+folder[-9:-6]+str(i)+'.png')
max_val = np.amax(array[i,:,:])
png_array = (array[i,:,:]/ float(max_val) * 255.0).astype(int)
H,W = png_array.shape
h = int(H/2)
w = int(W/2)
step = 33
png_array = png_array[h-step:h+step,w-step:w+step]
plt.imsave(fname,png_array,cmap='gray')
for i in range(array.shape[0]):
patient = folder.replace(cwd,'')[1:15]
fname = folder+'/'+patient+folder[-9:-6]+str(i)+'.png'
try:
directory = os.path.join(cwd,'ADC')
if not os.path.exists(directory):
os.makedirs(directory)
im = cv2.imread(fname, cv2.IMREAD_GRAYSCALE)
resized = cv2.resize(im,(256,256),interpolation = cv2.INTER_CUBIC)
plt.imsave(os.path.join(directory,patient+folder[-9:-6]+str(adc_count)+'.png'),resized,cmap='gray')
adc_count += 1
except:
pass
elif folder[-9:-6] == 'tra':
patient = folder.replace(cwd,'')[1:15]
print(count)
reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(folder)
reader.SetFileNames(dicom_names)
image = reader.Execute()
array = np.array(sitk.GetArrayFromImage(image))
count += 1
for i in range(array.shape[0]):
fname = os.path.join(cwd,folder,patient+folder[-9:-6]+str(i)+'.png')
max_val = np.amax(array[i,:,:])
png_array = (array[i,:,:]/ float(max_val) * 255.0).astype(int)
H,W = png_array.shape
h = int(H/2)
w = int(W/2)
step = 125
png_array = png_array[h-step:h+step,w-step:w+step]
plt.imsave(fname,png_array,cmap='gray')
for i in range(array.shape[0]):
patient = folder.replace(cwd,'')[1:15]
fname = folder+'/'+patient+folder[-9:-6]+str(i)+'.png'
try:
directory = os.path.join(cwd,'T2')
if not os.path.exists(directory):
os.makedirs(directory)
im = cv2.imread(fname, cv2.IMREAD_GRAYSCALE)
resized = cv2.resize(im,(256,256),interpolation = cv2.INTER_CUBIC)
plt.imsave(os.path.join(directory,patient+folder[-9:-6]+str(t2_count)+'.png'),resized,cmap='gray')
t2_count += 1
except:
pass