-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImagePreprocessing.py
More file actions
170 lines (126 loc) · 5.17 KB
/
ImagePreprocessing.py
File metadata and controls
170 lines (126 loc) · 5.17 KB
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
import numpy as np
import cv2
import matplotlib
from matplotlib import pyplot as plt
import os
import sys
import warnings
import myLib
def preprocess_radiograph(img):
img_median = cv2.medianBlur(img, 5)
img_bilateral = cv2.bilateralFilter(img_median, 15, 30, 30)
return scharr_gradient_operator(img_bilateral)
def median_filter(img):
return cv2.medianBlur(img, 5)
def bilateral_filter(img):
return cv2.bilateralFilter(img, 9, 10, 10)
# def sobel_operator(img):
# scale = 1
# delta = 0
# ddepth = cv2.CV_16S
#
# grad_x = cv2.Sobel(img, ddepth, 1, 0, ksize=3, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
# grad_y = cv2.Sobel(img, ddepth, 0, 1, ksize=3, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
#
# abs_grad_x = cv2.convertScaleAbs(grad_x)
# abs_grad_y = cv2.convertScaleAbs(grad_y)
#
# # return cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
# return cv2.add(abs_grad_x, abs_grad_y)
def scharr_gradient_operator(img):
ddepth = cv2.CV_16S
# Gradient X and Y
grad_x = cv2.Scharr(img, ddepth, 1, 0).astype(np.float)
grad_y = cv2.Scharr(img, ddepth, 0, 1).astype(np.float)
# converting back to uint8
abs_grad_x = cv2.convertScaleAbs(grad_x).astype(np.float) # converting back to uint8
abs_grad_y = cv2.convertScaleAbs(grad_y).astype(np.float)
x = cv2.add(abs_grad_x, abs_grad_y)
# normalise it between 0-255
x_norm = (x - np.min(x)) / (np.max(x) - np.min(x)) * 255
return x_norm.astype(np.uint8)
def read_image(image_name):
return cv2.imread(image_name, 0)
def cropped_image():
img = read_image('01.tif')
# cropped = img[500:1350, 1000:2000]
return img[700:1100, 1400:1750]
def save_preprocessed_radiograph(img, idx_radiofraph):
dir_path = "Project_Data/_Data/Radiographs_Preprocessed"
file_name = "/" + str(idx_radiofraph).zfill(2) + ".tif"
myLib.ensure_dir(dir_path)
cv2.imwrite(dir_path + file_name, img)
def create_preprocessed_radiographs():
for num_img in range(1, 15):
path_radiograph = "Project_Data/_Data/Radiographs/" + str(num_img).zfill(2) + ".tif"
img_org = cv2.imread(path_radiograph, 0)
img_pro = preprocess_radiograph(img_org)
# plt.figure()
# plt.imshow(img_org, cmap='gray', interpolation='bicubic')
# myLib.move_figure('top-right')
# plt.title('Original')
# plt.show()
#
# plt.figure()
# plt.imshow(img_pro, cmap='gray', interpolation='bicubic')
# myLib.move_figure('bottom-right')
# plt.title('Original')
# plt.show()
# plt.waitforbuttonpress()
# plt.close('all')
# save_preprocessed_radiograph(img_pro, num_img)
def create_combined_segmentation():
for idx_segmentation in range(1, 15): # 1 - 14
list_segmentation = []
for idx_incisor in range(1, 9): # 1 - 8
file_path = "Project_Data/_Data/Segmentations/" \
+ str(idx_segmentation).zfill(2) \
+ "-" + str(idx_incisor - 1) + ".png"
img_segmentation = cv2.imread(file_path, 0)
list_segmentation.append(img_segmentation)
combined_segmentation = np.zeros_like(list_segmentation[0])
for idx_incisor in range(len(list_segmentation)):
combined_segmentation += list_segmentation[idx_incisor]
plt.figure()
plt.imshow(combined_segmentation, cmap='gray', interpolation='bicubic')
plt.show()
# plt.waitforbuttonpress()
dir_path = "Project_Data/_Data/Segmentations_Combined"
file_name = "/" + str(idx_segmentation).zfill(2) + ".tif"
myLib.ensure_dir(dir_path)
cv2.imwrite(dir_path + file_name, combined_segmentation)
def create_preprocessed_segmentations():
for num_img in range(1, 15):
path_radiograph = "Project_Data/_Data/Segmentations_Combined/" + str(num_img).zfill(2) + ".tif"
img_org = cv2.imread(path_radiograph, 0)
img_pro = preprocess_radiograph(img_org)
plt.figure()
plt.imshow(img_org, cmap='gray', interpolation='bicubic')
myLib.move_figure('top-right')
plt.title('Original')
plt.show()
plt.figure()
plt.imshow(img_pro, cmap='gray', interpolation='bicubic')
myLib.move_figure('bottom-right')
plt.title('Original')
plt.show()
# plt.waitforbuttonpress()
plt.close('all')
save_preprocessed_segmentation(img_pro, num_img)
def save_preprocessed_segmentation(img, idx_segmentation):
dir_path = "Project_Data/_Data/Segmentations_Preprocessed"
file_name = "/" + str(idx_segmentation).zfill(2) + ".tif"
myLib.ensure_dir(dir_path)
cv2.imwrite(dir_path + file_name, img)
if __name__ == '__main__':
os.chdir(os.path.dirname(sys.argv[0]))
warnings.filterwarnings("ignore", ".*GUI is implemented.*")
matplotlib.interactive(True)
print("---------------------------")
print("Start of the script")
# create_preprocessed_radiographs()
# create_combined_segmentation()
# create_preprocessed_segmentations()
print "\nClick to finish process..."
plt.waitforbuttonpress()
print("==========================")