Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Introduction-to-convolutions-using-python
In this article, we will learn about convolutions in Python. Convolution is a fundamental operation in computer vision and neural networks for feature extraction from images.
Prerequisites − NumPy, Matplotlib
Installation
pip install numpy pip install matplotlib
What is Convolution?
Convolution is a mathematical operation performed on an image to extract features by applying a smaller matrix called a kernel (or filter) that slides over the image like a window. Depending on the values in the kernel, we can detect specific patterns such as edges, corners, or textures.
Edge Detection Example
We'll create simple 2x2 images and apply horizontal and vertical edge detection kernels ?
import numpy as np
from matplotlib import pyplot as plt
# Create sample images (2x2 matrices)
img1 = np.array([[100, 100], [80, 80]]) # Uniform gradient
img2 = np.array([[100, 100], [50, 0]]) # Horizontal edge
img3 = np.array([[100, 50], [100, 0]]) # Vertical edge
# Define edge detection kernels
horizontal_kernel = np.array([[3, 3], [-3, -3]])
vertical_kernel = np.array([[3, -3], [3, -3]])
print("Horizontal kernel:")
print(horizontal_kernel)
print("\nVertical kernel:")
print(vertical_kernel)
Horizontal kernel: [[ 3 3] [-3 -3]] Vertical kernel: [[ 3 -3] [ 3 -3]]
Applying Convolution
The convolution operation involves element-wise multiplication followed by summation ?
def apply_convolution(image, kernel):
"""Apply convolution operation: element-wise multiply then sum"""
return np.sum(np.multiply(image, kernel))
# Test on all three images
images = [img1, img2, img3]
image_names = ['Image 1 (Uniform)', 'Image 2 (Horizontal Edge)', 'Image 3 (Vertical Edge)']
for i, (img, name) in enumerate(zip(images, image_names)):
print(f"\n{name}:")
print(img)
h_score = apply_convolution(img, horizontal_kernel)
v_score = apply_convolution(img, vertical_kernel)
print(f"Horizontal edge score: {h_score}")
print(f"Vertical edge score: {v_score}")
Image 1 (Uniform): [[100 100] [ 80 80]] Horizontal edge score: 60 Vertical edge score: 0 Image 2 (Horizontal Edge): [[100 100] [ 50 0]] Horizontal edge score: 450 Vertical edge score: 150 Image 3 (Vertical Edge): [[100 50] [100 0]] Horizontal edge score: 150 Vertical edge score: 450
Understanding the Results
| Image Type | Horizontal Score | Vertical Score | Detected Feature |
|---|---|---|---|
| Uniform gradient | 60 | 0 | Slight horizontal change |
| Horizontal edge | 450 | 150 | Strong horizontal edge |
| Vertical edge | 150 | 450 | Strong vertical edge |
Visualizing the Images
# Visualize all images
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
for i, (img, name) in enumerate(zip(images, image_names)):
axes[i].imshow(img, cmap='gray')
axes[i].set_title(name)
axes[i].axis('off')
plt.tight_layout()
plt.show()
How It Works
The convolution operation works by:
- Element-wise multiplication − Each pixel in the image is multiplied by the corresponding kernel value
- Summation − All products are summed to get a single response value
- Feature detection − Higher scores indicate stronger presence of the target feature
Conclusion
Convolution is a powerful technique for feature extraction in image processing. By designing appropriate kernels, we can detect specific patterns like horizontal and vertical edges, which form the foundation of more complex computer vision algorithms.
