📸 Edge Detection in Images: Sobel vs Canny using OpenCV (Python)
✨ Introduction
Edge detection is a fundamental technique in computer vision used to identify boundaries of objects in images. Whether it's medical imaging, license plate recognition, or self-driving cars, edge detection helps machines interpret the structure of visual data.
In this blog, we’ll:
- Understand and implement Sobel and Canny edge detection techniques.
- Walk through a numerical example of convolution and thresholding using Sobel.
- Write Python code to visualize both methods side by side.
📘 Theory
🔹 What is Edge Detection?
Edge detection locates points in a digital image where brightness changes sharply. These changes often indicate boundaries of objects.
🔹 Sobel Operator
The Sobel operator uses two 3×3 convolution kernels to compute gradients in:
- Horizontal direction (Gx)
- Vertical direction (Gy)
From these, we compute the gradient magnitude at each pixel, indicating edge strength.
Sobel Kernels:
Sobel-X (Gx):
Kx = [[-1 0 1],
[-2 0 2],
[-1 0 1]]
Sobel-Y (Gy):
Ky = [[ 1 2 1],
[ 0 0 0],
[-1 -2 -1]]
🔢 Numerical Example of Sobel Edge Detection
We will apply the Sobel operator manually on a small 5×5 image to understand the math behind it.
✅ Step-by-Step Example
📷 Input Image (5×5)
I = [[10, 10, 10, 10, 10],
[10, 50, 50, 50, 10],
[10, 50, 90, 50, 10],
[10, 50, 50, 50, 10],
[10, 10, 10, 10, 10]]
🎯 Step 1: Choose 3×3 Region (Center)
Region = [[50, 50, 50],
[50, 90, 50],
[50, 50, 50]]
🔍 Step 2: Apply Sobel-X
Ix = (-1)*50 + 0*50 + 1*50 +
(-2)*50 + 0*90 + 2*50 +
(-1)*50 + 0*50 + 1*50 = 0
🔍 Step 3: Apply Sobel-Y
Iy = (1)*50 + 2*50 + 1*50 +
0*50 + 0*90 + 0*50 +
(-1)*50 + (-2)*50 + (-1)*50 = 0
✨ Step 4: Compute Edge Magnitude
M = √(Ix² + Iy²) = √(0² + 0²) = 0
So, the center pixel has no edge.
🧮 Step 5: Compute Full Magnitude Matrix
M = [[80, 50, 80],
[50, 0, 50],
[80, 50, 80]]
🔧 Step 6: Apply Thresholding
Threshold = 520 / 9 ≈ 58
🧊 Step 7: Final Binary Edge Map
Final Map = [[1, 0, 1],
[0, 0, 0],
[1, 0, 1]]
Where: 1 = Edge detected, 0 = No edge
🐍 Implementation in Python
💡 Required Libraries
import cv2
import numpy as np
import matplotlib.pyplot as plt
⚙️ Step-by-Step Code
# 1. Read image in grayscale
image = cv2.imread('your_image.jpg', cv2.IMREAD_GRAYSCALE)
# 2. Apply Gaussian blur for noise reduction
blurred = cv2.GaussianBlur(image, (5, 5), 1.4)
# 3. Compute Sobel edges
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
sobel_magnitude = np.sqrt(sobel_x**2 + sobel_y**2)
sobel_magnitude = np.uint8(np.clip(sobel_magnitude, 0, 255))
# 4. Compute Canny edges
canny_edges = cv2.Canny(blurred, 100, 200)
# 5. Display results
plt.figure(figsize=(12, 6))
plt.subplot(1, 3, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.subplot(1, 3, 2)
plt.title("Sobel Edge Detection")
plt.imshow(sobel_magnitude, cmap='gray')
plt.axis('off')
plt.subplot(1, 3, 3)
plt.title("Canny Edge Detection")
plt.imshow(canny_edges, cmap='gray')
plt.axis('off')
plt.tight_layout()
plt.show()
🖼️ Output Comparison
Here’s how the original, Sobel, and Canny edge-detected images look side by side:
🧠 Sobel vs Canny – Comparison
| Feature | Sobel | Canny |
|---|---|---|
| Type | Gradient-based | Multi-stage |
| Noise Sensitivity | High | Low (uses Gaussian blur) |
| Edge Thinness | Medium | Very thin and clean |
| Complexity | Simple | Complex |
| Use Case | Fast, simple projects | Accurate edge detection |
📝 Conclusion
Sobel is easy to implement and understand — great for learning.
Canny is more accurate, especially with noise and fine edges.
Understanding convolution and thresholding manually helps you appreciate how these algorithms work under the hood.
Comments
Post a Comment