Skip to content

Commit

Permalink
example with interactive exploration of properties obtained with regi…
Browse files Browse the repository at this point in the history
…onprops (#5010)

* example with interactive exploration of properties obtained with regionprops

* Update doc/examples/segmentation/plot_regionprops.py
  • Loading branch information
emmanuelle authored Oct 8, 2020
1 parent ac5e3b4 commit 944bf95
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
44 changes: 42 additions & 2 deletions doc/examples/segmentation/plot_regionprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
=========================
This example shows how to measure properties of labelled image regions. We
analyze an image with two ellipses.
first analyze an image with two ellipses. Below we show how to explore
interactively the properties of labelled objects.
"""
import math
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -76,3 +76,43 @@
# dict.

pd.DataFrame(props)

#####################################################################
# It is also possible to explore interactively the properties of labelled
# objects by visualizing them in the hover information of the labels.
# This example uses plotly in order to display properties when
# hovering over the objects.

import plotly.express as px
import plotly.graph_objects as go
from skimage import data, filters, measure, morphology

img = data.coins()
# Binary image, post-process the binary mask and compute labels
threshold = filters.threshold_otsu(img)
mask = img > threshold
mask = morphology.remove_small_objects(mask, 50)
mask = morphology.remove_small_holes(mask, 50)
labels = measure.label(mask)

fig = px.imshow(img, binary_string=True)
fig.update_traces(hoverinfo='skip') # hover is only for label info

props = measure.regionprops(labels, img)
properties = ['area', 'eccentricity', 'perimeter', 'mean_intensity']

# For each label, add a filled scatter trace for its contour,
# and display the properties of the label in the hover of this trace.
for index in range(1, labels.max()):
label = props[index].label
contour = measure.find_contours(labels == label, 0.5)[0]
y, x = contour.T
hoverinfo = ''
for prop_name in properties:
hoverinfo += f'<b>{prop_name}: {getattr(props[index], prop_name):.2f}</b><br>'
fig.add_trace(go.Scatter(
x=x, y=y, name=label,
mode='lines', fill='toself', showlegend=False,
hovertemplate=hoverinfo, hoveron='points+fills'))

fig
3 changes: 3 additions & 0 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
major, minor = v.release[:2]
binder_branch = 'v{}.{}.x'.format(major, minor)

# set plotly renderer to capture _repr_html_ for sphinx-gallery
import plotly.io as pio
pio.renderers.default = 'sphinx_gallery'

sphinx_gallery_conf = {
'doc_module': ('skimage',),
Expand Down
1 change: 1 addition & 0 deletions requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ pooch>=0.5.2
tifffile>=2020.5.30
myst-parser
ipywidgets
plotly>=4.8.0

0 comments on commit 944bf95

Please sign in to comment.