Image Processing & Filtering

image gradient

The image gradient measures how quickly brightness changes from one pixel to the next, and in which direction it changes fastest. Think of the grayscale image as a hilly landscape where bright pixels are high ground and dark pixels are low; the gradient at each point is the steepest uphill slope, an arrow whose length says how steep and whose heading says which way. Where the image is flat (a smooth wall) the gradient is near zero; where it crosses an edge (wall to window) the gradient is large. This is why the gradient is the foundation of edge detection.

Mathematically the gradient is the vector of partial derivatives, the symbol nabla applied to the image f gives (df/dx, df/dy), the rate of change horizontally and vertically. From these two components you compute two useful quantities: the gradient magnitude, sqrt(Gx^2 + Gy^2) (sometimes approximated as |Gx| + |Gy|), which is the edge strength, and the gradient orientation, atan2(Gy, Gx), the direction of steepest increase, which points perpendicular to the edge. On a discrete pixel grid the derivatives are estimated by finite differences, the simplest being neighbouring-pixel subtraction, and in practice by small kernels such as Sobel or Scharr.

A critical subtlety is that differentiation amplifies noise: subtracting noisy neighbours makes the noise relatively larger, so a raw gradient of a noisy image is dominated by speckle. The standard remedy is to smooth before differentiating, and because differentiation and convolution commute, this is done in one step using the derivative of a Gaussian kernel. The gradient is the workhorse beneath an enormous amount of computer vision: the Canny edge detector, the histogram-of-oriented-gradients (HOG) descriptor, SIFT keypoint orientation, optical flow, and the spatial-gradient cues that even modern deep networks implicitly learn in their first layers.

At a vertical black-to-white edge, Gx is large and positive while Gy is near zero, so the gradient magnitude spikes and the orientation reads 0 degrees (pointing across the edge), exactly the cue an edge detector keys on.

Why it matters: nearly every classical edge and feature detector is just a recipe for computing, thresholding, or histogramming the gradient. Understanding gradient magnitude and orientation, and the need to smooth first, demystifies Canny, HOG, and SIFT all at once.

Also called
intensity gradientspatial derivative