-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_and_show_images.py
190 lines (164 loc) · 8.51 KB
/
display_and_show_images.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
182
183
184
185
186
187
188
189
190
import matplotlib.pyplot as plt
import sys
import os
import numpy as np
from ipywidgets import interact, fixed
from MyProcessDicom import *
import SimpleITK as sitk
def show_slice_images(all_image_data):
last_name = '0000000L'
# for each image
for i in range(0, len(all_image_data)):
# get paths and file names of the current image
image_data = all_image_data[i]
# if image_data['moving_name'][:12] != last_name:
# last_name = image_data['moving_name'][:8]
print(image_data['moving_name'][:12])
moving_file_name = image_data['registered_sub_folder'] + image_data['frigid_name']
reference_file_name = image_data['reference_folder'] + image_data['reference_name']
reference = sitk.ReadImage(reference_file_name)
moving = sitk.ReadImage(moving_file_name)
show_mixed_images(reference, moving)
def display_images_with_mask(image_z, alpha, fixed, moving):
# img = (1.0 - alpha)*fixed[:,:,image_z] + alpha*moving[:,:,image_z]
fixed = sitk.GetArrayFromImage(fixed)
moving = sitk.GetArrayFromImage(moving) # mask
dst = fixed[image_z,:,:]*alpha+moving[image_z,:,:]*(1-alpha)*255
plt.figure(figsize=(9,9))
plt.imshow(dst, cmap=plt.cm.Greys_r)
# plt.imshow(sitk.GetArrayViewFromImage(img),cmap=plt.cm.Greys_r);
plt.axis('off')
plt.show()
def display_images_with_all_mask(image_z, alpha, fixed, moving, vessel):
# img = (1.0 - alpha)*fixed[:,:,image_z] + alpha*moving[:,:,image_z]
fixed = sitk.GetArrayFromImage(fixed)
moving = sitk.GetArrayFromImage(moving) # mask
vessel = sitk.GetArrayFromImage(vessel)
dst = fixed[image_z,:,:]*alpha+moving[image_z,:,:]*(1-alpha)*255 + vessel[image_z,:,:]*(1-alpha)*255
plt.figure(figsize=(9,9))
plt.imshow(dst, cmap=plt.cm.Greys_r)
# plt.imshow(sitk.GetArrayViewFromImage(img),cmap=plt.cm.Greys_r);
plt.axis('off')
plt.show()
def show_masked_images(image, mask, vessel=None):
if vessel is None:
interact(display_images_with_mask, image_z=(0,image.GetSize()[2] - 1), alpha=(0.0,1.0,0.05),
fixed = fixed(image), moving=fixed(mask));
else:
interact(display_images_with_all_mask, image_z=(0,image.GetSize()[2] - 1), alpha=(0.0,1.0,0.05),
fixed = fixed(image), moving=fixed(mask), vessel=fixed(vessel));
def display_images_with_mask_sagittal(image_z, fixed, moving):
# img = (1.0 - alpha)*fixed[:,:,image_z] + alpha*moving[:,:,image_z]
fixed = sitk.GetArrayFromImage(fixed)
moving = sitk.GetArrayFromImage(moving) # mask
dst = fixed[:,:,image_z]*0.5+moving[:,:,image_z]*0.5*255
dst = np.flipud(dst)
plt.figure(figsize=(7,7))
plt.imshow(dst, cmap=plt.cm.Greys_r)
# plt.imshow(sitk.GetArrayViewFromImage(img),cmap=plt.cm.Greys_r);
plt.axis('off')
plt.show()
def show_masked_images_sagittal(image, mask):
interact(display_images_with_mask_sagittal, image_z=(0,image.GetSize()[0] - 1),
fixed = fixed(image), moving=fixed(mask));
def display_images_with_all_masks(image_z, alpha, fixed, moving, tibia, femur_show = 1):
# img = (1.0 - alpha)*fixed[:,:,image_z] + alpha*moving[:,:,image_z]
fixed = sitk.GetArrayFromImage(fixed)
moving = sitk.GetArrayFromImage(moving) # mask
tibia = sitk.GetArrayFromImage(tibia) # mask
dst = fixed[:,:,image_z]*alpha+moving[:,:,image_z]*(1-alpha)*255*femur_show+tibia[:,:,image_z]*(1-alpha)*255
dst = np.flipud(dst)
plt.figure(figsize=(7,7))
plt.imshow(dst, cmap=plt.cm.Greys_r)
# plt.imshow(sitk.GetArrayViewFromImage(img),cmap=plt.cm.Greys_r);
plt.axis('off')
plt.show()
def show_all_masked_images(image, mask, tibia, femur_show=1):
interact(display_images_with_all_masks, image_z=(0,image.GetSize()[0] - 1), alpha=(0.0,1.0,0.05),
fixed = fixed(image), moving=fixed(mask), tibia=fixed(tibia), femur_show=fixed(femur_show));
def show_sagital_slice_images(all_image_data, is_rigid=False):
last_name = '0000000L'
# for each image
for i in range(0, len(all_image_data)):
# for i in range(0, 4):
# get paths and file names of the current image
image_data = all_image_data[i]
# if image_data['moving_name'][:8] != last_name:
# last_name = image_data['moving_name'][:8]
print(image_data['moving_name'][:12])
# mask_file_name = image_data[0]['reference_folder'] + '9941231L_TP0_prep_f.mha'
if is_rigid:
mask_file_name = image_data['segmented_folder'] + image_data['fmask'][:-5] + 'f_rigid.mha'
tibia_file_name = image_data['segmented_folder'] + image_data['fmask'][:-5] + 't_rigid.mha'
else:
mask_file_name = image_data['segmented_folder'] + image_data['fmask']
tibia_file_name = image_data['segmented_folder'] + image_data['fmask'][:-5] + 't.mha'
# mask_file_name = 'C:\\Zhixuan\\OAI-registration\\pykneer-yg\\reference\\longitudinal\\9941446L_TP0_prep_t.mha'
moving_file_name = image_data['moving_folder'] + image_data['moving_name']
mask = sitk.ReadImage(mask_file_name)
tibia = sitk.ReadImage(tibia_file_name)
moving = sitk.ReadImage(moving_file_name)
key = 1
if key == 1:
mask = resample_bwimage(mask)
tibia = resample_bwimage(tibia)
moving = resample_bwimage(moving)
# show_masked_images_sagittal(moving, mask)
show_all_masked_images(moving, mask,tibia)
else:
show_masked_images(moving, mask)
def show_sagital_reference(all_image_data, femur_show = 0, key = 1):
last_name = '0000000L'
# for each image
for i in range(0, len(all_image_data)):
# for i in range(0, 4):
# get paths and file names of the current image
image_data = all_image_data[i]
# if image_data['moving_name'][:8] != last_name:
# last_name = image_data['moving_name'][:8]
print(image_data['moving_name'][:12])
# mask_file_name = image_data[0]['reference_folder'] + '9941231L_TP0_prep_f.mha'
mask_file_name = image_data['segmented_folder'] + image_data['fmask']
tibia_file_name = "C:\\Zhixuan\\OAI-registration\\pykneer-yg\\reference\\longitudinal\\" + image_data['fmask'][:-5] + 't.mha'
# mask_file_name = 'C:\\Zhixuan\\OAI-registration\\pykneer-yg\\reference\\longitudinal\\9941446L_TP0_prep_t.mha'
moving_file_name = image_data['moving_folder'] + image_data['moving_name']
mask = sitk.ReadImage(mask_file_name)
tibia = sitk.ReadImage(tibia_file_name)
moving = sitk.ReadImage(moving_file_name)
if key == 1:
mask = resample_bwimage(mask)
tibia = resample_bwimage(tibia)
moving = resample_bwimage(moving)
# show_masked_images_sagittal(moving, mask)
show_all_masked_images(moving, mask,tibia, femur_show)
elif key == 2:
mask = resample_bwimage(mask)
tibia = resample_bwimage(tibia)
moving = resample_bwimage(moving)
show_all_masked_images(moving, tibia, mask, 0)
else:
show_masked_images(moving, mask)
def show_registered_result(all_image_data, key = 1):
last_name = '0000000L'
# for each image
for i in range(0, len(all_image_data)):
# for i in range(0, 4):
# get paths and file names of the current image
image_data = all_image_data[i]
# if image_data['moving_name'][:8] != last_name:
# last_name = image_data['moving_name'][:8]
print(image_data['moving_name'][:12])
# mask_file_name = image_data[0]['reference_folder'] + '9941231L_TP0_prep_f.mha'
mask_file_name = image_data['reference_folder'] + image_data['fmask_file_name']
vessel_file_name = image_data['reference_folder'] + image_data['fvmask_file_name']
# mask_file_name = 'C:\\Zhixuan\\OAI-registration\\pykneer-yg\\reference\\longitudinal\\9941446L_TP0_prep_t.mha'
moving_file_name = image_data['registered_sub_folder'] + image_data['frigid_name']
mask = sitk.ReadImage(mask_file_name)
vessel = sitk.ReadImage(vessel_file_name)
moving = sitk.ReadImage(moving_file_name)
if key == 1:
show_masked_images(moving, mask)
elif key == 2:
show_masked_images(moving, vessel)
else:
show_masked_images(moving, mask, vessel)