-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathct_simulator.py
181 lines (137 loc) · 6.62 KB
/
ct_simulator.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from datetime import datetime
from io import BytesIO
import numpy as np
import streamlit as st
from PIL import Image
from pydicom import dcmread
import constants as const
import functions as fun
st.set_page_config(
page_title="CT Simulator",
page_icon=":computer:",
layout="wide",
initial_sidebar_state="expanded",
menu_items={
'About': const.ABOUT
}
)
def set_default():
st.session_state.alpha = 2.0
st.session_state.phi = 180
st.session_state.n = 200
st.session_state.radio = "Clear"
def reset_radio():
st.session_state.radio = "Clear"
title = st.title(const.TITLE)
st.sidebar.markdown(const.AUTHORS, unsafe_allow_html=True)
st.sidebar.write(const.SIDEBAR_TEXT)
filter_checkbox = st.sidebar.checkbox('Filter Sinogram', value=True, on_change=reset_radio)
dicom_save_checkbox = st.sidebar.checkbox("Save in Dicom Format")
st.subheader("File upload")
file = None
image = None
patient_info = {}
dicom_checkbox = st.checkbox('use DICOM format')
if dicom_checkbox:
file = st.file_uploader("Choose file", type=['dcm'])
if file is not None:
st.info("DICOM file uploaded succesfully")
dc = dcmread(file)
image = dc.pixel_array
patient_info = fun.get_patient_info(dc)
st.write("Data from DICOM:")
st.write(patient_info)
else:
file = st.file_uploader("Choose file", type=['jpg', 'jpeg', 'png'])
if file is not None:
patient_info = fun.get_patient_info()
image = Image.open(file)
if image is not None:
image = np.array(image)
cropped_image = fun.adjust_image(image)
input1, input2 = st.columns(2)
with input1:
st.subheader(f'Uploaded Image ({image.shape[0]}x{image.shape[1]})')
st.image(image, caption="Uploaded image")
with input2:
st.subheader(f'Adjusted grayscaled image ({cropped_image.shape[0]}x{cropped_image.shape[1]})')
st.image(cropped_image, caption="Adjusted image")
st.subheader("Define tomograph parameters")
alpha_step = st.slider('Delta alpha (step in degrees)', 0.1, 5.0, 2.0, 0.1, key='alpha', on_change=reset_radio)
phi = st.slider("divergence (in degrees)", 0, 360, 180, 10, key='phi', on_change=reset_radio)
n = st.slider("number of detectors", 0, 400, 200, 10, key='n', on_change=reset_radio)
default_button = st.button(label='Set default', on_click=set_default)
st.subheader("Result generation options")
option = st.radio("", const.GEN_OPTIONS, key='radio')
if option == "Clear":
st.write("Cleared")
else:
if option == "Show intermediate steps":
sinogram = fun.make_sinogram(cropped_image, alpha_step, phi, n)
st.subheader("Sinogram generation steps")
sinogram_step = st.slider("", 1, int(np.floor(360 / alpha_step)), step=5, key="s_step")
st.subheader("Sinogram")
st.image(sinogram[0:sinogram_step], caption=f'Sinogram ({sinogram_step}x{sinogram.shape[1]})')
st.subheader("Tomograph rotation degree")
iterations = st.slider("", 1, 360, step=5, key="t_step")
if filter_checkbox:
filtered = fun.filter_sinogram(sinogram)
st.subheader("Reconstructed image")
reconstructed = fun.reconstruct_image(filtered, alpha_step, phi, n, cropped_image.shape[0], iterations)
st.image(reconstructed, caption="Reconstructed image")
else:
st.subheader("Reconstructed image")
reconstructed = fun.reconstruct_image(sinogram, alpha_step, phi, n, cropped_image.shape[0], iterations)
st.image(reconstructed, caption="Reconstructed image")
else:
if filter_checkbox:
sin1, sin2, rec = st.columns(3)
else:
sin1, rec = st.columns(2)
with sin1:
st.subheader("Sinogram")
sinogram = fun.make_sinogram(cropped_image, alpha_step, phi, n)
st.image(sinogram, caption=f'Sinogram ({sinogram.shape[0]}x{sinogram.shape[1]})')
if filter_checkbox:
with sin2:
st.subheader("Filtered sinogram")
filtered = fun.filter_sinogram(sinogram)
st.image(fun.normalize(filtered), caption="Filtered sinogram")
reconstructed = fun.reconstruct_image(filtered, alpha_step, phi, n, cropped_image.shape[0])
else:
reconstructed = fun.reconstruct_image(sinogram, alpha_step, phi, n, cropped_image.shape[0])
with rec:
st.subheader("Reconstructed image")
st.image(reconstructed, caption="Reconstructed image")
st.subheader("RMSE")
rmse = round(fun.calculate_rmse(reconstructed, cropped_image) * 100, 3)
st.info(f'{rmse}%')
st.subheader('Save file')
with st.form("File Name"):
file_name = st.text_input("File name", value=file.name, placeholder="file_name.jpg")
if dicom_save_checkbox:
st.write("Dicom metadata:")
form_col1, form_col2 = st.columns(2)
with form_col1:
patient_info['id'] = str(st.number_input("PatientID", min_value=0, value=int(patient_info['id'])))
patient_info['sex'] = st.radio("PatientSex", ("Male", "Female"))
patient_info['date'] = st.date_input("Date", value=datetime.now())
with form_col2:
patient_info['name'] = st.text_input("PatientName", value=patient_info['name'])
patient_info['weight'] = st.number_input("PatientWeight", min_value=0.0, max_value=200.0,
step=0.1, value=float(patient_info['weight']))
patient_info['time'] = st.time_input("Time", value=datetime.now())
patient_info['comments'] = st.text_input("ImageComments", value=patient_info['comments'])
confirm_filename = st.form_submit_button("Confirm")
image_data = BytesIO()
save_img = Image.fromarray(np.uint8(reconstructed * 255), 'L')
save_img.save(image_data, format="JPEG")
file_name = fun.adjust_filename(file_name, dicom_save_checkbox)
if dicom_save_checkbox:
"save in dicom format"
dicom_file = fun.create_dicom(save_img, patient_info)
st.download_button("Download dicom file", data=dicom_file, file_name=file_name)
else:
st.download_button("Download reconstructed image", data=image_data, file_name=file_name)
else:
st.warning("File has not been uploaded")