forked from CellProfiler/CellProfiler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedgedetection.py
100 lines (69 loc) · 2.5 KB
/
edgedetection.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
# coding=utf-8
"""
Detect edges in an image or volume using the Sobel transform.
Multi-channel images are converted to grayscale before the transform is
applied. An edge is a region in which intensity changes dramatically.
For example, an edge is the line between a dark background and a bright
foreground.
"""
import cellprofiler.image
import cellprofiler.module
import cellprofiler.setting
import numpy
import skimage.color
import skimage.filters
class EdgeDetection(cellprofiler.module.ImageProcessing):
category = "Feature Detection"
module_name = "EdgeDetection"
variable_revision_number = 1
def create_settings(self):
super(EdgeDetection, self).create_settings()
self.mask = cellprofiler.setting.ImageNameSubscriber(
u"Mask",
can_be_blank=True,
doc="""
Optional. A binary image the same shape as "Input". Limit application of the edge filter to unmasked
regions of "Input".
"""
)
def settings(self):
__settings__ = super(EdgeDetection, self).settings()
return __settings__ + [
self.mask
]
def visible_settings(self):
__settings__ = super(EdgeDetection, self).visible_settings()
return __settings__ + [
self.mask
]
def run(self, workspace):
x_name = self.x_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)
mask_data = None
if not self.mask.is_blank:
mask_name = self.mask.value
mask = images.get_image(mask_name)
mask_data = mask.pixel_data
dimensions = x.dimensions
if dimensions == 2:
y_data = skimage.filters.sobel(x_data, mask=mask_data)
else:
y_data = numpy.zeros_like(x_data)
for plane, image in enumerate(x_data):
plane_mask = None if mask_data is None else mask_data[plane]
y_data[plane] = skimage.filters.sobel(image, mask=plane_mask)
y = cellprofiler.image.Image(
image=y_data,
parent_image=x,
dimensions=dimensions
)
y_name = self.y_name.value
images.add(y_name, y)
if self.show_window:
workspace.display_data.x_data = x_data
workspace.display_data.y_data = y_data
workspace.display_data.dimensions = dimensions