From 6e9d6e28574ce8fe4eb7c26396e7fbc1d0ab5d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Fri, 31 Aug 2012 22:25:10 +0200 Subject: [PATCH] Improve visualization of labelling --- doc/examples/plot_label.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/doc/examples/plot_label.py b/doc/examples/plot_label.py index 7b2fd717..ab57d8e9 100644 --- a/doc/examples/plot_label.py +++ b/doc/examples/plot_label.py @@ -7,13 +7,14 @@ This example shows how to segment an image with image labelling. """ +import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as mpatches from skimage import data from skimage.filter import threshold_otsu from skimage.segmentation import clear_border -from skimage.morphology import label +from skimage.morphology import label, closing, square from skimage.measure import regionprops @@ -21,11 +22,7 @@ image = data.coins()[50:-50, 50:-50] # apply threshold thresh = threshold_otsu(image) -bw = image > thresh - -fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) -plt.gray() -ax.imshow(bw) +bw = closing(image > thresh, square(3)) # remove artifacts connected to image border cleared = bw.copy() @@ -33,6 +30,11 @@ clear_border(cleared) # label image regions label_image = label(cleared) +borders = np.logical_xor(bw, cleared) +label_image[borders] = -1 + +fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) +ax.imshow(label_image) for region in regionprops(label_image, ['Area', 'BoundingBox']):