The increasing use of computer vision is making it important to know how to work with images. There is a lot of information stored in images, and pre-processing them helps extract useful information. This helps in image enhancement, image retrieval, image recognition, and visualization.
In this guide, you will learn techniques to extract features from images using Python. This has applications in medical image analysis, geospatial computing, robotic vision, and artificial intelligence.
In this guide, you will use the powerful
scikit-image library to work with images. The
scikit-image package is dedicated to image processing and uses native numpy arrays as objects to understand and store images. There are many inbuilt images in the package that you will use in this guide.
The first two lines of code below import the
scikit-image package and the inbuilt data set. The last two lines of code plot the image.
1import skimage 2from skimage import data, io 3import numpy as np 4from matplotlib import pyplot as plt 5%matplotlib inline
After loading the required libraries, the next step is to load some images. The first image you will plot is the image of a rocket. This is done with the code below.
1rocket = data.rocket() 2io.imshow(rocket) 3plt.show()
You will also load an image of coins with the code below.
1coins = data.coins() 2io.imshow(coins) 3plt.show()
You have loaded the images above and will learn a couple of important transformations next.
Sometimes you'll need to transform an image color to grayscale. This is done with the
color module of
skimage. The code below performs this transformation on the
rocket image, using the
color.rgb2gray() module .
1from skimage import color 2grayscale = color.rgb2gray(rocket) 3 4io.imshow(grayscale) 5plt.show()
You may also want to flip an image. The
np.fluidup() function can be used for vertical flipping. The code below performs this task.
1# Flip the image in up direction 2verticalflip = np.flipud(rocket) 3 4io.imshow(verticalflip) 5plt.show()
In this case, the image is inverted, but in many cases, you will receive the inverted image and need to flip it. This function will be handy in those cases.
Images are represented by pixels, which means that the simplest way to create image features is to use these raw pixel values as separate features. Start by printing the shape of the
The number of features will be the same as the number of pixels, which in this case is the product of 303 times 384, or 116,352.
To arrange these pixels as features, you’ll use the
reshape() function from numpy. The first line of code creates the
features object using the reshape function, which takes image and its dimensions as input. The second line prints the shape and the features.
1features = np.reshape(coins, (303*384)) 2features.shape, features
1((116352,), array([ 47, 123, 133, ..., 4, 10, 7], dtype=uint8))
The output above shows that you have the feature, which is a one-dimensional array of length 116,352.
Thresholding is a technique used to partition an image into its foreground and background. This is the simplest method of image segmentation in which a cut-off is applied to the image pixel values to segment the background. To see how this works, start by loading an image of a camera with the code below.
1camera = data.camera() 2io.imshow(camera) 3plt.show()
The next step is to apply the threshold value to segment the image. This is done with the code below. The first line arbitrarily assigns a threshold value of 100. The second line applies this to the image pixel values. The last four lines of code display the original and the thresholded image to show the difference.
1threshold_value = 100 2camera_threholded = camera > threshold_value 3 4# Show the original image 5io.imshow(camera) 6plt.show() 7 8# Show the thresholded image 9io.imshow(camera_threholded) 10plt.show()
The importance of selecting the threshold value is illustrated by the example below, where the threshold is kept at 200.
1threshold_value = 200 2camera_threholded = camera > threshold_value 3 4 5io.imshow(camera) 6plt.show() 7 8io.imshow(camera_threholded) 9plt.show()
It’s obvious that selecting the wrong threshold value distorts the image to the point that it becomes useless.
The solution is that instead of an arbitrary threshold value, you can use the Otsu method, which uses a simple heuristic method for finding the optimal threshold.
To do this, the first step is to import the
threshold_otsu module, which is done in the first line of code below. The second line uses the
threshold_otsu() function to obtain the optimal threshold value, while the third line applies it to the image. The last four lines of code display the original and the thresholded image to show the difference.
1from skimage.filters import threshold_otsu 2 3threshold_value = threshold_otsu(camera) 4 5camera_threholded = camera > threshold_value 6 7# Show the original image 8io.imshow(camera) 9plt.show() 10 11# Show the thresholded image 12io.imshow(camera_threholded) 13plt.show()
One of the advanced image processing applications is a technique called edge detection, which aims to identify points in an image where the brightness changes sharply or has discontinuities. These points are organized into a set of curved line segments termed edges. You will work with the coins image to explore this technique using the canny edge detection technique, widely considered to be the standard in edge detection technique.
The first line of code imports the canny edge detector from the feature module. The second line converts the image to grayscale, which is a requirement for canny detector. The third line applies the canny edge detector module to the
coins image. The last four lines of code plot the original image and the resulting image with edges.
1from skimage.feature import canny 2coins = color.rgb2gray(coins) 3 4# Apply Canny detector 5coins_edges = canny(coins) 6 7io.imshow(coins) 8plt.show() 9 10io.imshow(coins_edges) 11plt.show()
In the above image, you can now see the edges more clearly. Edge detection is widely used in applications like face detection, fingerprint matching, and medical diagnosis.
In this guide, you learned about building features from image data in Python. You learned techniques including transforming images, thresholding, extracting features, and edge detection. Finally, you learned how to perform these tasks using the popular and powerful
scikit-image library in Python.
To learn more about data science using Python, please refer to the following guides.