An image is a two-dimensional representation of a visual subject, like a photograph, painting, or drawing. In digital imaging, images are stored as arrays of pixel values, where each pixel represents a sample of the image’s brightness and color. The color of each pixel can be represented by one or multiple channels, like the red, green, and blue (RGB) channels in traditional color images. In this article, you will learn various image preprocessing techniques.
Images can be processed using computer algorithms to change their appearance or extract information. Image processing techniques include operations like resizing, cropping, rotating, filtering, and thresholding. These operations are performed on the pixel values to modify the image or extract information about its content. Image processing is used in so many applications, including computer vision, medical imaging, and digital art.
By learning Image Preprocessing using SKimage, you will be able to:
This article was published as a part of the Data Science Blogathon.
Let’s go with an image that can be broken down into a matrix of numbers, where each number represents the strength. And this strength could be anywhere between 0 (representing black) and 255 (representing white). So, a monochromatic image can be represented by a single matrix.
But what can we do when it is a colored image below?
If we have to represent an image, we will break it into three images of three different colors: red, green, and blue. We can store the intensities of each color in two single matrices.
So the image will be broken down into three matrices: one for the red color, one for the green color, and one for the blue color so that we can represent the image in an N*M*3 matrix.
Any image that is n*m pixels wide can always be defined as a matrix N*M*3 anywhere in the computer.
When processing image data, it is common to convert the image into a numerical representation, like a matrix, so that computer algorithms can process it. The numerical representation of the image is called a digital image, and the data in the digital image can be manipulated using mathematical operations to perform different image-processing tasks.
Overall, understanding image data is necessary for working with image processing algorithms and extracting information from images.
Here are the common steps involved in processing an image in Python:
Transformations in images refer to mathematical operations applied to an image to change its appearance or extract useful information from it. Many types of transformations can be applied to images, including:
These transformations are performed using mathematical algorithms and can be implemented in software like Python or MATLAB. Transformations are essential to image processing and are crucial in applications like computer vision, medical image analysis, and facial recognition.
Now we will start to load the image and do some operations on the image using the Scikit Image Library.
Depending on the programming language and tools being used, there are several ways to load an image. Here are a few common ways:
Once the image is uploaded into memory, you can perform various operations on it, like resizing, cropping, color conversion, and filtering, using the functions provided by the image processing library.
Here is an example of how to load an image using the scikit-image (skimage) library in Python:
originating from Skimage Import IO
# Open the image
io.imread("image.jpg") = image
# Show the image
io.imshow(image)
io.show()
In this example, the imread function from the io module of the skimage library is used to load an image file called image.jpg into memory. The resulting image data is then displayed using the imshow function from the io module, followed by a call to “io.show()” to display the image. The imshow function automatically adjusts the image for display and handles issues like color channels and aspect ratio.
Visualizing an image involves displaying the image data on a screen or output device. Visualizing an image depends on the programming language and tools being used. Here are a few common ways:
Once the image is displayed, you can interact with it by zooming in and out, panning, and readjusting the display settings.
Here is an example of how to visualize an image using the scikit-image (skimage) library in Python:
from skimage import io
import matplotlib.pyplot as plt
# Load the image
image = io.imread("image.jpg")
# Display the image
plt.imshow(image)
plt.show()
In this example, the imread
function from the "io
” module of the skimage library is used to load an image file called image.jpg
into memory. The resulting image data is then displayed using the imshow
function from the matplotlib.pyplot
module, followed by a call to plt.show()
to display the image.
Python’s “scikit-image” (skimage) library provides several functions for resizing images. One commonly used function for this purpose is “resize” from the “transform” module.
Here is an example of using skimage to resize an image:
import skimage
from skimage import io, transform
# Load the image
image = io.imread(“example.jpg”)
resized_image = transform.resize(image, (300, 300))
# Save the resized image
io.imsave(“resized_image.jpg”, resized_image)
In this example, the original image is read using the imread
function from the io
module. The resize
the function is then used to resize the image to a size of (300 300) pixels. Finally, the resized image is saved using the imsave
function.
In Python, the “scikit-image” (skimage) library provides several functions for reshaping images. Here is an example of using skimage to reshape an image:
# import colour sub-module
from skimage import color
# reading the image
image = imread('index.png')
# converting image to grayscale
grayscale_image = color.rgb2gray(image)
grayscale_image.shape
import numpy as np
new_shape = (grayscale_image.shape[0]*grayscale_image.shape[1])
# reshape
image2 = np.reshape(grayscale_image, new_shape)
image2.shape
If converting a 4 by 4 2-D image to 1-D, we will have 4×4=16 values.
Image rotation involves rotating an image about its center by a specified angle. In scikit-image, you can use the rotate
function from the transform
module to rotate an image. Here’s an example in Python:
import numpy as np
from skimage import io
from skimage.transform import rotate
# Load an image
image = io.imread("image.jpg")
# Rotate the image by 180 degrees
rotated_image = rotate(image, angle=180, resize=True)
# Save the rotated image
io.imsave("rotated_image.jpg", rotated_image)
In this example, the rotate
function is used to rotate the input image by 180 degrees. The first argument to the rotate
function is the input image, and the angle
argument specifies the rotation angle in degrees.
Image cropping involves extracting a portion of an image by specifying a crop region. In scikit-image, you can use slicing and indexing to crop an image. Here’s an example in Python:
import numpy as np
from skimage import io
# Load an image
image = io.imread("image.jpg")
rows, cols = image.shape[:2]
cropped_image = image[rows//4:-rows//4, cols//4:-cols//4]
# Save the cropped image
io.imsave("cropped_image.jpg", cropped_image)
In this example, the input image is first loaded using the imread
function. The crop region is specified by slicing the image along both dimensions, such that the first and last quarter of the rows and columns are removed.
Image flipping in Python can be performed using the “cv2.flip” function from the OpenCV library. The “cv2.flip” function takes two arguments: the input image and a flip code. The flip code specifies the flipping to be performed and can be one of the following values:
Flipping can be considered an extension of rotation, allowing left-right and up-down image flipping.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# reading the image
image = imread('index.png')
image = np.array(image)
imshow(image)
plt.title('Original Image')
Now, what do you do if you have to flip the images, again read the image and now in order to flip it? let’s say we are doing left to right flip. I can easily do that using the “fliplr()” function.
# flip image left-to-right
flipLR = np.fliplr(image)
plt.imshow(flipLR)
plt.title('Left to Right Flipped')
And these are the ways in which you can flip the images.
Brightness manipulation in Python can be performed using the image library. The image library provides the exposure module, which includes the function of adjusting gamma that can be used to change the brightness of an image.
Images with different brightness can be used to make the model robust to changes in lighting conditions; this is important for systems that work in outdoor lightings, like CCTV cameras on traffic signals.
from skimage.exposure import adjust_gamma
# read the image
image = imread('index.png')
plt.title('Original Image')
imshow(image)
I am going to change the gamma value, and that changes the strength of the image. So, this is my bright image.
# brighten the image
bright = adjust_gamma(image,gamma=0.5,gain=1)
imshow(bright)
plt.title('Brightened IMage')
So, approximately I can make all of these changes to the image.
Scikit-image is a popular Python library for image processing that provides tools and functions for working with images. Here’s a summary of some of the critical features of image for image processing:
imread
for reading an image and imsave
for saving an image.Overall, image is a comprehensive and well-documented library for image processing that is widely used in scientific, medical, and industrial applications.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.