forked from CellProfiler/CellProfiler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomwalkeralgorithm.py
114 lines (78 loc) · 2.86 KB
/
randomwalkeralgorithm.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
# coding=utf-8
"""
Random walker algorithm
Single-channel images can be two-or-three-dimensional.
"""
import numpy
import skimage.color
import skimage.measure
import skimage.segmentation
import cellprofiler.module
import cellprofiler.object
import cellprofiler.setting
class RandomWalkerAlgorithm(cellprofiler.module.ImageSegmentation):
module_name = "Random walker algorithm"
variable_revision_number = 1
def create_settings(self):
super(RandomWalkerAlgorithm, self).create_settings()
self.first_phase = cellprofiler.setting.Float(
doc="First phase demarcates an image’s first phase.",
text="First phase",
value=0.5
)
self.second_phase = cellprofiler.setting.Float(
doc="Second phase demarcates an image’s second phase.",
text="Second phase",
value=0.5
)
self.beta = cellprofiler.setting.Float(
doc="""
Beta is the penalization coefficient for the random walker motion. Increasing the penalization
coefficient increases the difficulty of the diffusion. Likewise, decreasing the penalization coefficient
decreases the difficulty of the diffusion.
""",
text="Beta",
value=130.0
)
def settings(self):
__settings__ = super(RandomWalkerAlgorithm, self).settings()
return __settings__ + [
self.first_phase,
self.second_phase,
self.beta
]
def visible_settings(self):
__settings__ = super(RandomWalkerAlgorithm, self).settings()
return __settings__ + [
self.first_phase,
self.second_phase,
self.beta
]
def run(self, workspace):
x_name = self.x_name.value
y_name = self.y_name.value
images = workspace.image_set
x = images.get_image(x_name)
x_data = x.pixel_data
if x.multichannel:
x_data = skimage.color.rgb2gray(x_data)
labels_data = numpy.zeros_like(x_data, numpy.uint8)
labels_data[x_data > self.first_phase.value] = 1
labels_data[x_data < self.second_phase.value] = 2
y_data = skimage.segmentation.random_walker(
beta=self.beta.value,
data=x_data,
labels=labels_data,
multichannel=False,
spacing=x.spacing
)
y_data = skimage.measure.label(y_data)
objects = cellprofiler.object.Objects()
objects.segmented = y_data
objects.parent_image = x
workspace.object_set.add_objects(objects, y_name)
self.add_measurements(workspace)
if self.show_window:
workspace.display_data.x_data = x_data
workspace.display_data.y_data = y_data
workspace.display_data.dimensions = x.dimensions