Scikit-image is a collection of algorithms for image processing built on top of NumPy and SciPy. It’s designed to work with 2D and 3D images and is particularly useful for scientific imaging applications.
Why scikit-image?
- Comprehensive: Wide range of algorithms from basic filtering to advanced segmentation
- Scientific Focus: Designed for scientific and research applications
- NumPy Integration: Works naturally with NumPy arrays
- Well-Documented: Extensive documentation with examples
- Open Source: BSD license, community-driven development
Key Features
Image Filtering
from skimage import filters, io
import numpy as np
# Load image
image = io.imread('microscopy_image.tif')
# Gaussian smoothing
smoothed = filters.gaussian(image, sigma=2)
# Edge detection
edges = filters.sobel(image)
Segmentation
from skimage import segmentation, morphology
from skimage.feature import peak_local_max
from scipy import ndimage
# Watershed segmentation for cell detection
distance = ndimage.distance_transform_edt(binary_mask)
local_maxima = peak_local_max(distance, min_distance=10, labels=binary_mask)
markers = ndimage.label(local_maxima)[0]
labels = segmentation.watershed(-distance, markers, mask=binary_mask)
Morphological Operations
from skimage import morphology
# Remove small objects
cleaned = morphology.remove_small_objects(binary_image, min_size=50)
# Fill holes
filled = morphology.remove_small_holes(binary_image, area_threshold=64)
# Disk structuring element
selem = morphology.disk(3)
opened = morphology.opening(binary_image, selem)
Common Workflows in Calcium Imaging
Cell Segmentation Pipeline
from skimage import filters, morphology, segmentation, measure
# 1. Smooth image
smoothed = filters.gaussian(image, sigma=1.5)
# 2. Threshold
threshold = filters.threshold_otsu(smoothed)
binary = smoothed > threshold
# 3. Clean up
cleaned = morphology.remove_small_objects(binary, min_size=20)
cleaned = morphology.remove_small_holes(cleaned, area_threshold=50)
# 4. Watershed segmentation
distance = ndimage.distance_transform_edt(cleaned)
local_max = peak_local_max(distance, min_distance=5, labels=cleaned)
markers = ndimage.label(local_max)[0]
labels = segmentation.watershed(-distance, markers, mask=cleaned)
# 5. Extract properties
props = measure.regionprops(labels, intensity_image=image)
for prop in props:
print(f"Cell area: {prop.area}, Mean intensity: {prop.mean_intensity}")
Useful Modules
- filters: Gaussian, median, edge detection, thresholding
- morphology: Opening, closing, erosion, dilation, skeletonization
- segmentation: Watershed, active contours, region growing
- measure: Region properties, label detection, perimeter measurement
- feature: Blob detection, corner detection, peak finding
- exposure: Histogram equalization, contrast adjustment
- transform: Rescaling, rotation, Hough transform
Integration with Research Workflows
With tifffile
import tifffile
from skimage import filters
# Load multi-page TIFF
stack = tifffile.imread('calcium_recording.tif')
# Process each frame
filtered_stack = np.array([filters.gaussian(frame, sigma=1) for frame in stack])
With NumPy and Matplotlib
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original')
axes[1].imshow(filtered, cmap='gray')
axes[1].set_title('Filtered')
axes[2].imshow(labels, cmap='nipy_spectral')
axes[2].set_title('Segmented')
Installation
pixi add scikit-image
# or
conda install -c conda-forge scikit-image
# or
pip install scikit-image
Best Practices
- Start with visualization to understand your data
- Apply appropriate pre-processing (smoothing, contrast adjustment)
- Test threshold methods on representative samples
- Validate segmentation results visually
- Use regionprops for quantitative measurements
- Consider 3D operations for volumetric data
- Benchmark different approaches for your specific use case