Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Gradients and edges, recalled

~12 min

An edge is a boundary of an object, a lane marking, the outline of a face. Mathematically, it's a place where an image's brightness changes fast. digital-image-processing built the exact tool for measuring "changes fast": the gradient. Chapters 4 and 13 both start from an image gradient directly. This section restates the gradient, restates the two named kernels that compute it cheaply, and stops exactly where those chapters pick up.

The gradient is an arrow. At any point, it points toward the direction a function grows fastest. Its length says how fast. Calculus computes that arrow directly, for a smooth surface. But an image has no "in between" two pixels — there's nothing there for a derivative to measure. So the fix is to compare a pixel to its actual neighbors instead. That comparison stands in for the derivative calculus would have computed. A plain, single check — one pixel minus its left neighbor — works. But a single noisy pixel throws it off completely.

Sobel and Prewitt fix that noise problem the same way. Instead of checking just two pixels, they check several and average, using one 3\times3 grid — three rows times three columns of numbers, the same "times" as ordinary multiplication. That grid blends a horizontal derivative (the same left-versus-right check) with a vertical smoothing pass. One bad pixel gets watered down by its neighbors, instead of skewing the answer on its own. Sobel weights the middle row of that smoothing pass a little more heavily than Prewitt does. That's a small tweak, not a different idea. Either kernel's output is large exactly where a human eye would call something an edge. It's near zero on a flat, quiet patch. The arrow's length really is measuring "how edge-like is this spot."

For a smooth function f(x,y), the gradient \nabla f = \left(\partial f/\partial x,\ \partial f/\partial y\right) is a vector. It points in the direction f increases fastest, and its magnitude |\nabla f| equals that fastest rate of increase. An image I(x,y) is defined only at integer pixel coordinates, so its partial derivatives can't be taken in the calculus sense. digital-image-processing's fix is a finite-difference approximation: compare a pixel to its neighbors, computed by convolution with a small kernel instead of by calculus.

Two named 3\times3 kernels compute this approximation while resisting noise. Each one differentiates in one direction and smooths in the other, at the same time. The Prewitt kernel for the horizontal component is H_x^P = \begin{bmatrix}-1&0&1\\-1&0&1\\-1&0&1\end{bmatrix}, a horizontal difference averaged uniformly down each column. The Sobel kernel uses the identical structure, but weights the center row more heavily: H_x^S = \begin{bmatrix}-1&0&1\\-2&0&2\\-1&0&1\end{bmatrix}. This is a small refinement, giving slightly better noise resistance at the same cost. Sobel is the one nearly every real system reaches for. A vertical kernel H_y — rotate either grid 90° — gives the second gradient component. Combine both, |\nabla I| = \sqrt{G_x^2+G_y^2}, and you get exactly this section's opening claim, made computable: large where the image changes fast in either direction, near zero on a flat patch.

Quick review

Practice from earlier coursework, pulled in for this topic.

From Digital Image Processing

Why can't an image's gradient be computed using ordinary calculus derivatives directly?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Every pixel has a well-defined stored brightness value -- that's not the obstacle.

The gradient is exactly as applicable to a two-variable function like an image as it is elsewhere.

Periodicity is not a requirement for computing an ordinary derivative.

Why

A true derivative requires a continuum of input values; an image is defined only at discrete pixel coordinates, so a finite-difference approximation is needed instead.

An image is only defined at whole-number pixel coordinates.

From Digital Image Processing

In a perfectly flat (constant-value) region of an image, the gradient magnitude is...

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

255 would only occur for a specific coincidental jump, not for a flat region.

The gradient is well-defined (and small) in a flat region, not undefined.

The gradient magnitude measures RATE OF CHANGE, not the pixel value itself.

Why

With no brightness change to measure in a flat region, the gradient magnitude is near zero there.

Compare a flat, uniform region to a region with a sharp transition.

Formula

The Sobel and Prewitt Operators

Recalled from Digital Image Processing · Sobel and Prewitt operators — first taught there.

🔖

The Prewitt operator computes the horizontal and vertical gradient components with H_x^P = \begin{bmatrix}-1&0&1\\-1&0&1\\-1&0&1\end{bmatrix}, \qquad H_y^P = \begin{bmatrix}-1&-1&-1\\0&0&0\\1&1&1\end{bmatrix}. Each factors as a central-difference kernel in one direction and a uniform box-average in the other — differentiate one way, smooth the other, in a single kernel. The Sobel operator uses the identical structure with a weighted average instead of a uniform one, H_x^S = \begin{bmatrix}-1&0&1\\-2&0&2\\-1&0&1\end{bmatrix}, weighting the center row (or column, for H_y^S) twice as heavily — a discrete approximation closer to Gaussian-weighted smoothing, giving Sobel a slightly better noise response than Prewitt at identical cost.

Sobel Gx, stepped through on a vertical edge

Step through the output grid -- the output is large and positive right where the image jumps from dark to bright left-to-right, and near zero on the flat dark and flat bright regions on either side.

Loading interactive…

Worked example

Computing Sobel Gx by hand on a flat region

  1. 1

    Apply the Sobel H_x^S kernel to the flat 3\times3 window \begin{bmatrix}220&220&220\\220&220&220\\220&220&220\end{bmatrix} (deep inside the bright region, no edge nearby). Every column of the window has the identical value, so each kernel column's own contribution cancels against the opposite column.

  2. 2
    -1(220)+0(220)+1(220)\ -2(220)+0(220)+2(220)\ -1(220)+0(220)+1(220) = 0

    Zero, at every row. A perfectly flat region has no left-right change to detect. That matches this section's own claim: the gradient magnitude is near zero away from an edge.

Loading interactive…

Worked example

Computing Sobel Gx by hand right at the edge

  1. 1

    Now apply the same kernel to the window straddling the edge, \begin{bmatrix}20&220&220\\20&220&220\\20&220&220\end{bmatrix}. The center column is identical in both windows (220 throughout); only the left column's values changed, from 220 to 20.

  2. 2
    -1(20)+0(220)+1(220)\ -2(20)+0(220)+2(220)\ -1(20)+0(220)+1(220)= (220-20) + 2(220-20) + (220-20) = 200+400+200 = 800

    A large positive response, right at the edge. Compare that to the previous example's clean 0, deep inside a flat region. This is exactly the "large change here, none there" signature that makes the gradient magnitude |\nabla I| into an edge detector.

Loading interactive…

Where this goes next

Feature detection and camera calibration

Chapter 4's Harris corner detector computes exactly I_x and I_y — this section's Sobel outputs — as its very first step. A corner turns out to be a point where the gradient is large in two different directions at once, not just one. Chapter 13's stereo-vision matching also leans on gradient-rich regions. A flat patch with no gradient gives a matching algorithm nothing distinctive to lock onto between two camera views.

Check your understanding (counts toward your progress)

Why can't an image's partial derivatives be computed in the ordinary calculus sense?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Every image pixel DOES store a brightness value -- that's not the obstacle to taking a derivative.

The gradient is a 2D-function concept from the start; nothing about it requires three dimensions.

Calculus handles integer-valued functions fine in general -- the real obstacle is that an image simply has no values between adjacent pixels to take a limit across.

Why

A derivative requires values arbitrarily close together to compare; an image has no data between one pixel and the next, so a finite-difference approximation stands in instead.

Explain, in terms of how each kernel weights its neighborhood, why Sobel is more resistant to a single noisy pixel than a plain two-pixel central difference.

What is the key structural difference between the Sobel and Prewitt kernels?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Both kernels work identically on grayscale or per-channel color data -- color isn't the distinguishing factor.

Both are 3\times3 kernels -- their size is identical.

Both kernels have a horizontal AND a vertical version (rotate the given kernel 90 degrees) -- direction isn't the difference between them.

Why

Sobel and Prewitt share the identical differentiate-and-smooth structure; Sobel just gives the center row of the smoothing pass double weight.

The gradient of an image at a point is largest in magnitude where the image...

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

A flat region has no brightness change to measure at all -- the gradient there is near zero, not large.

A single bright pixel value alone says nothing about how fast brightness is CHANGING nearby -- the gradient measures change, not absolute brightness.

The gradient depends on local brightness change, not on physical position in the image -- the center is not inherently special.

Why

The gradient measures the rate of change; a sharp brightness transition (an edge) is exactly where that rate is largest.

A 3\times3 window has left column 30, center column 30, right column 230 (every row identical). Apply the Sobel H_x kernel and report the result.

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

Each row contributes (230-30) once for the top/bottom rows and 2(230-30) for the center row -- 200+400+200=800, a large positive response right at this left-to-right jump.

Correct answer

Also accepted:

Why

Apply the Sobel H_x kernel \begin{bmatrix}-1&0&1\\-2&0&2\\-1&0&1\end{bmatrix} to the flat window with every entry equal to 90. What is the result?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

Every column of a perfectly flat window is identical, so the negative and positive kernel entries cancel exactly -- 0, confirming a flat region has no left-right change to detect.

Correct answer

Also accepted:

Why

Practice: Computing and interpreting image gradients (4 problems · doesn’t affect your progress)

Why can't an image's gradient be computed using ordinary calculus derivatives?

Apply the Sobel H_x kernel to a flat 3\times3 window where every entry is 150. What is the result?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

A perfectly flat window has no left-right brightness change for the kernel to detect -- every term cancels, giving 0.

Correct answer

Also accepted:

Why

0

A 3\times3 window has left column 50, center column 50, right column 250 in every row. Apply Sobel H_x.

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

(250-50) + 2(250-50) + (250-50) = 200+400+200 = 800, a strong positive response at this sharp left-to-right jump.

Correct answer

Also accepted:

Why

800

Compared to a plain two-pixel central difference, Sobel and Prewitt are more resistant to noise because they...

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

Differentiating in one direction while simultaneously averaging in the other dilutes the effect of any single noisy pixel.

also average across neighboring rows while differentiating

Feeling good about this section?

Take the section quiz

Every answer gets a full explanation at the end.

Graph settings

Calculator

Sign in to save highlights

Insert an equation

  • No matches.