scikit-image

Language: Python

Data Science

scikit-image was developed as part of the scikit-learn ecosystem to provide easy-to-use image processing tools in Python. It integrates closely with NumPy arrays and scientific Python libraries, making it popular for academic research, prototyping, and real-world image processing applications.

scikit-image is a Python library for image processing that provides a collection of algorithms for segmentation, geometric transformations, color space manipulation, filtering, morphology, feature detection, and more.

Installation

pip: pip install scikit-image
conda: conda install -c conda-forge scikit-image

Usage

scikit-image provides functions for reading and writing images, performing geometric and color transformations, applying filters, detecting features, and extracting image statistics. It works seamlessly with NumPy arrays for numerical computations and can be combined with Matplotlib for visualization.

Reading and displaying an image

from skimage import io
img = io.imread('image.jpg')
io.imshow(img)
io.show()

Reads an image from a file and displays it using scikit-image’s I/O functions.

Converting to grayscale

from skimage.color import rgb2gray
gray_img = rgb2gray(img)
io.imshow(gray_img)
io.show()

Converts an RGB image to grayscale using `rgb2gray`.

Edge detection using Canny

from skimage import feature
edges = feature.canny(gray_img)
io.imshow(edges)
io.show()

Detects edges in a grayscale image using the Canny algorithm.

Resizing an image

from skimage.transform import resize
resized_img = resize(img, (200, 200))
io.imshow(resized_img)
io.show()

Resizes the input image to 200x200 pixels.

Applying a Gaussian filter

from skimage.filters import gaussian
smoothed_img = gaussian(gray_img, sigma=1)
io.imshow(smoothed_img)
io.show()

Applies Gaussian smoothing to reduce noise in an image.

Labeling connected components

from skimage.measure import label
labeled_img = label(gray_img > 0.5)
io.imshow(labeled_img)
io.show()

Labels connected regions in a binary image.

Error Handling

ValueError: image dtype not supported: Convert images to supported dtypes (e.g., float or uint8) using `img_as_float` or `img_as_ubyte`.
IndexError: tuple index out of range: Check the image shape and ensure operations are applied to valid dimensions.
ImportError: No module named 'skimage': Install scikit-image using pip or conda in your current Python environment.

Best Practices

Use NumPy arrays as the primary image representation for efficiency.

Normalize images to float in [0, 1] when using scikit-image filters.

Leverage built-in visualization functions or Matplotlib for displaying results.

Use modular functions to chain processing steps cleanly.

Handle images with varying shapes and channels appropriately.