-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_generation.py
More file actions
147 lines (116 loc) · 4.43 KB
/
data_generation.py
File metadata and controls
147 lines (116 loc) · 4.43 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
import cv2
import numpy as np
import matplotlib.pyplot as plt
import sys
import time
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 = 'IHPC_cropped'
seg_method = 'otsu'
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')
# FFT images
fft = fast_Fourier_transform.fft_rectangular(
thresholded_otsu, r_masks=rectangular_masks)
# 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)
# Data extraction and saving data
data_extraction.data_extraction(
merged['merged markers'], 'Data/data_{}_{}'.format(image_name, seg_method))
end = time.time()
# Print run time
print('Data generation took {} seconds to execute.'.format(round(end-start, 1)))