-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualisation.py
More file actions
184 lines (148 loc) · 5.66 KB
/
visualisation.py
File metadata and controls
184 lines (148 loc) · 5.66 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import cv2
import numpy as np
import matplotlib.pyplot as plt
import sys
import time
import copy
import data_extraction
import oversegmentation
import watershed
import fast_Fourier_transform
import image_processing
# Set image files and segmentation method
if len(sys.argv) > 1:
image_name = sys.argv[1]
assert isinstance(image_name, str), 'Incorrect first variable type passed!'
seg_method = sys.argv[2]
assert isinstance(seg_method, str), 'Incorrect second variable type passed!'
try:
seg_cmap = sys.argv[3]
except:
seg_cmap = None
else:
image_name = 'MIPAR_cropped'
seg_method = 'otsu'
seg_cmap = None
print('Segmenting {} image based on {} method...'.format(image_name, seg_method))
# Set parameters and load image acoording to image_name
if image_name == 'IHPC':
image = cv2.imread(
'Data/' + image_name + '.png')
image_ori = cv2.imread(
'Data/' + image_name + '.png')
rectangular_masks = [(-52, 60), (75, 45), (89.9, 30),(60, 25)] # FFT masks
if seg_method == 'FFT':
(thersh, kernel, thresh_pre, dia_iter) = (
0.24, (3, 3), 25, 3) # Watershed segmentation
merge_thresh = 6500 # Merging threshold
elif seg_method == 'otsu':
(thersh, kernel, thresh_pre, dia_iter) = (
0.20, (3, 3), 30, 3) # Watershed segmentation
merge_thresh = 6000 # Merging threshold
else:
raise ValueError('Incorret seg_method!')
elif image_name == 'IHPC_cropped':
image = cv2.imread(
'Data/' + image_name + '.png')
image_ori = cv2.imread(
'Data/' + image_name + '.png')
rectangular_masks = [(70, 30), (-35, 30)] # FFT masks
if seg_method == 'FFT':
(thersh, kernel, thresh_pre, dia_iter) = (
0.24, (3, 3), 25, 3) # Watershed segmentation
merge_thresh = 4000 # Merging threshold
elif seg_method == 'otsu':
(thersh, kernel, thresh_pre, dia_iter) = (
0.20, (3, 3), 30, 3) # Watershed segmentation
merge_thresh = 4000 # Merging threshold
else:
raise ValueError('Incorret seg_method!')
elif image_name == 'MIPAR':
image = cv2.imread(
'Data/' + image_name + '.png')
image_ori = cv2.imread(
'Data/' + image_name + '.png')
rectangular_masks = [(-30, 50), (65, 45), (89.9, 40)] # FFT masks
if seg_method == 'FFT':
(thersh, kernel, thresh_pre, dia_iter) = (
0.21, (5, 5), 65, 2) # Watershed segmentation
merge_thresh = 800 # Merging threshold
elif seg_method == 'otsu':
(thersh, kernel, thresh_pre, dia_iter) = (
0.22, (5, 5), 30, 2) # Watershed segmentation
merge_thresh = 1000 # Merging threshold
else:
raise ValueError('Incorret seg_method!')
elif image_name == 'MIPAR_cropped':
image = cv2.imread(
'Data/' + image_name + '.png')
image_ori = cv2.imread(
'Data/' + image_name + '.png')
rectangular_masks = [(89.9, 30)] # FFT masks
if seg_method == 'FFT':
(thersh, kernel, thresh_pre, dia_iter) = (
0.21, (5, 5), 65, 2) # Watershed segmentation
merge_thresh = 400 # Merging threshold
elif seg_method == 'otsu':
(thersh, kernel, thresh_pre, dia_iter) = (
0.22, (5, 5), 30, 2) # Watershed segmentation
merge_thresh = 800 # Merging threshold
else:
raise ValueError('Incorret seg_method!')
else:
raise ValueError('Incorret image name!')
# Measure run time
start = time.time()
# Denoisng
denoised = image_processing.denoise(
image, method='gaussian', ksize=(5, 5), sigmaX=5)
# Thresholding
thresholded_otsu = image_processing.threshold(denoised, method='Otsu')
# Visualise denoised and thresholded images
image_processing.display_image_1D(
[(denoised, 'Denoised Image'),
(thresholded_otsu, 'Thresholded Image')],
cmap=[None, 'gray'],
visualisation=True)
# FFT images
fft = fast_Fourier_transform.fft_rectangular(
thresholded_otsu, r_masks=rectangular_masks)
masks = fast_Fourier_transform.create_rectangular_masks(
thresholded_otsu, r_masks=rectangular_masks)
fft_comparison = fast_Fourier_transform.fft_filter(thresholded_otsu, masks)
# Visualise FFT comparison image
image_processing.display_image_2D(
[(fft_comparison['input image'], 'Input Image'),
(fft_comparison['after FFT'], 'Frequency Domain'),
(fft_comparison['FFT + mask'], 'Masked Frequency Domain'),
(fft_comparison['after FFT inverse'], 'Output Image')],
rows=2, cols=2,
cmap=['gray', None, None, 'gray'],
visualisation=True)
# Segmentation
if seg_method == 'FFT':
segmented = watershed.watershed(
fft, image, thresh=thersh, kernel=kernel, thresh_pre=thresh_pre, dia_iter=dia_iter)
else:
segmented = watershed.watershed(
thresholded_otsu, image, thresh=thersh, kernel=kernel, thresh_pre=thresh_pre, dia_iter=dia_iter)
# Reducing oversegmentation
merged = oversegmentation.oversegmentation(segmented['modified markers'], image_ori, merge_thresh)
# Visualise circumference image
circum = data_extraction.circumference_visualise(merged['merged markers'])
image_processing.display_image(
(circum, 'Circumferecne Illustration'),
cmap=seg_cmap,
visualisation=True)
# Visualise segmentation results
image_processing.display_image_2D(
[(image_ori, 'Original Image'),
(merged['merged segmented image'], 'Segmented Image'),
(segmented['modified markers'], 'Marker Image before Merging'),
(merged['merged markers'], 'Merged Marker Image')],
rows=2, cols=2,
cmap=[None, None, seg_cmap, seg_cmap],
visualisation=True)
end = time.time()
# Print run time
print('Visualisation took {} seconds to execute.'.format(round(end-start, 1)))