← All articles
BlogComputer Vision

How does edge detection work in computer vision?

Computer VisionBy Sotiris Spyrou·Published 2026-07-30
How does edge detection work in computer vision?

How does edge detection work in computer vision?

Edge detection works by calculating the mathematical gradient of an image to identify regions where pixel intensity changes sharply. Computer vision algorithms use spatial filters, called kernels, to slide across the image and highlight these rapid transitions, which typically correspond to physical boundaries between objects. By isolating these boundaries, the system reduces the amount of data it needs to process while preserving the structural shape of the scene.

When you look at a photograph, your brain naturally separates a dark building from a bright sky. A computer only sees a grid of numbers representing pixel brightness. To make sense of this grid, algorithms must locate the lines where one object ends and another begins. This process strips away surface textures and colours, leaving behind a wireframe representation. This structural map forms the foundation for more advanced tasks like object recognition, motion tracking, and 3D reconstruction. As a student preparing for an exam, you need to understand both the underlying calculus and the practical matrix operations that make this possible.

The mathematics of finding boundaries

At its core, edge detection is an application of differential calculus applied to a discrete grid of pixels. In a continuous one-dimensional mathematical function, you find the rate of change by taking the first derivative. Where the function transitions rapidly from a low value to a high value, the first derivative peaks. In an image, we are dealing with a two-dimensional function where coordinates map to a pixel intensity value. An edge occurs where there is a steep gradient in this intensity function.

Because digital images are discrete grids of pixels rather than continuous mathematical curves, we cannot calculate a perfect derivative. Instead, we approximate the derivative by comparing adjacent pixel values. If a pixel on the left has an intensity of 10 and the pixel on the right has an intensity of 200, the difference is 190. This large difference indicates a high probability of a vertical edge existing between those two points. Conversely, a region of flat uniform colour will yield a difference near zero.

To capture edges in all orientations, algorithms calculate the image gradient in both the horizontal direction and the vertical direction. The gradient is a vector, meaning it has both a magnitude and a direction. The magnitude tells the algorithm how sharp the edge is, which helps in setting thresholds to ignore minor texture variations. The direction points to the steepest ascent in brightness, which is always perpendicular to the edge itself. Understanding this perpendicular relationship is a frequent requirement in university assessments.

The Sobel operator and convolution

The most common way to approximate these derivatives in practice is through a mathematical operation called convolution. Convolution involves a small matrix of numbers known as a kernel or filter. The kernel slides over the image one pixel at a time. At each position, the algorithm multiplies the kernel values by the corresponding pixel values underneath and sums the results. This single output number replaces the central pixel in the new filtered image.

The Sobel operator is a classic example that you will almost certainly encounter in your module. It uses two separate 3x3 kernels to compute the horizontal and vertical approximations of the derivative. The horizontal kernel typically contains negative numbers on the left column, zeros in the middle, and positive numbers on the right. When this kernel passes over a vertical edge where dark pixels transition to bright pixels left-to-right, the negative numbers multiply the dark pixels, and the positive numbers multiply the bright pixels. The sum creates a massive positive response.

Let us look at a miniature worked example. Imagine a 3x3 patch of an image where the left column consists of black pixels with an intensity of 0, and the middle and right columns consist of white pixels with an intensity of 255. If we apply a simplified horizontal gradient kernel with -1 on the left, 0 in the middle, and 1 on the right, the calculation is straightforward. The -1 multiplies the 0, the 0 multiplies the 255, and the 1 multiplies the 255. The sum is 255. This high positive number flags a strong edge. If the image patch was entirely white, the -1 would multiply 255, and the 1 would multiply 255, resulting in a sum of zero. The kernel successfully ignores flat regions.

Once you have the horizontal gradient and the vertical gradient, you combine them to find the overall edge magnitude. The formula is the square root of the horizontal gradient squared plus the vertical gradient squared, straight from basic geometry. In an exam, if you are asked to calculate the gradient magnitude for a specific pixel, remember to compute both the horizontal and vertical convolutions separately before combining them.

Canny edge detection as the gold standard

While the Sobel operator is excellent for basic gradient calculation, it often produces thick and blurry edges because the gradient peaks over several pixels. John Canny introduced a multi-stage algorithm in 1986 that remains the baseline standard for edge detection today. The Canny edge detector aims to find single-pixel-wide edges, minimise false positives from noise, and ensure that real edges are not broken into fragmented lines.

The first step in the Canny pipeline is noise reduction. Because derivatives are highly sensitive to sudden changes, a single noisy pixel can trigger a massive false edge. The algorithm applies a Gaussian blur to smooth the image before calculating any gradients. This averages out the random noise spikes. Once the image is smoothed, the algorithm computes the gradient magnitudes and directions using standard kernels like Sobel.

The magic of Canny happens in the third step, called non-maximum suppression. The algorithm scans along the gradient direction of every pixel. If a pixel’s gradient magnitude is not the absolute local maximum compared to its neighbours along that direction, its value is suppressed to zero. This thins the thick gradient bands down to crisp lines that are exactly one pixel wide. If your exam asks how Canny achieves thin edges, non-maximum suppression is the exact phrase the examiner wants to read.

The final step is hysteresis thresholding. Instead of using a single cutoff point to decide if a pixel is an edge, Canny uses two thresholds. Any pixel above the high threshold is immediately classed as a strong edge. Any pixel below the low threshold is discarded. Pixels that fall between the two thresholds are marked as weak edges. These weak edges are only accepted as true edges if they physically connect to a strong edge. This prevents continuous boundary lines from breaking apart in regions where the lighting slightly dims.

Why edge detection matters for downstream tasks

Edge detection is rarely the final goal of a computer vision system. It operates as an intermediate feature-extraction step that makes subsequent analysis computationally cheaper and far more accurate. By discarding flat regions of uniform colour, you reduce the amount of image data by orders of magnitude. The system no longer has to process thousands of identical blue pixels in a sky, focusing only on the outline of the aeroplane crossing it.

In classical computer vision pipelines, edge maps are fed into algorithms like the Hough Transform to detect geometric primitives. If you want to detect lane markings for a self-driving car, you first run an edge detector. The Hough Transform then analyses those edge pixels to find mathematically straight lines. Without the initial edge detection step, the Hough Transform would be overwhelmed by the raw pixel data and fail entirely.

Even in modern deep learning approaches like Convolutional Neural Networks, edge detection plays a foundational role. If you visualise the learned weights of the very first layer in a trained neural network, you will see that the network has independently learned to act as an edge detector. The network discovers on its own that finding edges at various orientations is the most logical first step before attempting to identify complex shapes like eyes, wheels, or text. Understanding the classical maths behind edges helps you interpret what these neural network layers are actually doing.

How to answer this in an exam

When faced with a computer vision question on edge detection, mark schemes heavily reward precision in your terminology. Do not just write that the algorithm looks for changes in colour. State explicitly that it computes the spatial gradient of image intensity. If asked to compare operators, focus on the specific trade-offs. Sobel is computationally cheap but sensitive to noise. Canny is computationally expensive but provides continuous, thin boundaries.

You will often be asked to perform a manual convolution on a tiny 3x3 or 4x4 matrix. Show every single step of your working. Write out the element-wise multiplication before you write the final sum. If you make an arithmetic error in the final addition, a lenient examiner will still award method marks if they can clearly see you understood how to align the kernel over the image patch. Always double-check your signs, as mixing up positive and negative weights is the most common reason students drop marks here.

If you need to revise this practically, the Full Marks Press Computer Vision guide covers this with worked exam questions, walking you through exact matrix calculations and Canny pipeline diagrams. Practising with real past-paper scenarios is the only way to ensure the mathematical steps become second nature before you walk into the exam hall.

Frequently asked questions

What is the difference between an edge and a boundary? An edge is a low-level image feature defined by a sharp local change in pixel intensity. A boundary is a higher-level concept representing the physical outline of an object. Edge detectors find edges, which we hope correspond to object boundaries, though shadows and surface textures can create edges that are not true physical boundaries.

Why do we use a Gaussian filter before edge detection? Edge detection relies on mathematical derivatives, which amplify high-frequency noise in an image. A Gaussian filter acts as a low-pass filter, smoothing out this random noise so the derivative operator only triggers on significant structural changes in the scene rather than camera static.

What does the gradient direction tell us? The gradient direction indicates the angle of the steepest change in pixel intensity. This direction is always strictly perpendicular to the physical line of the edge itself. Algorithms use this angle during non-maximum suppression to accurately thin the edge down to a single pixel.

Can edge detectors handle colour images? Most classical edge detectors first convert colour images to greyscale to simplify the maths to a single intensity channel. However, advanced methods can compute gradients for red, green, and blue channels separately and combine them to find edges that only appear as colour transitions without any corresponding brightness changes.

You can grab a free sample copy at fullmarkspress.com/free.

Revising this for an exam? The Full Marks Press guides cover it with worked exam questions and mark schemes. Get a free copy.

ShareXLinkedInWhatsApp
Sotiris Spyrou

Practitioner and author at Full Marks Press, a Verity AI imprint.