Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96178
License: OTHER
1
import matplotlib.pyplot as plt
2
3
from skimage import data
4
from skimage import filter
5
from skimage import morphology
6
from skimage.viewer import ImageViewer
7
from skimage.viewer.widgets import history
8
from skimage.viewer.plugins.labelplugin import LabelPainter
9
10
11
class OKCancelButtons(history.OKCancelButtons):
12
13
def update_original_image(self):
14
# OKCancelButtons updates the original image with the filtered image
15
# by default. Override this method to update the overlay.
16
self.plugin._show_watershed()
17
self.plugin.close()
18
19
20
class WatershedPlugin(LabelPainter):
21
22
def help(self):
23
helpstr = ("Watershed plugin",
24
"----------------",
25
"Use mouse to paint each region with a different label.",
26
"Press OK to display segmented image.")
27
return '\n'.join(helpstr)
28
29
def _show_watershed(self):
30
viewer = self.image_viewer
31
edge_image = filter.sobel(viewer.image)
32
labels = morphology.watershed(edge_image, self.paint_tool.overlay)
33
viewer.ax.imshow(labels, cmap=plt.cm.jet, alpha=0.5)
34
viewer.redraw()
35
36
37
image = data.coins()
38
plugin = WatershedPlugin()
39
plugin += OKCancelButtons()
40
41
viewer = ImageViewer(image)
42
viewer += plugin
43
viewer.show()
44
45