Features are the basic attributes or aspects which clearly help us identify the particular object, image, or anything. Features are the marked properties which are unique. While working on an image dataset we need to extract the features of different images which will help us segregate the images based on certain features or aspects.
There is no exact definition of the features of an image but things like the shape, size, orientation, etc. constitute the feature of the image. Extracting these features can be done using different techniques using python.
Scikit-Image is an open-source image processing library for Python. It includes algorithms for segmentation, geometric transformations, color space manipulation, analysis, filtering, morphology, feature detection, and more. We will use scikit-image for feature extraction.
In this article we will discuss:
- Loading and Visualizing an Image
- Analyze image properties
- Image Feature Extraction using Scikit-Image
We will start by analyzing the image and then basic feature extraction using python followed by feature extraction using Scikit-Image. We can use any local image we have on our system, I will use an image saved on my system for which I will try and extract features.
a. Importing the required libraries
We will look at all the aspects of the image so we need to import different libraries including NumPy, pandas, etc.
import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline from skimage.io import imread, imshow
b. Loading the image
You can use any image from your system. I have an image named’elephant.jpg’ for which I will be performing feature extraction.
image1 = imread('C:/Users/Lenovo/Downloads/elephant.jpg') imshow(image1); image2 = imread('elephant.jpg', as_gray=True) imshow(image2);
c. Analyzing both the images
#Shape of images print(image1.shape) print(image2.shape)
Here we can see that the colored image contains rows, columns, and channels as it is a colored image there are three channels RGB while grayscale pictures have only one channel. So we can clearly identify the colored and grayscale images by their shapes.
Let’s look at the size of the images.
Image size is the product of the rows, columns, and channels. Despite being the same images grayscale is smaller in size because it has only 1 channel.
d. Feature Extraction
i. Pixel Features
The number of pixels in an image is the same as the size of the image for grayscale images we can find the pixel features by reshaping the shape of the image and returning the array form of the image.
pixel_feat1 = np.reshape(image2, (1080 * 1920) pixel_feat1
Similarly, we can find the pixel feature for the colored image.
pixel_feat2 = np.reshape(image1, (1080 * 1920 * 3) pixel_feat2
ii. Edge Features
Edges in an image are the corners where the pixel change drastically, as the images are stored in array form we can visualize different values and see where the change in pixel value is higher but doing it manually takes time, Scikit-Image provides functions for image edge features extraction namely:
It is an edge detection kernel that works separately for both horizontal and vertical axis.
- Sobel Kernel:
It works on creating images with emphasis on edges
- Canny Algorithm
Canny Algorithm is an edge detection technique that uses a multi-stage algorithm to detect a wide range of edges in images.
Using these three algorithms for edge feature extraction.
from skimage import filters # prewitt kernel pre_hor = prewitt_h(image2) pre_ver = prewitt_v(image2) # Sobel Kernel ed_sobel = filters.sobel(image2) #canny algorithm can = feature.canny(image2) imshow(pre_ver, cmap='gray'); imshow(pre_hor, cmap='gray'); imshow(ed_sobel, cmap='gray'); imshow(can, cmap=’gray);
There are many other kernels for edge feature extraction but these three are the most used ones.
We use this segment object from a background. We will use our image and try region-based segmentation on it.
from skimage.exposure import histogram hist, hist_centers = histogram(image2) #Plotting the Image and the Histogram of gray values fig, axes = plt.subplots(1, 2, figsize=(8, 3)) axes.imshow(image2, cmap=plt.cm.gray) axes.axis('off') axes.plot(hist_centers, hist, lw=2) axes.set_title('histogram of gray values')
This image clearly depicts the feature we have tried to extract i.e segmentation of object from the background.
These are some of the main image features which can be extracted using python and Scikit-Image.
In this article, we successfully discovered:
- How to analyze an image?
- Extracting feature using basic python.
- Extracting advanced image features using Scikit-Image.