scikit-image: a tour
There are many tools and utilities in the package, far too many to cover in a tutorial. This notebook is designed as a road map, to guide you as you explore or search for additional tools for your applications. This is intended as a guide, not an exhaustive list.
Each submodule of scikit-image has its own section, which you can navigate to below in the table of contents.
skimage.color - color conversion
The color
submmodule includes routines to convert to and from common color representations. For example, RGB (Red, Green, and Blue) can be converted into many other representations.
Example: conversion to grayscale
Example: conversion to HSV
Usually, objects in images have distinct colors (hues) and luminosities, so that these features can be used to separate different areas of the image. In the RGB representation the hue and the luminosity are expressed as a linear combination of the R,G,B channels, whereas they correspond to single channels of the HSV image (the Hue and the Value channels). A simple segmentation of the image can then be effectively performed by a mere thresholding of the HSV channels. See below link for additional details.
https://en.wikipedia.org/wiki/HSL_and_HSV
We first load the RGB image and extract the Hue and Value channels:
The cup and saucer have a Hue distinct from the remainder of the image, which can be isolated by thresholding
An additional threshold in the value channel can remote most of the shadow
Additional color conversion examples available in the online gallery.
Back to the Table of Contents
skimage.data - test images
The data
submodule includes standard test images useful for examples and testing the package. These images are shipped with the package.
There are scientific images, general test images, and a stereoscopic image.
skimage.draw - drawing primitives on an image
The majority of functions in this submodule return the coordinates of the specified shape/object in the image, rather than drawing it on the image directly. The coordinates can then be used as a mask to draw on the image, or you pass the image as well as those coordinates into the convenience function draw.set_color
.
Lines and circles can be drawn with antialiasing (these functions end in the suffix *_aa).
At the current time text is not supported; other libraries including matplotlib have robust support for overlaying text.
Example: drawing shapes
skimage.exposure - evaluating or changing the exposure of an image
One of the most common tools to evaluate exposure is the histogram, which plots the number of points which have a certain value against the values in order from lowest (dark) to highest (light). The function exposure.histogram
differs from numpy.histogram
in that there is no rebinnning; each value along the x-axis is preserved.
Example: Histogram equalization
Additional examples available in the example gallery
Back to the Table of Contents
skimage.feature - extract features from an image
This submodule presents a diverse set of tools to identify or extract certain features from images, including tools for
Edge detection
feature.canny
Corner detection
feature.corner_kitchen_rosenfeld
feature.corner_harris
feature.corner_shi_tomasi
feature.corner_foerstner
feature.subpix
feature.corner_moravec
feature.corner_fast
feature.corner_orientations
Blob detection
feature.blob_dog
feature.blob_doh
feature.blob_log
Texture
feature.greycomatrix
feature.greycoprops
feature.local_binary_pattern
feature.multiblock_lbp
Peak finding
feature.peak_local_max
Object detction
feature.hog
feature.match_template
Stereoscopic depth estimation
feature.daisy
Feature matching
feature.ORB
feature.BRIEF
feature.CENSURE
feature.match_descriptors
feature.plot_matches
This is a large submodule. For brevity here is a short example illustrating ORB feature matching, and additional examples can be explored in the online gallery.
Additional feature detection and extraction examples available in the online gallery.
skimage.filters - apply filters to an image
Filtering applies whole-image modifications such as sharpening or blurring. Thresholding methods also live in this submodule.
Notable functions include (links to relevant gallery examples)
filters.threshold_* (multiple different functions with this prefix)
skimage.filters.try_all_threshold to compare various methods
filters.sobel
filters.prewitt
filters.scharr
filters.roberts
filters.laplace
filters.hessian
filters.meijering
filters.sato
filters.frangi
Inverse filtering (see also skimage.restoration)
filters.weiner
filters.inverse
filters.gabor
Blurring/denoising
filters.gaussian
filters.median
filters.unsharp_mask
Define your own
LPIFilter2D
Rank filters
There is a sub-submodule, skimage.filters.rank
, which contains rank filters. These filters are nonlinear and operate on the local histogram.
To learn more about the rank filters, see the comprehensive gallery example for rank filters.
Additional feature detection and extraction examples available in the online gallery.
Back to the Table of Contents
skimage.future - stable code with unstable API
Bleeding edge features which work well, and will be moved from here into the main package in future releases. However, on the way their API may change.
Back to the Table of Contents
skimage.graph - graph theory, minimum cost paths
Graph theory. Currently this submodule primarily deals with a constructed "cost" image, and how to find the minimum cost path through it, with constraints if desired.
The panorama tutorial lecture illustrates a real-world example.
skimage.io - utilities to read and write images in various formats
Reading your image and writing the results back out. There are multiple plugins available, which support multiple formats. The most commonly used functions include
io.imread - Read an image to a numpy array.
io.imsave - Write an image to disk.
io.imread_collection - Read multiple images which match a common prefix
skimage.measure - measuring image or region properties
Multiple algorithms to label images, or obtain information about discrete regions of an image.
Label an image
measure.label
In a labeled image (image with discrete regions identified by unique integers, as returned by
label
), find various properties of the labeled regions.regionprops
is extremely usefulmeasure.regionprops
Finding paths from a 2D image, or isosurfaces from a 3D image
measure.find_contours
measure.marching_cubes_lewiner
measure.marching_cubes_classic
measure.mesh_surface_area (surface area of 3D mesh from marching cubes)
Quantify the difference between two whole images (often used in denoising or restoration)
measure.compare_*
RANDom Sample Consensus fitting (RANSAC) - a powerful, robust approach to fitting a model to data. It exists here because its initial use was for fitting shapes, but it can also fit transforms.
measure.ransac
measure.CircleModel
measure.EllipseModel
measure.LineModelND
skimage.morphology - binary and grayscale morphology
Morphological image processing is a collection of non-linear operations related to the shape or morphology of features in an image, such as boundaries, skeletons, etc. In any given technique, we probe an image with a small shape or template called a structuring element, which defines the region of interest or neighborhood around a pixel.
Example: Flood filling
Flood fill is an algorithm to iteratively identify and/or change adjacent values in an image based on their similarity to an initial seed point. The conceptual analogy is the ‘paint bucket’ tool in many graphic editors.
The flood
function returns the binary mask of the flooded area. flood_fill
returns a modified image. Both of these can be set with a tolerance
keyword argument, within which the adjacent region will be filled.
Here we will experiment a bit on the cameraman, turning his coat from dark to light.
Example: Binary and grayscale morphology
Here we outline the following basic morphological operations:
Erosion
Dilation
Opening
Closing
White Tophat
Black Tophat
Skeletonize
Convex Hull
To get started, let’s load an image using io.imread
. Note that morphology functions only work on gray-scale or binary images, so we set as_gray=True
.
Erosion
Morphological erosion
sets a pixel at (i, j) to the minimum over all pixels in the neighborhood centered at (i, j). Erosion shrinks bright regions and enlarges dark regions.
The structuring element, selem
, passed to erosion is a boolean array that describes this neighborhood. Below, we use disk
to create a circular structuring element, which we use for most of the following examples.
Dilation
Morphological dilation
sets a pixel at (i, j) to the maximum over all pixels in the neighborhood centered at (i, j). Dilation enlarges bright regions and shrinks dark regions.
Notice how the white boundary of the image thickens, or gets dilated, as we increase the size of the disk. Also notice the decrease in size of the two black ellipses in the centre, and the thickening of the light grey circle in the center and the 3 patches in the lower part of the image.
Opening
Morphological opening
on an image is defined as an erosion followed by a dilation. Opening can remove small bright spots (i.e. “salt”) and connect small dark cracks.
Since opening an image starts with an erosion operation, light regions that are smaller than the structuring element are removed. The dilation operation that follows ensures that light regions that are larger than the structuring element retain their original size. Notice how the light and dark shapes in the center their original thickness but the 3 lighter patches in the bottom get completely eroded. The size dependence is highlighted by the outer white ring: The parts of the ring thinner than the structuring element were completely erased, while the thicker region at the top retains its original thickness.
Closing
Morphological closing
on an image is defined as a dilation followed by an erosion. Closing can remove small dark spots (i.e. “pepper”) and connect small bright cracks.
To illustrate this more clearly, let’s add a small crack to the white border:
Since closing an image starts with an dilation operation, dark regions that are smaller than the structuring element are removed. The dilation operation that follows ensures that dark regions that are larger than the structuring element retain their original size. Notice how the white ellipses at the bottom get connected because of dilation, but other dark region retain their original sizes. Also notice how the crack we added is mostly removed.
White tophat
The white_tophat
of an image is defined as the image minus its morphological opening. This operation returns the bright spots of the image that are smaller than the structuring element.
To make things interesting, we’ll add bright and dark spots to the image:
As you can see, the 10-pixel wide white square is highlighted since it is smaller than the structuring element. Also, the thin, white edges around most of the ellipse are retained because they’re smaller than the structuring element, but the thicker region at the top disappears.
Black tophat
The black_tophat
of an image is defined as its morphological closing minus the original image. This operation returns the dark spots of the image that are smaller than the structuring element.
As you can see, the 10-pixel wide black square is highlighted since it is smaller than the structuring element.
Duality
As you should have noticed, many of these operations are simply the reverse of another operation. This duality can be summarized as follows:
Erosion <-> Dilation
Opening <-> Closing
White tophat <-> Black tophat
Skeletonize
Thinning is used to reduce each connected component in a binary image to a single-pixel wide skeleton. It is important to note that this is performed on binary images only.
As the name suggests, this technique is used to thin the image to 1-pixel wide skeleton by applying thinning successively.
Convex hull
The convex_hull_image is the set of pixels included in the smallest convex polygon that surround all white pixels in the input image. Again note that this is also performed on binary images.
skimage.restoration - restoration of an image
This submodule includes routines to restore images. Currently these routines fall into four major categories. Links lead to topical gallery examples.
restoration.denoise_*
Deconvolution, or reversing a convolutional effect which applies to the entire image. For example, lens correction. This can be done unsupervised.
restoration.weiner
restoration.unsupervised_weiner
restoration.richardson_lucy
Inpainting, or filling in missing areas of an image
restoration.inpaint_biharmonic
restoration.unwrap_phase
skimage.segmentation - identification of regions of interest
One of the key image analysis tasks is identifying regions of interest. These could be a person, an object, certain features of an animal, microscopic image, or stars. Segmenting an image is the process of determining where these things you want are in your images.
Segmentation has two overarching categories: Supervised and Unsupervised.
Supervised - must provide some guidance (seed points or initial conditions)
segmentation.random_walker
segmentation.active_contour
segmentation.watershed
segmentation.flood_fill
segmentation.flood
some thresholding algorithms in
filters
Unsupervised - no human input
segmentation.slic
segmentation.felzenszwalb
segmentation.chan_vese
some thresholding algorithms in
filters
There is a segmentation lecture (and solution) you may peruse, as well as many gallery examples which illustrate all of these segmentation methods.
skimage.transform - transforms & warping
This submodule has multiple features which fall under the umbrella of transformations.
Forward (radon
) and inverse (iradon
) radon transforms, as well as some variants (iradon_sart
) and the finite versions of these transforms (frt2
and ifrt2
). These are used for reconstructing medical computed tomography (CT) images.
Hough transforms for identifying lines, circles, and ellipses.
Changing image size, shape, or resolution with resize
, rescale
, or downscale_local_mean
.
warp
, and warp_coordinates
which take an image or set of coordinates and translate them through one of the defined *Transforms
in this submodule. estimate_transform
may be assist in estimating the parameters.
Numerous gallery examples are available illustrating these functions. The panorama tutorial also includes warping via SimilarityTransform
with parameter estimation via measure.ransac
.
skimage.util - utility functions
These are generally useful functions which have no definite other place in the package.
util.img_as_*
are convenience functions for datatype conversion.
util.invert
is a convenient way to invert any image, accounting for its datatype.
util.random_noise
is a comprehensive function to apply any amount of many different types of noise to images. The seed may be set, resulting in pseudo-random noise for testing.
util.view_as_*
allows for overlapping views into the same memory array, which is useful for elegant local computations with minimal memory impact.
util.apply_parallel
uses Dask to apply a function across subsections of an image. This can result in dramatic performance or memory improvements, but depending on the algorithm edge effects or lack of knowledge of the remainder of the image may result in unexpected results.
util.pad
and util.crop
pads or crops the edges of images. util.pad
is now a direct wrapper for numpy.pad
.