-
Notifications
You must be signed in to change notification settings - Fork 4
/
projection_library.py
142 lines (89 loc) · 3.59 KB
/
projection_library.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pydicom
from pydicom import dcmread
from pydicom.uid import ExplicitVRLittleEndian
from pydicom.data import get_testdata_file
import pandas as pd
from pandas import DataFrame
import numpy as np
from numba import jit
import cv2
from skimage.transform import rescale
import os
# In[2]:
def import_dicom(in_dir_path):
#names the path to the input model directory, and lists the files in this directory
in_dir_list = sorted(os.listdir(in_dir_path))
model_depth = len(in_dir_list)
model_data = []
i = 0
while i < model_depth:
path = in_dir_path + "/" + in_dir_list[i]
current_slice = pydicom.dcmread(path)
model_slice = current_slice.pixel_array
model_slice = np.asarray(model_slice)
model_data.append(model_slice)
i = i + 1
model_data = np.asarray(model_data, dtype = np.int16)
return model_data
# In[3]:
@jit
def add_img(img_1, img_2):
#tests which of the inputs is the larger image
if len(img_1) > len(img_2):
lar_img = img_1
sml_img = img_2
else:
lar_img = img_2
sml_img = img_1
#finds the distance (in the x-axis direction) between the left
#corners of the two images, if the smaller image was placed
#in the middle of the larger image
lar_centre_index_x = round(len(lar_img)/2) - 1
sml_centre_index_x = round(len(sml_img)/2) - 1
edge_x = lar_centre_index_x - sml_centre_index_x
#finds the distance (in the y-axis direction) between the left
#corners of the two images, if the smaller image was placed
#in the middle of the larger image
lar_centre_index_y = round(len(lar_img[0])/2) - 1
sml_centre_index_y = round(len(sml_img[0])/2) - 1
edge_y = lar_centre_index_y - sml_centre_index_y
output = lar_img
x = edge_x
#works through each pixel where the two images overlap
#and adds the value of the pixels from each image together
while x < (edge_x + len(sml_img)):
i = edge_y
while i < (edge_y + len(sml_img[0])):
out_pixel = (lar_img[x][i] + sml_img[x - edge_x][i - edge_y])
output[x][i]= out_pixel
i = i + 1
x = x + 1
return output
# In[4]:
@jit
def project(model, source_object_distance, object_image_distance):
source_image_distance = source_object_distance + len(model) + object_image_distance
model_depth = len(model)
model_height = len(model[0])
model_length = len(model[0][0])
max_mag = ((source_image_distance)/(source_object_distance))
base_cube = np.zeros((model_depth, int(np.ceil(max_mag * model_height)), int(np.ceil(max_mag * model_length))))
enclosed_cube = np.zeros((model_depth, int(np.ceil(max_mag * model_height)), int(np.ceil(max_mag * model_length))))
i = 0
while i < len(model):
slice_mag = (source_image_distance)/(source_object_distance + i)
temp = cv2.resize(model[i], dsize = (round(model_length * slice_mag), round(model_height * slice_mag)), interpolation = cv2.INTER_LINEAR)
enclosed_cube[i] = add_img(base_cube[i], temp)
i = i + 1
projection = np.sum(base_cube, axis = 0)
return projection
# In[5]:
def save_dicom(image, filename):
dicom_out_path = get_testdata_file("CT_small.dcm")
dicom_out = dcmread(dicom_out_path)
dicom_out.file_meta.TransferSyntaxUID = ExplicitVRLittleEndian
dicom_out.PixelData = image.astype(np.float16).tobytes()
dicom_out.save_as(filename)