-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
78 lines (63 loc) · 2.47 KB
/
utils.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
import skimage.exposure as exp
from scipy import ndimage as ndi
from functools import partial
from skimage.segmentation import watershed
import numpy as np
from skimage.measure import regionprops_table, label
import pandas as pd
def adjust(frame, method, **kwargs):
if method=="equalize":
adjusted=exp.equalize_hist(frame, **kwargs)
elif method=="gamma":
adjusted=exp.adjust_gamma(frame, **kwargs)
elif method=="log":
adjusted=exp.adjust_log(frame, **kwargs)
elif method=="sigmoid":
adjusted=exp.adjust_sigmoid(frame, **kwargs)
elif method=="adaptive":
adjusted=exp.equalize_adapthist(frame, **kwargs)
else:
raise ValueError("method can be equalize, gamma, log, sigmoid or adaptive")
return adjusted
def curry(orig_func, **kwargs):
newfunc=partial(orig_func, **kwargs)
return newfunc
def apply_watershed(frame, threshold, **kwargs):
markers=np.zeros_like(frame)
if type(threshold)==np.ndarray:
markers[frame < threshold[0]] = 1
markers[frame > threshold[len(threshold) - 2]] = 2
else:
markers[frame < threshold] = 1
markers[frame > threshold] = 2
mask=watershed(frame, markers, **kwargs)
return mask
#TODO I'm not calculating anything here
def calculate_properties(mask, image, properties=None, to_cache=True, fill_holes=False, min_size=20000,
get_largest=False):
if fill_holes:
mask = ndi.binary_fill_holes(mask-1)
labels=label(mask)
else:
labels=label(mask)
intensities=[]
for lab in np.unique(labels):
if lab == 0:
continue
pix_sum = np.sum(image[labels == lab])
intensities.append(pix_sum)
if properties is None:
properties=["label", "area", "bbox_area", "convex_area", "eccentricity", "extent", "local_centroid",
"major_axis_length", "minor_axis_length", "perimeter", "solidity",
"weighted_local_centroid", "orientation"]
else:
properties.insert(0, "label")
attrs=regionprops_table(label_image=labels, intensity_image=image, properties=properties,
cache=to_cache)
attrs = pd.DataFrame(attrs)
attrs.insert(1, "intensities", intensities)
if not get_largest:
attrs=attrs[attrs["area"] > min_size]
else:
attrs=attrs[attrs["area"]==attrs["area"].max()]
return pd.DataFrame(attrs)