-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_cell_crops.py
143 lines (121 loc) · 4.4 KB
/
generate_cell_crops.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
import warnings
import numpy as np
from imageio.v2 import imsave
from skimage.measure import regionprops
from scipy.ndimage.morphology import grey_dilation
import pandas as pd
warnings.simplefilter(action="ignore", category=FutureWarning)
colors = ["red", "yellow", "blue", "green"]
def convert_bitdepth(image, bitdepth):
if bitdepth == 8:
if image.dtype != np.uint8:
return (image / np.iinfo(image.dtype).max * 255).astype(np.uint8)
else:
return np.uint8(image)
elif bitdepth == 16:
if image.dtype != np.uint16:
return (image / np.iinfo(image.dtype).max * 65535).astype(np.uint16)
else:
return np.uint16(image)
elif bitdepth == 32:
if image.dtype != np.uint32:
return (image / np.iinfo(image.dtype).max * 4294967295).astype(np.uint32)
else:
return np.uint32(image)
return image
def safe_crop(image, bbox):
y1, x1, y2, x2 = bbox
img_h, img_w = image.shape[:2]
is_single_channel = len(image.shape) == 2
if x1 < 0:
pad_x1 = 0 - x1
new_x1 = 0
else:
pad_x1 = 0
new_x1 = x1
if y1 < 0:
pad_y1 = 0 - y1
new_y1 = 0
else:
pad_y1 = 0
new_y1 = y1
if x2 > img_w - 1:
pad_x2 = x2 - (img_w - 1)
new_x2 = img_w - 1
else:
pad_x2 = 0
new_x2 = x2
if y2 > img_h - 1:
pad_y2 = y2 - (img_h - 1)
new_y2 = img_h - 1
else:
pad_y2 = 0
new_y2 = y2
patch = image[new_y1:new_y2, new_x1:new_x2]
patch = (
np.pad(
patch,
((pad_y1, pad_y2), (pad_x1, pad_x2)),
mode="constant",
constant_values=0,
)
if is_single_channel
else np.pad(
patch,
((pad_y1, pad_y2), (pad_x1, pad_x2), (0, 0)),
mode="constant",
constant_values=0,
)
)
return patch, (new_y1, new_x1, new_y2, new_x2)
# Optional code to generate the segmented cell crops
def generate_crops(image_stack, cell_mask, crop_size, crop_bitdepth, crop_mask, mask_cell, output_folder, output_prefix):
regions = regionprops(cell_mask)
cell_bboxes = []
for region in regions:
image_cp = image_stack[0][0].copy()
bbox = region.bbox
bbox_center = [(bbox[0] + bbox[2]) // 2, (bbox[1] + bbox[3]) // 2]
h, w = bbox[2] - bbox[0], bbox[3] - bbox[1]
fixed_bbox = (
bbox_center[0] - crop_size // 2,
bbox_center[1] - crop_size // 2,
bbox_center[0] + crop_size // 2,
bbox_center[1] + crop_size // 2,
)
if crop_mask:
this_cell_mask = cell_mask.copy()
this_cell_mask[this_cell_mask != region.label] = 0
this_cell_mask[this_cell_mask == region.label] = 1
this_cell_mask = grey_dilation(this_cell_mask, size=7)
curr_cell_mask, _ = safe_crop(this_cell_mask, fixed_bbox)
imsave(f"{output_folder}/{output_prefix}cell{region.label}_mask.png", np.uint8(curr_cell_mask * 255))
for curr_img_index in range(len(image_stack)):
if curr_img_index != 0:
image_cp = image_stack[curr_img_index][0].copy()
cell_crop, _ = safe_crop(image_cp, fixed_bbox)
imsave(f"{output_folder}/{output_prefix}cell{region.label}_crop_" + colors[curr_img_index] + ".png", convert_bitdepth(cell_crop, crop_bitdepth))
if mask_cell:
this_cell_mask = cell_mask == region.label
this_cell_mask = grey_dilation(this_cell_mask, size=7)
image_cp[this_cell_mask == 0] = 0
cell_mask_crop, _ = safe_crop(image_cp, fixed_bbox)
imsave(f"{output_folder}/{output_prefix}cell{region.label}_crop_masked_" + colors[curr_img_index] + ".png", convert_bitdepth(cell_mask_crop, crop_bitdepth))
new_center = (crop_size // 2, crop_size // 2)
new_bbox = (
new_center[0] - h // 2,
new_center[1] - w // 2,
new_center[0] + h // 2,
new_center[1] + w // 2,
)
cell_bboxes.append(
{
"id" : output_prefix,
"cell": region.label,
"y1": new_bbox[0],
"x1": new_bbox[1],
"y2": new_bbox[2],
"x2": new_bbox[3],
}
)
return pd.DataFrame(cell_bboxes)