Python Advanced - Data Science with SciKit-Image
Performing advanced image processing and analysis using SciKit-Image
SciKit-Image is an open-source image processing library for Python that provides a collection of algorithms for image processing and computer vision. It is built on top of NumPy, SciPy, and Matplotlib and is designed to be easy to use and integrate with other scientific libraries. This tutorial explores how to use SciKit-Image for advanced image processing and analysis.
Key Points:
- SciKit-Image is an open-source image processing library for Python.
- It provides a collection of algorithms for image processing and computer vision.
- SciKit-Image is built on top of NumPy, SciPy, and Matplotlib.
Installing SciKit-Image
To use SciKit-Image, you need to install it using pip:
pip install scikit-image
Loading and Displaying Images
You can load and display images using SciKit-Image and Matplotlib. Here is an example:
import matplotlib.pyplot as plt
from skimage import io
# Load an image from a file
image = io.imread('path/to/your/image.jpg')
# Display the image
plt.imshow(image)
plt.axis('off')
plt.show()
Basic Image Manipulations
SciKit-Image provides functions for basic image manipulations such as resizing, rotating, and flipping. Here is an example:
from skimage.transform import resize, rotate
# Resize the image
resized_image = resize(image, (100, 100))
# Rotate the image
rotated_image = rotate(image, angle=45)
# Flip the image
flipped_image = image[:, ::-1]
# Display the manipulated images
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
axes[0].imshow(resized_image)
axes[0].set_title('Resized Image')
axes[0].axis('off')
axes[1].imshow(rotated_image)
axes[1].set_title('Rotated Image')
axes[1].axis('off')
axes[2].imshow(flipped_image)
axes[2].set_title('Flipped Image')
axes[2].axis('off')
plt.show()
Filtering and Enhancing Images
SciKit-Image provides various filters for enhancing images. Here is an example:
from skimage import filters
# Apply a Gaussian filter
gaussian_filtered = filters.gaussian(image, sigma=1)
# Apply a Sobel filter
sobel_filtered = filters.sobel(image)
# Display the filtered images
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
axes[0].imshow(gaussian_filtered)
axes[0].set_title('Gaussian Filtered Image')
axes[0].axis('off')
axes[1].imshow(sobel_filtered)
axes[1].set_title('Sobel Filtered Image')
axes[1].axis('off')
plt.show()
Segmentation
SciKit-Image provides various algorithms for image segmentation. Here is an example of using the Otsu thresholding method:
from skimage.color import rgb2gray
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import closing, square
# Convert the image to grayscale
gray_image = rgb2gray(image)
# Apply Otsu thresholding
thresh = threshold_otsu(gray_image)
binary_image = closing(gray_image > thresh, square(3))
# Remove artifacts connected to the image border
cleared = clear_border(binary_image)
# Label the image regions
label_image = label(cleared)
# Display the segmented image
fig, ax = plt.subplots(figsize=(10, 6))
ax.imshow(label_image, cmap='gray')
ax.set_title('Segmented Image')
ax.axis('off')
plt.show()
Feature Extraction
SciKit-Image provides functions for extracting features from images. Here is an example of extracting edges using the Canny edge detector:
from skimage.feature import canny
# Apply Canny edge detection
edges = canny(gray_image, sigma=1)
# Display the edges
plt.imshow(edges, cmap='gray')
plt.title('Edges Detected using Canny')
plt.axis('off')
plt.show()
Object Detection
SciKit-Image provides functions for detecting objects in images. Here is an example of detecting objects using region properties:
# Detect objects in the image
regions = regionprops(label_image)
# Display the detected objects
fig, ax = plt.subplots(figsize=(10, 6))
ax.imshow(gray_image, cmap='gray')
for region in regions:
# Draw rectangle around segmented objects
minr, minc, maxr, maxc = region.bbox
rect = plt.Rectangle((minc, minr), maxc - minc, maxr - minr, fill=False, edgecolor='red', linewidth=2)
ax.add_patch(rect)
ax.set_title('Detected Objects')
ax.axis('off')
plt.show()
Summary
In this tutorial, you learned about performing advanced image processing and analysis using SciKit-Image in Python. SciKit-Image provides a collection of algorithms for image processing and computer vision, making it easy to perform tasks such as loading and displaying images, basic image manipulations, filtering and enhancing images, segmentation, feature extraction, and object detection. Understanding how to use SciKit-Image can help you effectively analyze and process images for various data science applications.