From 4e3895b44b63c2ff9e0555143d14ac2d6b739966 Mon Sep 17 00:00:00 2001 From: emmanuelle Date: Thu, 8 Dec 2011 11:04:34 +0100 Subject: [PATCH] ENH contours of segmentation in segmentation tutorial --- .../applications/plot_coins_segmentation.py | 17 +++++++++++++++-- doc/source/user_guide/segmentation.txt | 6 +++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/doc/examples/applications/plot_coins_segmentation.py b/doc/examples/applications/plot_coins_segmentation.py index 5b3c9de7..f572b5b6 100644 --- a/doc/examples/applications/plot_coins_segmentation.py +++ b/doc/examples/applications/plot_coins_segmentation.py @@ -107,6 +107,7 @@ elevation_map = sobel(coins) segmentation = watershed(elevation_map, markers) + plt.figure(figsize=(7, 3)) plt.subplot(131) plt.imshow(markers, cmap=plt.cm.spectral, interpolation='nearest') @@ -124,12 +125,24 @@ plt.title('segmentation') plt.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0, right=1) +# ------------------- Removing a few small holes --------------------- + +segmentation = ndimage.binary_fill_holes(segmentation - 1) #------------------ Labeling the coins -------------------------------- -labeled_coins, _ = ndimage.label(segmentation - 1) +labeled_coins, _ = ndimage.label(segmentation) -plt.figure(figsize=(6, 4)) +plt.figure(figsize=(6, 3)) +plt.subplot(121) +plt.imshow(coins, cmap=plt.cm.gray, interpolation='nearest') +plt.contour(segmentation, [0.5], linewidths=1.2, colors='y') +plt.axis('off') +plt.subplot(122) plt.imshow(labeled_coins, cmap=plt.cm.spectral, interpolation='nearest') plt.axis('off') +plt.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0, + right=1) + + plt.show() diff --git a/doc/source/user_guide/segmentation.txt b/doc/source/user_guide/segmentation.txt index fe820612..b61f3025 100644 --- a/doc/source/user_guide/segmentation.txt +++ b/doc/source/user_guide/segmentation.txt @@ -131,9 +131,13 @@ markers for the background were not well distributed, the barriers in the elevation map were high enough for these markers to flood the entire background. +We remove a few small holes with mathematical morphology:: + + >>> segmentation = ndimage.binary_fill_holes(segmentation - 1) + We can now label all the coins one by one using ``ndimage.label``:: - >>> labeled_coins, _ = ndimage.label(segmentation - 1) + >>> labeled_coins, _ = ndimage.label(segmentation) .. image:: ../../_images/plot_coins_segmentation_5.png :target: ../auto_examples/applications/plot_coins_segmentation.html