9 Powerful Tricks for Working with Image Data using Skimage in Python
Introduction
As a data scientist or machine learning practitioner, sooner or later you‘ll need to work with image data. Whether you‘re building computer vision models for object detection, facial recognition, or image classification, being able to effectively preprocess and manipulate image data is an essential skill.
Python‘s scikit-image library, or skimage for short, is a powerful open source package for working with images. If you‘re already familiar with scikit-learn for machine learning in Python, you‘ll find skimage follows a similar API design and is just as easy to use. Even if you‘re new to Python, skimage is beginner-friendly and has excellent documentation.
In this article, we‘ll dive into 9 powerful techniques for working with image data using skimage. With helpful code examples in Python, you‘ll learn how to load, manipulate, transform and preprocess images to build better computer vision models. Even if you‘re a beginner in Python or computer vision, this guide will give you the tools and understanding you need to confidently work with image data. Let‘s jump in!
1. Reading Images in Different Formats
The first step in any computer vision task is loading your image data. Skimage makes it easy to read images in both color and grayscale formats using the imread() function.
To load a color image:
from skimage import io
image = io.imread(‘image.jpg‘)
The image is loaded as a NumPy array, with dimensions (height, width, 3). The ‘3‘ represents the RGB color channels.
To load a grayscale image, simply pass the as_gray=True argument:
image = io.imread(‘image.jpg‘, as_gray=True)
Now the loaded image has shape (height, width), with pixel intensities ranging from 0 (black) to 1 (white).
When should you use color vs grayscale? Color images contain more information, but grayscale images are smaller in size which is computationally cheaper. If color information isn‘t needed for your task (e.g. detecting edges), grayscale is usually the way to go.
2. Converting Between Image Formats
Skimage provides utility functions to easily convert between different image formats like RGB, grayscale, HSV (hue, saturation, value), and HSL (hue, saturation, lightness).
To convert a color image to grayscale:
from skimage.color import rgb2gray
image_gray = rgb2gray(image)
To convert RGB to HSV or HSL formats:
from skimage.color import rgb2hsv, rgb2hsl
image_hsv = rgb2hsv(image)
image_hsl = rgb2hsl(image)
HSV and HSL are cylindrical representations of the RGB color model that can be more intuitive for certain image processing tasks. The hue dimension represents color pigment, saturation is the intensity of the color, and value/lightness refers to brightness.
3. Resizing Images
When building machine learning models with image data, it‘s often necessary to resize all images to a consistent shape first, especially when using deep learning. Skimage‘s resize() function lets you easily change the dimensions of an image.
from skimage.transform import resize
image_resized = resize(image, (128, 128))
Here we‘ve resized the image to 128×128 pixels, but you can use any dimensions you need. Keep in mind that resizing will affect the aspect ratio of the image.
4. Rescaling Images
Rescaling is similar to resizing, but instead of specifying an absolute size, we define a scale factor. This is useful if you have images of varying dimensions and want to scale them all by the same amount.
To scale an image by a factor of 0.5 (50%):
from skimage.transform import rescale
image_rescaled = rescale(image, 0.5, anti_aliasing=True, multichannel=True)
Here we‘ve scaled the image to half its original size. The anti_aliasing parameter helps avoid artifacts when downscaling, and multichannel=True is needed to correctly handle color images.
You can also scale by different factors for height and width:
image_rescaled = rescale(image, (0.5, 0.75), anti_aliasing=True, multichannel=True)
Now the height is scaled by 50% and width by 75%. Rescaling is a useful preprocessing step to speed up computations if your original images are very large.
5. Rotating Images
Skimage lets you easily rotate an image by any arbitrary angle using the rotate() function. This is useful for correcting orientation issues or as a data augmentation technique.
To rotate an image 45 degrees:
from skimage.transform import rotate
image_rotated = rotate(image, 45, resize=True)
Setting resize=True will enlarge the output image to fit the rotated image, otherwise parts of the image will be cut off.
A common data augmentation trick is to randomly rotate images during training to improve model robustness. You could do this using a little utility function:
from skimage.transform import rotate
import numpy as np
def random_rotation(image):
angle = np.random.uniform(-90, 90)
return rotate(image, angle, resize=True)
Every time you call random_rotation(), the input image will be rotated by a random angle between -90 and +90 degrees. Cheap and easy way to generate more training data!
6. Flipping Images
Another easy data augmentation technique is to flip images horizontally and/or vertically. Scikit-image doesn‘t have a built-in function for flipping, but NumPy makes it easy:
import numpy as np
image_flipped = np.fliplr(image) # horizontal flip
image_flipped = np.flipud(image) # vertical flip
Flipping images is a quick way to double or quadruple your training dataset and expose your model to more variations.
7. Cropping Images
Cropping is useful for extracting regions of interest from an image, or removing irrelevant border regions. Cropping is just array slicing in NumPy:
image_cropped = image[50:400, 100:500]
This crops the image to the region from height 50 to 400 and width 100 to 500 (assuming the original image is at least that size).
You can also crop by relative amounts, which is more useful if you need to process many images of different sizes:
crop_width = image.shape[1] // 4
crop_height = image.shape[0] // 4
image_cropped = image[crop_height:-crop_height, crop_width:-crop_width]
This crops 25% off each border of the image. Negative slicing with -crop_height and -crop_width counts backwards from the end of the array dimensions.
8. Adjusting Image Brightness
Skimage provides the adjust_gamma() function to easily adjust image brightness by applying a non-linear transformation. Values 1 will brighten it.
To darken an image:
from skimage import exposure
image_darkened = exposure.adjust_gamma(image, gamma=0.5)
And to brighten an image:
image_brightened = exposure.adjust_gamma(image, gamma=1.5)
Gamma adjustment is a useful technique for contrast enhancement, especially for images captured in low-light conditions. You can also use random gamma adjustment as another data augmentation trick.
9. Applying Image Filters
Image filters are used to modify an image by altering the pixel values based on some function of neighboring pixels. Skimage provides a variety of built-in filters for blurring, sharpening, edge detection and more.
One of the most common filters is Gaussian blur, which is often used to reduce image noise:
from skimage.filters import gaussian
image_blurred = gaussian(image, sigma=1, multichannel=True)
The sigma parameter controls the amount of blurring (higher sigma = more blurring). Setting multichannel=True is required for RGB images, otherwise you‘ll get an error.
Another useful filter is the Sobel filter which computes the gradient of the image and highlights edges:
from skimage.filters import sobel
image_edges = sobel(image)
The resulting image will have bright pixels wherever there are strong edges or transitions in intensity. Edge detection is often used as a preprocessing step before applying more complex computer vision algorithms.
There are many other filters available in skimage for various applications. The unsharp_mask() filter can be used to sharpen an image, median() is effective for removing salt-and-pepper noise, and roberts() is another simple edge detector. Experiment with the different options to see what works best for your specific task and image data!
Conclusion
Skimage is an invaluable tool for anyone working with images in Python. With its simple and intuitive interface, it makes advanced image processing techniques accessible even for beginners.
In this article, we‘ve covered 9 essential tricks for manipulating and pre-processing images using skimage and Python. You‘ve learned how to:
- Read images in color and grayscale formats
- Convert between different image color spaces
- Resize images to a target size
- Rescale images by a relative factor
- Rotate images by an arbitrary angle
- Flip images horizontally and vertically
- Crop images to a region of interest
- Adjust image brightness using gamma correction
- Apply filters for blurring, sharpening and edge detection
Along the way, we‘ve also seen how techniques like rotation, flipping, and brightness adjustment can be used for data augmentation to generate additional training examples and improve model robustness.
To take your skills further, you can explore the many other functions available in skimage for tasks like segmentation, feature detection, and image restoration. Be sure to consult the excellent API documentation which includes examples and helpful explanations.
Of course, as you work on real computer vision projects, you‘ll often need to combine these fundamental operations with machine learning models and deep neural networks to solve challenging problems. But with a solid foundation in image manipulation using skimage, you‘ll be well-prepared to tackle any computer vision task.
So get out there and start experimenting with skimage! And if you‘re eager to dive deeper into computer vision and learn how to build advanced models using deep learning, be sure to check out Analytics Vidhya‘s comprehensive course. Happy coding!