Image processing with Scikit-image in Python

Image processing with Scikit-image in Python

Scikit-image is a powerful image processing library in Python that is built on top of SciPy. It offers a wide range of algorithms for tasks like image segmentation, geometric transformations, color space manipulation, analysis, and more.

Let's go through some fundamental image processing tasks using scikit-image:

1. Installation:

You can install scikit-image using pip:

pip install scikit-image 

2. Basic Image I/O:

Load an image from a file and display it:

import matplotlib.pyplot as plt from skimage import io image = io.imread('path_to_image.jpg') plt.imshow(image) plt.axis('off') plt.show() 

3. Color Conversion:

Convert an image from RGB to grayscale:

from skimage import color gray_image = color.rgb2gray(image) plt.imshow(gray_image, cmap='gray') plt.axis('off') plt.show() 

4. Resizing:

Resize an image:

from skimage.transform import resize resized_image = resize(image, (100, 100)) plt.imshow(resized_image) plt.axis('off') plt.show() 

5. Edge Detection:

Detect edges using the Canny filter:

from skimage import feature edges = feature.canny(gray_image, sigma=2) plt.imshow(edges, cmap='gray') plt.axis('off') plt.show() 

6. Image Segmentation:

Segment objects using the Felzenszwalb's efficient graph-based segmentation:

from skimage.segmentation import felzenszwalb segments = felzenszwalb(image, scale=100) plt.imshow(segments, cmap='rainbow') plt.axis('off') plt.show() 

7. Filters:

Apply a Gaussian filter for blurring:

from skimage import filters blurred = filters.gaussian(gray_image, sigma=2) plt.imshow(blurred, cmap='gray') plt.axis('off') plt.show() 

8. Morphology:

Apply morphological operations like dilation and erosion:

from skimage.morphology import dilation, erosion, disk selem = disk(5) # Defines a disk-shaped structuring element dilated = dilation(gray_image, selem) eroded = erosion(gray_image, selem) fig, ax = plt.subplots(1, 3, figsize=(12, 4)) ax[0].imshow(gray_image, cmap='gray') ax[0].set_title('Original') ax[0].axis('off') ax[1].imshow(dilated, cmap='gray') ax[1].set_title('Dilated') ax[1].axis('off') ax[2].imshow(eroded, cmap='gray') ax[2].set_title('Eroded') ax[2].axis('off') plt.show() 

These are just some basic examples. Scikit-image offers a plethora of functions for various image processing tasks.


More Tags

jmeter-4.0 android-10.0 amazon-sqs react-dates excel-2007 binning epoch angular-ui-router microservices pose-estimation

More Programming Guides

Other Guides

More Programming Examples