Skip to content
BoKSA

Computer vision

Edge Detection

Introduction

An edge is a place where brightness changes quickly: the rim of a part, a line on the floor, the boundary between a label and the background. Edge detection finds those places so later steps can measure a shape, follow a line, or hand a region to object detection or blob detection.

You do not need a full vision library to understand the idea. An image is a grid of intensities. A strong edge is a steep step in that grid. This article is the gradient, the Sobel operator, and the related operators you will see named next to it.


What an edge is

Take one row of a grayscale image. Dark pixels, then a jump, then bright pixels:

1
2
3
20 20 22 21  180 190 185 188
              ^
              edge

The jump is the edge. Colour images are usually converted to grayscale first, or the same idea is applied per channel. Slow shading (a soft shadow across the table) is a weak change. A machined edge in decent light is a strong one.

Write the intensity at column x and row y as I(x, y). The gradient is how fast I changes:

Piece Meaning
Horizontal change brightness difference toward the right
Vertical change brightness difference toward the bottom
Magnitude how strong the edge is
Direction which way the edge faces (across the boundary)

A large magnitude means a strong edge. The direction tells you the edge runs across that direction: a vertical boundary has a strong horizontal change.

In plain terms

The gradient is a slope on the brightness surface. A cliff is an edge. A gentle hill is shading. Magnitude is how steep the cliff is. Direction is which way it faces.

Noise looks like tiny cliffs everywhere. A small blur before the gradient keeps the real outline and softens the speckles. If every pixel becomes an edge, blur first or raise the magnitude cutoff.


Sobel

The Sobel operator estimates the horizontal and vertical changes with two 3×3 kernels. You lay each kernel on a pixel and its eight neighbours, multiply, and add.

Horizontal kernel Gx (responds to a vertical edge):

1
2
3
-1   0  +1
-2   0  +2
-1   0  +1

Vertical kernel Gy (responds to a horizontal edge):

1
2
3
-1  -2  -1
 0   0   0
+1  +2  +1

The centre column of Gx is zero and the sides have opposite signs, so a flat region cancels out and a left-to-right jump does not. The 2 in the middle row weighs the closest neighbours a bit more than the corners.

Combine the two results into a strength. The usual magnitude is:

1
G = sqrt(Gx * Gx + Gy * Gy)

A cheaper lab approximation is G ≈ |Gx| + |Gy|. Then keep pixels where G is above a threshold you choose. Those pixels are your edge map.

Sobel is a good first operator because the kernels are small, the meaning is visible, and it runs on a modest CPU. It also responds to noise and to thick, soft transitions, so the raw output is a band of strong pixels, not a one-pixel line.


You will meet these names beside Sobel. Each one still looks for a fast change in intensity. They differ in the kernel and in what they do after the gradient.

Operator What it adds
Prewitt Same 3×3 idea as Sobel, with equal weights (1 instead of 2). Slightly simpler, similar edges.
Laplacian A second-difference kernel. Edges show up where the result crosses zero. Sensitive to noise, so it is often used after a blur.
Canny A short pipeline: blur, Sobel-style gradient, thin each edge to a ridge, then two thresholds (strong edges kept, weak edges kept only when they touch a strong one). The usual choice when you want a clean line rather than a band.

Laplacian of Gaussian and difference of Gaussians show up again in Blob detection, where the goal is a spot rather than a long boundary.

Pick Sobel or Prewitt when you want a fast strength image. Pick Canny when a later step needs a thin contour (measure a gap, follow a tape line). Always look at the picture after the threshold. A cutoff that looked fine on one bench fails when the lamp moves.


What you use the edges for

Goal Why edges help
Line following The tape is a long edge or a pair of edges
Part outline The silhouette is a closed chain of edge pixels
Measurement Distance in pixels between two parallel edges, after calibration
A simpler detector Edges feed a shape check when a full object detector is more than the task needs

Edges do not name the object. A strong rectangle might be the part, the shadow, or the table rim. Naming is object detection. Grouping a filled region is blob detection. Driving a robot from either result is vision-guided robotics.


Relevant topics


Starting points

  1. Sketch one image row with a dark-to-bright step and mark where the horizontal change is largest.
  2. Apply the Gx kernel by hand to a 3×3 patch that is dark on the left and bright on the right. Confirm the result is non-zero.
  3. Apply the same kernel to a flat 3×3 patch and confirm it cancels.
  4. Compare a Sobel magnitude image with a Canny image of the same photo and say which one you would use for a line follower.

Focus points

  • Grayscale and blur come before a sharp threshold. Noise is a field of fake edges.
  • Magnitude and direction answer different questions: how strong, and which way.
  • Sobel draws a band. Thin it (Canny, or a later step) when you need a single-pixel line.
  • Light moves edges. A shadow is a real intensity step and will show up.
  • An edge map does not name the object. Pair it with a blob, a shape rule, or a detector before you grasp.

Key points

  • An edge is a rapid change in image intensity. The gradient measures its strength and direction.
  • Sobel estimates that gradient with two 3×3 kernels, Gx and Gy, then combines them into a magnitude.
  • Prewitt is the equal-weight cousin. Laplacian looks for a second-difference zero crossing. Canny thins the gradient and applies two thresholds.
  • Use the edge map as an input to measurement, blobs, or detection, and check it under the lighting you will actually run.