Introduction to Convolutions using Python
Last Updated :
14 Mar, 2023
Convolutions are one of the key features behind Convolutional Neural Networks. For the details of working of CNNs, refer to Introduction to Convolution Neural Network. Feature Learning Feature Engineering or Feature Extraction is the process of extracting useful patterns from input data that will help the prediction model to understand better the real nature of the problem. A good feature learning will present patterns in a way that increase significantly the accuracy and performance of the applied machine learning algorithms in a way that would be impossible or too expensive by the machine learning itself. Feature learning algorithms find the common patterns that are important to distinguish between the wanted classes and extract them automatically.
After this process, they are ready to be used in a classification or regression problem. Let us consider a popular image classification problem, classification of images of a face and a non-face object. In the early days of computer vision, scientists tried to solve the problem by hand coding the detection algorithms of possible features of a human face like shape, eyes, nose, lips etc. This approach usually gave poor results because a face may appear in so many varieties, that it was not possible to account for even a significant fraction of the features. Just a simple change in lighting or orientation can bring about change in an image such that the algorithms were no longer able to detect faces. In 1998, Yann Lecun introduced the concept of Convolutional Neural Networks which was capable of classifying images of handwritten characters with about 99% accuracy.
The great advantage of Convolutional Neural Networks is that they are uncommonly good at finding features in images that grow after each level, resulting in high-level features in the end. The final layers (can be one or more) use all these generated features for classification or regression. Convolution Convolution is an operation that is performed on an image to extract features from it applying a smaller tensor called a kernel like a sliding window over the image. Depending on the values in the convolutional kernel, we can pick up specific patterns from the image. In the following example, we will demonstrate detection of horizontal and vertical edges in an image using appropriate kernels.
Convolution is a mathematical operation that is used to combine two functions to form a third function that expresses how the shape of one is modified by the other. In the context of image processing and computer vision, convolutions are used to extract features from images.
In Python, one popular library for image processing and computer vision is OpenCV. OpenCV has the function cv2.filter2D() which can be used to apply a convolution to an image.
Python3
import numpy as np
import matplotlib.pyplot as plt
# let img1 be an image with no features
img1 = np.array([np.array([200, 200]), np.array([200, 200])])
img2 = np.array([np.array([200, 200]), np.array([0, 0])])
img3 = np.array([np.array([200, 0]), np.array([200, 0])])
kernel_horizontal = np.array([np.array([2, 2]), np.array([-2, -2])])
print(kernel_horizontal, 'is a kernel for detecting horizontal edges')
kernel_vertical = np.array([np.array([2, -2]), np.array([2, -2])])
print(kernel_vertical, 'is a kernel for detecting vertical edges')
# We will apply the kernels on the images by
# elementwise multiplication followed by summation
def apply_kernel(img, kernel):
return np.sum(np.multiply(img, kernel))
# Visualizing img1
plt.imshow(img1)
plt.axis('off')
plt.title('img1')
plt.show()
# Checking for horizontal and vertical features in image1
print('Horizontal edge confidence score:', apply_kernel(img1,
kernel_horizontal))
print('Vertical edge confidence score:', apply_kernel(img1,
kernel_vertical))
# Visualizing img2
plt.imshow(img2)
plt.axis('off')
plt.title('img2')
plt.show()
# Checking for horizontal and vertical features in image2
print('Horizontal edge confidence score:', apply_kernel(img2,
kernel_horizontal))
print('Vertical edge confidence score:', apply_kernel(img2,
kernel_vertical))
# Visualizing img3
plt.imshow(img3)
plt.axis('off')
plt.title('img3')
plt.show()
# Checking for horizontal and vertical features in image3
print('Horizontal edge confidence score:', apply_kernel(img3,
kernel_horizontal))
print('Vertical edge confidence score:', apply_kernel(img3,
kernel_vertical))
Output:
[ [ 2 2] [-2 -2] ] is a kernel for detecting horizontal edges [ [ 2 -2] [ 2 -2] ] is a kernel for detecting vertical edges
Horizontal edge confidence score: 0 Vertical edge confidence score: 0
Horizontal edge confidence score: 800 Vertical edge confidence score: 0
Horizontal edge confidence score: 0 Vertical edge confidence score: 800
Advantages and Disadvantages:
Advantages of using convolution in image processing and computer vision include:
- The ability to extract features from images: Convolutions can be used to identify patterns and features in an image, such as edges, corners, and textures. This can be useful for tasks such as object detection, image classification, and image segmentation.
- Translation invariance: Convolutions are translation invariant, which means that the same feature can be detected regardless of its position in the image. This is important for tasks such as object recognition, where the object may be in different positions in different images.
- Efficiency: Convolutions can be computed using fast algorithms such as the Fast Fourier Transform (FFT), which makes them efficient to compute even for large images.
- ability to learn features from data: In CNNs, the convolutional layers learn to extract features from the input data, which makes them useful in tasks such as image classification.
Disadvantages of using convolution in image processing and computer vision include:
- Limited ability to process large images: Convolutions are limited by the size of the kernel, which means that they are not well-suited for processing very large images.
- Limited ability to detect non-linear features: Convolutions are linear operations, which means that they are not well-suited for detecting non-linear features in images.
- High computational cost: Convolutional neural networks have a high computational cost which makes them less efficient to train and run.
- Overfitting: CNNs are prone to overfitting, especially when the training dataset is small. It is important to use techniques such as regularization to prevent overfitting.
References:
There are several books that provide in-depth coverage of convolution and its applications in image processing and computer vision. Some popular ones include:
- "Digital Image Processing" by Rafael C. Gonzalez and Richard E. Woods: This book provides a comprehensive introduction to image processing, including a thorough coverage of convolution and its applications.
- "Computer Vision: Algorithms and Applications" by Richard Szeliski: This book covers a wide range of computer vision topics, including a detailed discussion of convolution and its use in image processing.
- "Deep Learning for Computer Vision" by Rajalingapuram K. Sundaram: This book provides an in-depth introduction to deep learning for computer vision, including coverage of convolutional neural networks and their applications in image and video analysis.
- "Hands-On Image Processing with Python" by Sandipan Dey: This book is a practical guide to image processing using Python and its libraries such as OpenCV and scikit-image. It covers a wide range of image processing techniques, including convolution and its applications.
- "Python Machine Learning" by Sebastian Raschka and Vahid Mirjalili: This book provides a comprehensive introduction to machine learning with Python, including coverage of convolutional neural networks and their use in image and video analysis.
Similar Reads
Using User Input to Call Functions - Python
input() function allows dynamic interaction with the program. This input can then be used to call specific functions based on the user's choice .Letâs take a simple example to call function based on user's input .Example:Pythondef add(x, y): return x + y # Add def sub(x, y): return x - y # Subtract
2 min read
Introduction to Python for Absolute Beginners
Are you a beginner planning to start your career in the competitive world of Programming? Looking resources for Python as an Absolute Beginner? You are at the perfect place. This Python for Beginners page revolves around Step by Step tutorial for learning Python Programming language from very basics
6 min read
Introduction To Machine Learning using Python
Machine learning has revolutionized the way we approach data-driven problems, enabling computers to learn from data and make predictions or decisions without explicit programming. Python, with its rich ecosystem of libraries and tools, has become the de facto language for implementing machine learni
6 min read
Image Filtering Using Convolution in OpenCV
Prerequisites: Basics of OpenCV, Basics of Convolution In this article, filtering of images using convolution in OpenCV (Open Source Computer Vision) is discussed. In order to use the OpenCV library in Python, the following libraries should be installed as a prerequisite: Numpy libraryMatplotlib lib
8 min read
Taking input from console in Python
What is Console in Python? Console (also called Shell) is basically a command line interpreter that takes input from the user i.e one command at a time and interprets it. If it is error free then it runs the command and gives required output otherwise shows the error message. A Python Console looks
2 min read
How to Call Multiple Functions in Python
In Python, calling multiple functions is a common practice, especially when building modular, organized and maintainable code. In this article, weâll explore various ways we can call multiple functions in Python.The most straightforward way to call multiple functions is by executing them one after a
3 min read
Concatenate images using OpenCV in Python
To concatenate images vertically and horizontally with Python, cv2 library comes with two functions as: hconcat(): It is used as cv2.hconcat() to concatenate images horizontally. Here h means horizontal.vconcat(): It is used as cv2.vconcat() to concatenate images vertically. Here v means vertical.Im
3 min read
Vector outer product with Einstein summation convention using NumPy in Python
In this article, we will find vector outer product with Einstein summation convention in Python. numpy.einsum() method The numpy.einsum() method from the NumPy library is used to find the vector outer product with the Einstein summation convention in Python. Many common multi-dimensional, linear al
3 min read
Python - Channel Drop using Pillow
A channel drop is a method of removing one of the channels of a multichannel image. By removing means turning the color value of a particular channel to 0 (all pixels), i.e. that particular channel doesn't have any effect on the final image (assuming colors are blended `normally`). Color theory (Col
5 min read
How to Blur Faces in Images using OpenCV in Python?
Prerequisite: OpenCV OpenCV is a huge open-source library for computer vision, machine learning, and image processing. Â It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optim
2 min read