-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell_seg.py
196 lines (167 loc) · 8.77 KB
/
cell_seg.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
191
192
193
194
195
196
import os
import time
import argparse
import cv2
import sys
import numpy as np
from PIL import Image
from math import ceil
work_path = os.path.abspath('.')
__py__ = {
'MEDIAR': 'miniconda3/envs/MEDIAR/bin/python',
'cellpose': 'python',
'cellpose3': 'python',
'deepcell': 'python',
'sam': 'python',
'stardist': 'python',
'cellprofiler': 'miniconda3/envs/cp4/bin/python'
}
__methods__ = ['MEDIAR', 'cellpose', 'cellpose3', 'sam', 'stardist', 'deepcell', 'cellprofiler']
__script__ = {
'MEDIAR': os.path.join(work_path, 'src/methods/MEDIAR/mediar_main.py'),
'cellpose': os.path.join(work_path, 'src/methods/cellpose_main.py'),
'cellpose3': os.path.join(work_path, 'src/methods/cellpose3_main.py'),
'deepcell': os.path.join(work_path, 'src/methods/deepcell_main.py'),
'sam': os.path.join(work_path, 'src/methods/sam_main.py'),
'stardist': os.path.join(work_path, 'src/methods/stardist_main.py'),
'cellprofiler': os.path.join(work_path, 'src/methods/cellprofiler/cellprofiler_main.py')
}
def split_image(image, photo_size=2000, photo_step=1000):
overlap = photo_size - photo_step
act_step = ceil(overlap / 2)
patches = []
height, width = image.shape[:2]
padded_image = np.pad(image, ((act_step, act_step), (act_step, act_step), (0, 0)), 'constant')
for y in range(0, height, photo_step):
for x in range(0, width, photo_step):
patch = padded_image[y:y + photo_size, x:x + photo_size]
patches.append((patch, (y, x)))
return patches, (height, width), act_step
def stitch_patches(patches, image_size, act_step, max_size=2000):
if len(patches[0][0].shape) == 3:
full_image = np.zeros((image_size[0] + 2 * act_step, image_size[1] + 2 * act_step, 3), dtype=np.uint8)
else:
full_image = np.zeros((image_size[0] + 2 * act_step, image_size[1] + 2 * act_step), dtype=np.uint8)
for patch, (y, x) in patches:
# Adjust the patch to fit within the maximum size, if necessary
patch = patch[:max_size, :max_size]
# Stitch the patch into the full image
end_y = min(y + patch.shape[0], full_image.shape[0])
end_x = min(x + patch.shape[1], full_image.shape[1])
cropped_patch = patch[:end_y - y, :end_x - x]
full_image[y:y + cropped_patch.shape[0], x:x + cropped_patch.shape[1]] = cropped_patch
# Remove the padding region
full_image = full_image[act_step:-act_step, act_step:-act_step]
return full_image
def generate_grayscale_negative(image_path, output_path):
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
negative_img = cv2.bitwise_not(img)
cv2.imwrite(output_path, negative_img)
def generate_negative(image_path, output_path):
img = cv2.imread(image_path)
negative_img = cv2.bitwise_not(img)
cv2.imwrite(output_path, negative_img)
def process_images_in_directory(image_dir, new_dir, img_type):
for filename in os.listdir(image_dir):
image_path = os.path.join(image_dir, filename)
if os.path.isfile(image_path):
new_image_path = os.path.join(new_dir, filename)
if img_type == 'he':
generate_grayscale_negative(image_path, new_image_path)
elif img_type == 'mif':
generate_negative(image_path, new_image_path)
return new_dir
def main():
parser = argparse.ArgumentParser(description="you should add those parameter")
parser.add_argument('-i', "--input", help="the input directory path", required=True)
parser.add_argument('-o', "--output", help="the output directory", required=True)
parser.add_argument("-m", "--method", nargs='+', help='/'.join(__methods__), required=True)
parser.add_argument("-t", "--img_type", help="ss/he", required=True)
parser.add_argument("-g", "--gpu", help="the gpu index", default="-1")
args = parser.parse_args()
image_dir = args.input
output_path = args.output
methods = args.method
is_gpu = args.gpu
img_type = args.img_type.lower()
processed_dir = image_dir
assert os.path.isdir(image_dir), "Input path must be a directory"
print(f'get {len(os.listdir(image_dir))} files')
for m in methods:
assert m in __methods__
if img_type == 'he' or img_type == 'mif':
new_dir = os.path.join(os.path.dirname(image_dir), 'processed_images')
os.makedirs(new_dir, exist_ok=True)
processed_dir = process_images_in_directory(image_dir, new_dir, img_type)
photo_size = 2048 # Patch size
photo_step = 2000 # Cutting step size
# Step 1: Split the image into patches
for filename in os.listdir(image_dir):
image_path = os.path.join(image_dir, filename)
image = cv2.imread(image_path)
if image.shape[0] > 2048 and image.shape[1] > 2048:
patches, original_size, act_step = split_image(image, photo_size, photo_step)
# Save patches temporarily
patch_output_dir = os.path.join(output_path, 'patches', os.path.splitext(filename)[0])
os.makedirs(patch_output_dir, exist_ok=True)
for idx, (patch, (y, x)) in enumerate(patches):
temp_patch_path = os.path.join(patch_output_dir, f'patch_{y}_{x}.tif')
cv2.imwrite(temp_patch_path, patch)
# Step 2: Process patches using methods
for m in methods:
if m in ['cellprofier']:
cmd = '{} {} -i {} -o {} -g {} -t {}'.format(
__py__[m], __script__[m], image_dir, os.path.join(output_path, m), is_gpu, img_type)
print(cmd)
os.system(cmd)
t = time.time() - start
print(f'{m} ran for a total of {t} s')
continue
method_output_dir = os.path.join(output_path, m, os.path.splitext(filename)[0])
os.makedirs(method_output_dir, exist_ok=True)
# Run the segmentation methods on the patch directory
start = time.time()
cmd = '{} {} -i {} -o {} -g {} -t {}'.format(
__py__[m], __script__[m], patch_output_dir, method_output_dir, is_gpu, img_type)
print(cmd)
os.system(cmd)
t = time.time() - start
print(f'{m} ran for a total of {t} s')
# # Step 3: After segmentation, stitch the processed patches back into the full image
processed_patches = []
for idx, (patch, (y, x)) in enumerate(patches):
if m == 'MEDIAR':
processed_patch = Image.open(os.path.join(method_output_dir, f'patch_{y}_{x}.tif'))
processed_patch = np.array(processed_patch)
else:
processed_patch = cv2.imread(os.path.join(method_output_dir, f'patch_{y}_{x}.tif'))
processed_patches.append((processed_patch, (y, x)))
stitched_result = stitch_patches(processed_patches, original_size, act_step)
final_result_path = os.path.join(output_path, m, filename)
os.makedirs(os.path.dirname(final_result_path), exist_ok=True)
cv2.imwrite(final_result_path, stitched_result)
cv2.imwrite(final_result_path, stitched_result)
print(f'{m} result for {filename} saved to {final_result_path}')
else:
for m in methods:
start = time.time()
if m == 'cellprofiler':
cmd = '{} {} -i {} -o {} -g {} -t {}'.format(
__py__[m], __script__[m], image_dir, os.path.join(output_path, m), is_gpu, img_type)
elif m == 'stardist' and img_type == 'he':
cmd = '{} {} -i {} -o {} -g {} -t {}'.format(
__py__[m], __script__[m], image_dir, os.path.join(output_path, m), is_gpu, img_type)
elif (m == 'cellpose' or m == 'cellpose3' or m == 'MEDIAR') and img_type == 'mif':
cmd = '{} {} -i {} -o {} -g {} -t {}'.format(
__py__[m], __script__[m], image_dir, os.path.join(output_path, m), is_gpu, img_type)
else:
cmd = '{} {} -i {} -o {} -g {} -t {}'.format(
__py__[m], __script__[m], processed_dir, os.path.join(output_path, m), is_gpu, img_type)
print(cmd)
os.system(cmd)
t = time.time() - start
print(f'{m} ran for a total of {t} s')
print(f'{m} result saved to {os.path.join(output_path, m)}')
break
if __name__ == '__main__':
main()