-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_segmentation.py
63 lines (47 loc) · 1.29 KB
/
simple_segmentation.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
#!/usr/bin/env python3
import os
import numpy as np
import tifffile as tif
from cellpose import core, io, models, metrics
mask_filter = "_masks"
img_folder = "/path/H2B_Fusion"
path_store = "/path/segmentation"
def normalize_custom(img,
lower=0.0,
upper=400.0,
background=100,
percentile = False,
percentile_lower = 1,
percentile_upper = 99,
):
if percentile:
x01 = np.percentile(img, percentile_lower)
x99 = np.percentile(img, percentile_upper)
if x99 - x01 > 1e-3:
img = (img - x01) / (x99 - x01)
else:
img[:] = 0
else:
img = np.asarray(img)
img = img.astype(np.float32)
img = img - background
img[img<0] = 0
img = (img - lower) / (upper - lower)
return img
model = models.CellposeModel(gpu=True, model_type="CP")
files_img = io.get_image_files(img_folder, mask_filter)
for image in files_img:
path_img = os.path.join(img_folder, image)
img = io.imread(path_img)
img = normalize_custom(img)
print("Model is evaluated")
masks, flows, styles = model.eval(img,
channels = [0,0],
diameter = 25.0,
flow_threshold = 0.4,
cellprob_threshold = 0.0,
stitch_threshold = 0.5,
do_3D = False,
normalize = False,
)
tif.imwrite(os.path.join(path_store, os.path.basename(path_img)), masks, compression='LZW')