Files
scikit-image/doc/examples/segmentation/plot_label.py
T
emmanuelle a5a771a8e4 Modified label function so that background pixels are labeled with 0, and
background=0 by default.

Modified label function so that background pixels are labeled with 0, and
background=0 by default. All tests of _ccomp.pyx pass

Modified a couple of files to be consistent with the new behavior of
measure.label

Modified doctring of label to pass doctest

Modified TODO.txt as well as release notes to mention the new behavior of
label.

Typo in docstring

Typo in docstring

Changed default value of kw argument background in measure.label

Removed unnecessary and outdated comment
2016-02-22 21:19:23 +01:00

58 lines
1.5 KiB
Python

"""
===================
Label image regions
===================
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
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from skimage import data
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label
from skimage.morphology import closing, square
from skimage.measure import regionprops
from skimage.color import label2rgb
image = data.coins()[50:-50, 50:-50]
# apply threshold
thresh = threshold_otsu(image)
bw = closing(image > thresh, square(3))
# remove artifacts connected to image border
cleared = bw.copy()
clear_border(cleared)
# label image regions
label_image = label(cleared)
image_label_overlay = label2rgb(label_image, image=image)
fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6))
ax.imshow(image_label_overlay)
for region in regionprops(label_image):
# skip small images
if region.area < 100:
continue
# draw rectangle around segmented coins
minr, minc, maxr, maxc = region.bbox
rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
fill=False, edgecolor='red', linewidth=2)
ax.add_patch(rect)
plt.show()