Skip to content

Commit cf9c67e

Browse files
jotasijnothman
authored andcommitted
DOC Change image in segmentation example (scikit-learn#10647)
* The Coins image is segmented more intuitively * Changed face->coin in the docs as well * Removed unused scipy import.
1 parent 8afeb5d commit cf9c67e

File tree

3 files changed

+36
-46
lines changed

3 files changed

+36
-46
lines changed

doc/modules/clustering.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -469,15 +469,15 @@ function of the gradient of the image.
469469
* :ref:`sphx_glr_auto_examples_cluster_plot_segmentation_toy.py`: Segmenting objects
470470
from a noisy background using spectral clustering.
471471

472-
* :ref:`sphx_glr_auto_examples_cluster_plot_face_segmentation.py`: Spectral clustering
473-
to split the image of the raccoon face in regions.
472+
* :ref:`sphx_glr_auto_examples_cluster_plot_coin_segmentation.py`: Spectral clustering
473+
to split the image of coins in regions.
474474

475-
.. |face_kmeans| image:: ../auto_examples/cluster/images/sphx_glr_plot_face_segmentation_001.png
476-
:target: ../auto_examples/cluster/plot_face_segmentation.html
475+
.. |coin_kmeans| image:: ../auto_examples/cluster/images/sphx_glr_plot_coin_segmentation_001.png
476+
:target: ../auto_examples/cluster/plot_coin_segmentation.html
477477
:scale: 65
478478

479-
.. |face_discretize| image:: ../auto_examples/cluster/images/sphx_glr_plot_face_segmentation_002.png
480-
:target: ../auto_examples/cluster/plot_face_segmentation.html
479+
.. |coin_discretize| image:: ../auto_examples/cluster/images/sphx_glr_plot_coin_segmentation_002.png
480+
:target: ../auto_examples/cluster/plot_coin_segmentation.html
481481
:scale: 65
482482

483483
Different label assignment strategies
@@ -495,7 +495,7 @@ geometrical shape.
495495
===================================== =====================================
496496
``assign_labels="kmeans"`` ``assign_labels="discretize"``
497497
===================================== =====================================
498-
|face_kmeans| |face_discretize|
498+
|coin_kmeans| |coin_discretize|
499499
===================================== =====================================
500500

501501
Spectral Clustering Graphs
@@ -631,12 +631,12 @@ merging to nearest neighbors as in :ref:`this example
631631
<sphx_glr_auto_examples_cluster_plot_agglomerative_clustering.py>`, or
632632
using :func:`sklearn.feature_extraction.image.grid_to_graph` to
633633
enable only merging of neighboring pixels on an image, as in the
634-
:ref:`raccoon face <sphx_glr_auto_examples_cluster_plot_face_ward_segmentation.py>` example.
634+
:ref:`coin <sphx_glr_auto_examples_cluster_plot_coin_ward_segmentation.py>` example.
635635

636636
.. topic:: Examples:
637637

638-
* :ref:`sphx_glr_auto_examples_cluster_plot_face_ward_segmentation.py`: Ward clustering
639-
to split the image of a raccoon face in regions.
638+
* :ref:`sphx_glr_auto_examples_cluster_plot_coin_ward_segmentation.py`: Ward clustering
639+
to split the image of coins in regions.
640640

641641
* :ref:`sphx_glr_auto_examples_cluster_plot_ward_structured_vs_unstructured.py`: Example of
642642
Ward algorithm on a swiss-roll, comparison of structured approaches

examples/cluster/plot_face_segmentation.py renamed to examples/cluster/plot_coin_segmentation.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
===================================================
3-
Segmenting the picture of a raccoon face in regions
4-
===================================================
2+
================================================
3+
Segmenting the picture of greek coins in regions
4+
================================================
55
66
This example uses :ref:`spectral_clustering` on a graph created from
77
voxel-to-voxel difference on an image to break this image into multiple
@@ -25,37 +25,32 @@
2525
import time
2626

2727
import numpy as np
28-
import scipy as sp
2928
from scipy.ndimage.filters import gaussian_filter
3029
import matplotlib.pyplot as plt
31-
from skimage import img_as_float
30+
from skimage.data import coins
3231
from skimage.transform import rescale
3332

3433
from sklearn.feature_extraction import image
3534
from sklearn.cluster import spectral_clustering
3635

3736

38-
# load the raccoon face as a numpy array
39-
try: # SciPy >= 0.16 have face in misc
40-
from scipy.misc import face
41-
orig_face = img_as_float(face(gray=True))
42-
except ImportError:
43-
orig_face = img_as_float(sp.face(gray=True))
37+
# load the coins as a numpy array
38+
orig_coins = coins()
4439

45-
# Resize it to 10% of the original size to speed up the processing
40+
# Resize it to 20% of the original size to speed up the processing
4641
# Applying a Gaussian filter for smoothing prior to down-scaling
4742
# reduces aliasing artifacts.
48-
smoothened_face = gaussian_filter(orig_face, sigma=4.5)
49-
rescaled_face = rescale(smoothened_face, 0.1, mode="reflect")
43+
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
44+
rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect")
5045

5146
# Convert the image into a graph with the value of the gradient on the
5247
# edges.
53-
graph = image.img_to_graph(rescaled_face)
48+
graph = image.img_to_graph(rescaled_coins)
5449

5550
# Take a decreasing function of the gradient: an exponential
5651
# The smaller beta is, the more independent the segmentation is of the
5752
# actual image. For beta=1, the segmentation is close to a voronoi
58-
beta = 5
53+
beta = 10
5954
eps = 1e-6
6055
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps
6156

@@ -71,10 +66,10 @@
7166
labels = spectral_clustering(graph, n_clusters=N_REGIONS,
7267
assign_labels=assign_labels, random_state=42)
7368
t1 = time.time()
74-
labels = labels.reshape(rescaled_face.shape)
69+
labels = labels.reshape(rescaled_coins.shape)
7570

7671
plt.figure(figsize=(5, 5))
77-
plt.imshow(rescaled_face, cmap=plt.cm.gray)
72+
plt.imshow(rescaled_coins, cmap=plt.cm.gray)
7873
for l in range(N_REGIONS):
7974
plt.contour(labels == l,
8075
colors=[plt.cm.spectral(l / float(N_REGIONS))])

examples/cluster/plot_face_ward_segmentation.py renamed to examples/cluster/plot_coin_ward_segmentation.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
2-
=========================================================================
3-
A demo of structured Ward hierarchical clustering on a raccoon face image
4-
=========================================================================
2+
======================================================================
3+
A demo of structured Ward hierarchical clustering on an image of coins
4+
======================================================================
55
66
Compute the segmentation of a 2D image with Ward hierarchical
77
clustering. The clustering is spatially constrained in order
@@ -17,12 +17,11 @@
1717
import time as time
1818

1919
import numpy as np
20-
import scipy as sp
2120
from scipy.ndimage.filters import gaussian_filter
2221

2322
import matplotlib.pyplot as plt
2423

25-
from skimage import img_as_float
24+
from skimage.data import coins
2625
from skimage.transform import rescale
2726

2827
from sklearn.feature_extraction.image import grid_to_graph
@@ -31,41 +30,37 @@
3130

3231
# #############################################################################
3332
# Generate data
34-
try: # SciPy >= 0.16 have face in misc
35-
from scipy.misc import face
36-
orig_face = img_as_float(face(gray=True))
37-
except ImportError:
38-
orig_face = img_as_float(sp.face(gray=True))
33+
orig_coins = coins()
3934

40-
# Resize it to 10% of the original size to speed up the processing
35+
# Resize it to 20% of the original size to speed up the processing
4136
# Applying a Gaussian filter for smoothing prior to down-scaling
4237
# reduces aliasing artifacts.
43-
smoothened_face = gaussian_filter(orig_face, sigma=4.5)
44-
rescaled_face = rescale(smoothened_face, 0.1, mode="reflect")
38+
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
39+
rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect")
4540

46-
X = np.reshape(rescaled_face, (-1, 1))
41+
X = np.reshape(rescaled_coins, (-1, 1))
4742

4843
# #############################################################################
4944
# Define the structure A of the data. Pixels connected to their neighbors.
50-
connectivity = grid_to_graph(*rescaled_face.shape)
45+
connectivity = grid_to_graph(*rescaled_coins.shape)
5146

5247
# #############################################################################
5348
# Compute clustering
5449
print("Compute structured hierarchical clustering...")
5550
st = time.time()
56-
n_clusters = 15 # number of regions
51+
n_clusters = 27 # number of regions
5752
ward = AgglomerativeClustering(n_clusters=n_clusters, linkage='ward',
5853
connectivity=connectivity)
5954
ward.fit(X)
60-
label = np.reshape(ward.labels_, rescaled_face.shape)
55+
label = np.reshape(ward.labels_, rescaled_coins.shape)
6156
print("Elapsed time: ", time.time() - st)
6257
print("Number of pixels: ", label.size)
6358
print("Number of clusters: ", np.unique(label).size)
6459

6560
# #############################################################################
6661
# Plot the results on an image
6762
plt.figure(figsize=(5, 5))
68-
plt.imshow(rescaled_face, cmap=plt.cm.gray)
63+
plt.imshow(rescaled_coins, cmap=plt.cm.gray)
6964
for l in range(n_clusters):
7065
plt.contour(label == l,
7166
colors=[plt.cm.spectral(l / float(n_clusters)), ])

0 commit comments

Comments
 (0)