Edge Detection - iffatAGheyas/computer-vision-handbook GitHub Wiki

🎛️ Edge Detection (Sobel & Canny)

Edge detection extracts the “skeleton” of shapes in an image by finding where pixel values change rapidly (e.g., dark→bright or object→background). It’s a fundamental step in many vision tasks, from feature extraction to object recognition.


🧠 What Is Edge Detection?

Edge detection identifies boundaries in images—locations of rapid intensity change:

  • From dark to bright
  • From object to background

Think of it as outlining the important lines and shapes in a scene.


1. Sobel Operator — Basic Edge Detector

What It Does

  • Detects gradients (rate of change in pixel values)
  • Applies in x-direction for vertical edges and y-direction for horizontal edges

Sobel Kernels

Gx = [ [-1, 0, 1],
       [-2, 0, 2],
       [-1, 0, 1] ]

Gy = [ [-1, -2, -1],
       [  0,  0,  0],
       [  1,  2,  1] ]

These emphasize intensity changes along the x- or y-axis.

🐍 Python Example (OpenCV)

import cv2
import matplotlib.pyplot as plt

# Load image in grayscale
img = cv2.imread("bird.jpg", cv2.IMREAD_GRAYSCALE)

# Apply Sobel filters
sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)

# Convert to absolute values
sobel_x = cv2.convertScaleAbs(sobel_x)
sobel_y = cv2.convertScaleAbs(sobel_y)

# Combine both directions
sobel_combined = cv2.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0)

# Display results without axes
plt.figure(figsize=(12, 4))

plt.subplot(1, 3, 1)
plt.imshow(sobel_x, cmap="gray")
plt.title("Sobel X")
plt.axis("off")

plt.subplot(1, 3, 2)
plt.imshow(sobel_y, cmap="gray")
plt.title("Sobel Y")
plt.axis("off")

plt.subplot(1, 3, 3)
plt.imshow(sobel_combined, cmap="gray")
plt.title("Combined Edges")
plt.axis("off")

plt.tight_layout()
plt.show()

image

2. Canny Edge Detection — Advanced, Smart Detector

What It Does

  • Combines gradient calculation, non-maximum suppression, double-thresholding and edge tracking
  • Produces thin, clean edges with minimal noise

How It Works (Simplified)

  1. Smooth the image with a Gaussian filter
  2. Compute gradient magnitude and direction
  3. Suppress non-maximum pixels (keep only thin edges)
  4. Threshold to classify strong vs weak edges
  5. Track edges by hysteresis to link weak edges to strong ones

🐍 Python Example (OpenCV)

import cv2
import matplotlib.pyplot as plt

# Load image in grayscale
img = cv2.imread("bird.jpg", cv2.IMREAD_GRAYSCALE)
# Blur for noise reduction
blur = cv2.GaussianBlur(img, (5, 5), sigmaX=1)

# Perform Canny edge detection
edges = cv2.Canny(blur, threshold1=50, threshold2=150)

# Display result without axes
plt.figure(figsize=(6, 4))
plt.imshow(edges, cmap="gray")
plt.title("Canny Edges")
plt.axis("off")
plt.show()
  • threshold1: lower bound → potential edges

  • threshold2: upper bound → strong edges

image

🔍 Sobel vs Canny — What’s the Difference?

Feature Sobel Canny
Type Gradient-based Multi-stage edge detection
Output Thick, blurry edges Thin, clean edges
Parameters Kernel size Gaussian size + two thresholds
Use Case Quick, simple edge detection High-quality contours or object edges

✅ Summary

Technique Key Feature Code
Sobel Detects vertical/horizontal gradients cv2.Sobel()
Canny Smart edges with thresholding cv2.Canny()