From da3f2b5f4d6993130299bd54bf8f56e0004861b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Thu, 30 Aug 2012 22:00:43 +0200 Subject: [PATCH 1/4] Add example script for image labelling --- doc/examples/plot_label.py | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 doc/examples/plot_label.py diff --git a/doc/examples/plot_label.py b/doc/examples/plot_label.py new file mode 100644 index 00000000..7b2fd717 --- /dev/null +++ b/doc/examples/plot_label.py @@ -0,0 +1,49 @@ +""" +=================== +Label image regions +=================== + +This example shows how to segment an image with image labelling. + +""" + +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.measure import regionprops + + +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) + +# remove artifacts connected to image border +cleared = bw.copy() +clear_border(cleared) + +# label image regions +label_image = label(cleared) + +for region in regionprops(label_image, ['Area', 'BoundingBox']): + + # skip small images + if region['Area'] < 100: + continue + + # draw rectangle around segmented coins + minr, minc, maxr, maxc = region['BoundingBox'] + rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr, + fill=False, edgecolor='red', linewidth=2) + ax.add_patch(rect) + +plt.show() 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 2/4] 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']): From c9291718f9912a1ce402a47a828a5e0bad8ed74e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Fri, 31 Aug 2012 22:30:09 +0200 Subject: [PATCH 3/4] Update description with more detailed explanation of applied steps --- doc/examples/plot_label.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/examples/plot_label.py b/doc/examples/plot_label.py index ab57d8e9..e9e47831 100644 --- a/doc/examples/plot_label.py +++ b/doc/examples/plot_label.py @@ -3,7 +3,13 @@ Label image regions =================== -This example shows how to segment an image with image labelling. +This example shows how to segment an image with image labelling. The following +steps are applied: + +1. Thresholding with automatic Otsu method +2. Close small holes with binary closing +3. Remove artifacts touching image border +4. Measure image regions to filter small objects """ From f360316f17c6fbc36a3ab3d76f676d4cb6466d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Fri, 31 Aug 2012 22:55:24 +0200 Subject: [PATCH 4/4] Explicitly define colormap --- doc/examples/plot_label.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/examples/plot_label.py b/doc/examples/plot_label.py index e9e47831..8c46cb8e 100644 --- a/doc/examples/plot_label.py +++ b/doc/examples/plot_label.py @@ -40,7 +40,7 @@ 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) +ax.imshow(label_image, cmap='jet') for region in regionprops(label_image, ['Area', 'BoundingBox']):