Computer Vision Edge Detection

Edges in an image mark the boundaries between different regions — where a foreground object ends and the background begins, or where one surface meets another. Detecting edges is one of the fundamental steps in understanding what is inside an image.

What Is an Edge?

An edge is a place in an image where the pixel values change sharply. Imagine drawing a stick figure on white paper. The ink marks are the edges — the boundary between ink and paper. In a photo, the edge of a cup against a wall, the outline of a person, or the horizon line are all edges.

Edge as a Sudden Brightness Change

Pixel brightness along a horizontal scan line (left to right):

  255 ──────────────┐
                    │   ← EDGE HERE (sharp jump)
    0               └──────────────

Position: 1   2   3   4   5   6   7   8   9
Value:   250 248 252 249   5   3   6   4   2

Pixels 1–4 = bright (white area)
Pixels 5–9 = dark (black area)
Edge = between pixel 4 and pixel 5

Gradient: Measuring the Rate of Change

The mathematical way to find edges is to measure the gradient — how fast the brightness changes between neighboring pixels. A large gradient means a strong edge. A small gradient means a smooth, flat region.

Gradient Direction Diagram

Image region with a vertical edge:

  250 | 250 | 10  | 10
  248 | 252 | 8   | 12
  251 | 249 | 11  | 9

Gradient (left–right difference):
  252–10 = 242  ← Large! Strong vertical edge here.

Gradient (top–bottom difference):
  250–248 = 2   ← Small. No horizontal edge.

Edges run PERPENDICULAR to the direction of the large gradient.

The Sobel Operator

The Sobel operator uses two kernels to detect edges — one finds vertical edges and one finds horizontal edges. The results combine to give the overall edge strength at each pixel.

Sobel Kernels

Gx kernel (finds vertical edges):    Gy kernel (finds horizontal edges):
+----+----+----+                      +----+----+----+
| -1 |  0 | +1 |                      | -1 | -2 | -1 |
+----+----+----+                      +----+----+----+
| -2 |  0 | +2 |                      |  0 |  0 |  0 |
+----+----+----+                      +----+----+----+
| -1 |  0 | +1 |                      | +1 | +2 | +1 |
+----+----+----+                      +----+----+----+

Edge magnitude = √(Gx² + Gy²)
Edge direction = arctan(Gy / Gx)

Sobel Applied to a Simple Image

Input (left half = bright, right half = dark):

  250  250  250  10   10   10
  250  250  250  10   10   10
  250  250  250  10   10   10

Sobel Gx output (approximate):
    0    0  480  480    0    0
    ↑
    Bright area has no change → Gx ≈ 0
               ↑
               Middle column has big change → Gx ≈ 480 (edge!)

The Canny Edge Detector

Canny is the most widely used edge detection algorithm. It performs multiple steps to find thin, precise edges while ignoring noise. It was developed by John Canny in 1986 and remains the standard for high-quality edge detection.

Canny's Step-by-Step Process

Step 1: Gaussian Blur
  [Image] → Remove noise first to avoid detecting noise as edges.

Step 2: Gradient Calculation (Sobel)
  [Blurred Image] → Find gradient magnitude and direction at every pixel.

Step 3: Non-Maximum Suppression
  [Gradient Map] → Keep only the pixel with the strongest gradient
                   along each edge direction. Thin the edges to 1 pixel wide.

Step 4: Double Thresholding
  Strong edge:  gradient > High Threshold  → definite edge (keep)
  Weak edge:    Low < gradient < High      → possible edge (check next)
  Non-edge:     gradient < Low Threshold   → discard

Step 5: Edge Tracking by Hysteresis
  Weak edge pixels connected to a strong edge → kept as edge.
  Weak edge pixels not connected to strong → discarded.

Result: [Thin, clean, precise edge map]

Threshold Effect on Canny Output

Threshold SettingEdges DetectedTrade-off
Low thresholdsMany (including weak ones)More false edges (noise)
High thresholdsFew (only strongest)May miss real edges
Balanced thresholdsClean, meaningful edgesRequires tuning per image

Other Edge Detection Methods

Prewitt Operator

Similar to Sobel but uses equal weights for all neighbors. Slightly less noise-resistant than Sobel but simpler to compute.

Prewitt Gx:              Sobel Gx:
-1  0  +1               -1   0  +1
-1  0  +1               -2   0  +2
-1  0  +1               -1   0  +1

Difference: Sobel weights the center row more (value 2 vs 1).
Result: Sobel is more sensitive to edges in the middle of the kernel.

Laplacian of Gaussian (LoG)

This method blurs the image first (Gaussian) then applies the Laplacian operator, which finds pixels where the gradient changes direction — exactly where edges are. It detects edges in all directions simultaneously but is sensitive to noise.

Real-World Applications of Edge Detection

  • Document scanning apps – Detect the edges of a document in a photo to crop and straighten it.
  • Lane detection in cars – Find the edges of lane markings on a road.
  • Medical imaging – Outline the edges of organs or tumors in scans.
  • Barcode scanners – Detect the edges of the black and white bars.
  • Augmented reality – Find the edges of surfaces to overlay virtual objects correctly.

Lane Detection Using Edge Detection

[Camera image of road]
       ↓
[Convert to grayscale]
       ↓
[Gaussian blur]        ← Remove road texture noise
       ↓
[Canny edge detection] ← Find lane marking edges
       ↓
[Focus on lower half]  ← Lane markings are in front of car
       ↓
[Hough Transform]      ← Connect edge points into lines
       ↓
[Lane lines overlaid on original image]

Key Takeaways

  • An edge is a location where pixel brightness changes sharply.
  • The gradient measures how fast brightness changes — large gradient = edge.
  • Sobel operator uses two kernels to find horizontal and vertical edges separately.
  • Canny edge detector is the industry standard — it blurs, finds gradients, thins edges, and uses double thresholding.
  • Edge detection is used in document scanning, lane detection, medical imaging, and more.

Leave a Comment

Your email address will not be published. Required fields are marked *