Key Features & Explanation:
scikit-image
provides a comprehensive suite of image processing functions built on top of NumPy
arrays, making it efficient and easy to integrate with scientific computing and machine learning workflows. Below are some of its most powerful functionalities:
1. Image Loading & Display
One of the most fundamental tasks in image processing is loading an image from different sources (files, URLs, arrays) and displaying it properly. scikit-image
provides io.imread()
to read images, which works with various formats like PNG, JPEG, and TIFF.
Example: Loading an Image from a URL and Displaying it:
import matplotlib.pyplot as plt
from skimage import io
= io.imread('https://upload.wikimedia.org/wikipedia/commons/8/89/Portrait_Placeholder.png')
image
plt.imshow(image)'off')
plt.axis( plt.show()
Output: (Displays the loaded image)
- Key Functions:
io.imread()
,plt.imshow()
- Use Case: Reading and displaying images from different sources for further processing.
2. Image Preprocessing & Filtering
Before performing complex operations, it’s essential to preprocess images. This can include blurring, sharpening, or noise reduction to improve image quality.
Example: Applying a Gaussian Blur for Noise Reduction
from skimage.filters import gaussian
= gaussian(image, sigma=2, multichannel=True)
blurred_image
plt.imshow(blurred_image)'off')
plt.axis( plt.show()
Output: (Original vs. Blurred Image - noise is reduced)
- Key Functions:
gaussian()
- Use Case: Reducing noise before edge detection, smoothing images for better segmentation.
3. Edge Detection
Edge detection helps in extracting object boundaries from an image. The Canny Edge Detector is a powerful method that finds edges while reducing noise.
Example: Detecting Edges Using the Canny Algorithm
from skimage.color import rgb2gray
from skimage.feature import canny
= rgb2gray(image)
gray_image
= canny(gray_image, sigma=1)
edges
='gray')
plt.imshow(edges, cmap'off')
plt.axis( plt.show()
Output: (Edges of objects highlighted, making them more visible)
- Key Functions:
canny()
,rgb2gray()
- Use Case: Object detection, feature extraction, and shape recognition.
4. Image Transformation (Resizing, Rotating, Warping)
Sometimes, images need to be resized or transformed before applying machine learning models.
Example: Resizing an Image
from skimage.transform import resize
= resize(image, (100, 100))
resized_image
plt.imshow(resized_image)'off')
plt.axis( plt.show()
Output: (Smaller version of the original image, maintaining aspect ratio)
- Key Functions:
resize()
,rotate()
,warp()
- Use Case: Standardizing image sizes for ML models, reducing computational load.
5. Image Segmentation (Object Isolation)
Segmentation is used to separate objects within an image. The SLIC (Simple Linear Iterative Clustering) algorithm helps in superpixel segmentation, breaking the image into meaningful regions.
Example: Superpixel Segmentation Using SLIC
from skimage.segmentation import slic
from skimage.color import label2rgb
= slic(image, n_segments=100, compactness=10)
segments
= label2rgb(segments, image, kind='avg')
segmented_image
plt.imshow(segmented_image)'off')
plt.axis( plt.show()
Output: (Image divided into multiple colored superpixels)
- Key Functions:
slic()
,label2rgb()
- Use Case: Identifying separate objects within an image (e.g., medical imaging, object tracking).
6. Feature Extraction (Corner and Blob Detection)
Feature extraction identifies important key points in images, useful in facial recognition, motion tracking, and object detection.
Example: Detecting Corners Using Harris Detector
from skimage.feature import corner_harris, corner_peaks
= corner_peaks(corner_harris(gray_image), min_distance=5)
corners
='gray')
plt.imshow(gray_image, cmap1], corners[:, 0], color='red', s=10)
plt.scatter(corners[:, 'off')
plt.axis( plt.show()
Output: (Red dots highlight detected corners in the image)
- Key Functions:
corner_harris()
,corner_peaks()
- Use Case: Object tracking, motion analysis, and pattern re cognition.
7. Morphological Operations (Dilation, Erosion, Skeletonization)
Morphological operations help in enhancing or removing structures in an image. These are useful in text recognition and medical imaging.
Example: Applying Dilation and Erosion
from skimage.morphology import dilation, erosion, disk
= disk(3)
selem
= dilation(gray_image, selem)
dilated_image = erosion(gray_image, selem)
eroded_image
= plt.subplots(1, 2, figsize=(10, 5))
fig, ax 0].imshow(dilated_image, cmap='gray')
ax[0].set_title("Dilated Image")
ax[0].axis('off')
ax[
1].imshow(eroded_image, cmap='gray')
ax[1].set_title("Eroded Image")
ax[1].axis('off')
ax[
plt.show()
Output: (Left: Dilated Image - structures are expanded, Right: Eroded Image - structures are shrunk)
- Key Functions:
dilation()
,erosion()
,disk()
- Use Case: Noise removal, shape detection, and medical imaging analysis.
8. Color Space Conversions (RGB to Grayscale, HSV, LAB)
Converting between color spaces helps in color-based object detection and segmentation.
Example: Convert RGB to HSV
from skimage.color import rgb2hsv
= rgb2hsv(image)
hsv_image
plt.imshow(hsv_image)'off')
plt.axis( plt.show()
Output: (HSV representation of the image, useful for detecting color-based features)
- Key Functions:
rgb2gray()
,rgb2hsv()
,rgb2lab()
- Use Case: Color-based segmentation, object tracking, and image enhancement.
These examples showcase how scikit-image
simplifies powerful image processing techniques.