Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 54 ยท Edges, Contours and Shapes

Gradients, Sobel and Laplacian

Learn how image gradients find edges. Understand cv2.Sobel, cv2.Laplacian and cv2.Scharr, why CV_64F matters, and rebuild Sobel from scratch in NumPy.

An edge is a place where brightness changes quickly: the border between a dark table and a white sheet of paper, the outline of a face against a wall. If you can measure how fast brightness changes at every pixel, you can find edges. That measurement is called the image gradient, and it is the foundation of almost everything in this chapter: Canny edges, contours, line detection and feature points all start here.

OpenCV gives you three ready-made tools: cv2.Sobel, cv2.Scharr and cv2.Laplacian. They are all small convolution-style filters, close relatives of the kernels you met in Kernels and filter2D. In this lesson you will see what they compute, how to call them without losing data, and you will rebuild Sobel with NumPy so nothing stays a black box.

The real cv2 calls below are shown in read-only blocks because opencv-python is not available in the browser. Every idea also has a runnable NumPy stand-in. The stand-ins use the same kernels and the same arithmetic, so the numbers they print are what OpenCV would compute for the same pixels, apart from border handling and rounding details.

What a gradient is

For a grayscale image, treat brightness as a function of position. The gradient at a pixel is a pair of numbers: how much brightness changes when you step to the right (gx) and how much when you step down (gy). On a flat area both are zero. Across a vertical edge gx is large and gy is near zero. Across a horizontal edge it is the other way around.

On a computer you cannot take a true derivative, but you can take a difference. Subtract the left neighbor from the right neighbor and you get a slope. Run it on one row of pixels:

Python A derivative is a difference

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The slope is zero on the flat parts and peaks (100) in the middle of the ramp. Writing the difference as the kernel [-1, 0, 1] shows why gradients fit the filtering framework: sliding that kernel over an image gives the horizontal derivative everywhere.

From brightness to edges

Brightness Pixels are numbers.
Difference Neighbor minus neighbor.
Gradient A pair, gx and gy.
Edge Where the gradient is large.
1 / 2

The Sobel operator

A plain difference is noisy, because one odd pixel ruins it. The Sobel operator averages first and differences second: it differences horizontally while smoothing vertically with weights 1, 2, 1. The two 3x3 kernels are:

TEXT
sobel_x               sobel_y
[-1  0  1]            [-1 -2 -1]
[-2  0  2]            [ 0  0  0]
[-1  0  1]            [ 1  2  1]

sobel_x responds to brightness changing from left to right, so it lights up on vertical edges. sobel_y is its transpose and lights up on horizontal edges. In OpenCV the call looks like this:

Python
import cv2
import numpy as np

gray = cv2.imread("coins.png", cv2.IMREAD_GRAYSCALE)

gx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)   # dx=1, dy=0
gy = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)   # dx=0, dy=1

magnitude = cv2.magnitude(gx, gy)                  # sqrt(gx**2 + gy**2)
angle = cv2.phase(gx, gy, angleInDegrees=True)     # 0..360 degrees

# Convert back to a displayable 8-bit image
edges_8u = cv2.convertScaleAbs(magnitude)

The third and fourth arguments are the order of the derivative in x and in y. ksize is 1, 3, 5 or 7 (odd); larger kernels smooth more and respond to wider edges. Passing ksize=-1 (or cv2.FILTER_SCHARR) selects the Scharr kernel.

Why CV_64F matters

The second argument, the output depth, is the most common source of bugs. A bright-to-dark edge produces a negative gradient. If you ask for cv2.CV_8U (unsigned 8-bit), OpenCV saturates every negative result to 0 and you silently lose all edges that go from bright to dark. So compute the derivatives in a signed, wide type (cv2.CV_64F or cv2.CV_32F), combine them, and only then scale back to 8-bit with cv2.convertScaleAbs, which takes the absolute value, scales, and saturates. See Data Types, Ranges and Saturation for the background.

cv2.Sobel(gray, cv2.CV_8U, 1, 0) is legal and returns an image, but it only contains one direction of each edge. Use a signed depth first, always.

Magnitude and direction

Two numbers per pixel are awkward, so we usually combine them. The magnitude sqrt(gxยฒ + gyยฒ) says how strong the edge is. A cheaper approximation is |gx| + |gy|, which cv2.addWeighted on absolute values gives you. The direction atan2(gy, gx) says which way brightness increases. The edge itself runs perpendicular to the gradient, a fact Canny edge detection uses in the next lesson.

Now rebuild all of this in NumPy. The stand-in below builds a small image with a bright disk, applies the Sobel kernels with a plain correlation function, and prints the magnitude as text art, then the angles at four points on the disk's border, and finally a Laplacian.

Python Sobel and Laplacian from scratch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The magnitude image is a clean ring: zero in the flat background and zero inside the disk, because there brightness does not change. The four probe pixels show the direction rule. On the left border brightness increases to the right, so gx is positive and the angle is 0 degrees; on the right border it is negative and the angle is 180. Top and bottom borders give 90 and 270 degrees, because in image coordinates y grows downward.

The Laplacian

Sobel is a first derivative. The Laplacian is a second derivative: it measures how the slope itself changes. With the common 3x3 kernel it adds the four neighbors and subtracts four times the center:

Python
lap = cv2.Laplacian(gray, cv2.CV_64F, ksize=1)   # ksize=1 is the [0 1 0; 1 -4 1; 0 1 0] kernel
lap_8u = cv2.convertScaleAbs(lap)

It has no direction (one number per pixel), and it responds to an edge with a pair of values of opposite sign, positive on the dark side and negative on the bright side, with a zero crossing exactly at the edge. In the stand-in output, row 7 reads 180, -540, 0 going from the background into the disk: a positive value just outside, a strongly negative value just inside. The price of the second derivative is noise sensitivity: it amplifies every speck. That is why people blur first (the "Laplacian of Gaussian" idea) and why Sobel is more common for detecting edges, while the Laplacian is popular for measuring sharpness. A well-known trick is the variance of the Laplacian as a blur detector: a sharp image has a high variance, a blurry one a low variance.

Scharr and choosing a kernel

For a 3x3 Sobel, the direction estimate is slightly inaccurate at angles that are not multiples of 45 degrees. Scharr uses the weights 3, 10, 3 instead of 1, 2, 1 and rotates more accurately for the same cost:

Python
gx = cv2.Scharr(gray, cv2.CV_64F, 1, 0)
gy = cv2.Scharr(gray, cv2.CV_64F, 0, 1)

A practical rule: use Sobel with ksize=3 as a default, switch to Scharr when the exact orientation matters, use bigger Sobel kernels when the image is noisy or the edges are wide, and use Laplacian for "how sharp is this?" questions. In all cases, smooth noisy photos first with a Gaussian blur from Blurring and Noise Reduction.

Python Edge strength, direction and a threshold

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here brightness falls toward the lower right, so gx and gy are equal and negative: the angle is -135 degrees (the same direction as 225). In that direction the sum of absolute values (1080) overestimates the true magnitude (about 764) by a factor of the square root of 2, which is why it is only used when speed matters more than accuracy. The last line mimics saturating to 8 bits: all 36 negative responses in gx disappear.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which output depth should you normally pass to cv2.Sobel so that bright-to-dark edges are not lost?

    Gradients can be negative. CV_8U saturates negatives to 0, so half of the edges vanish. A signed wide type keeps them; convert back with cv2.convertScaleAbs afterwards.
  2. 2. A pixel has gx = 0 and a large positive gy. What kind of edge is it on?

    gy measures change from top to bottom, so a large gy with gx near zero means brightness changes vertically, which is what a horizontal edge looks like.
  3. 3. What is special about the Laplacian compared with Sobel?

    The Laplacian measures how the slope changes, has no direction, and flips sign across an edge. Because it amplifies noise, images are usually blurred first.
  4. 4. What does the parameter ksize control in cv2.Sobel?

    ksize is the kernel width: 1, 3, 5 or 7. Bigger kernels smooth more and respond to wider edges, at the cost of fine detail.

Summary

  • The image gradient (gx, gy) measures how fast brightness changes; edges are where its magnitude is large.
  • Sobel differences in one direction while smoothing in the other; sobel_y is the transpose of sobel_x.
  • Always compute derivatives in cv2.CV_64F or CV_32F, then combine with cv2.magnitude and convert with cv2.convertScaleAbs.
  • Magnitude is sqrt(gxยฒ + gyยฒ) and the angle is atan2(gy, gx); the edge runs perpendicular to the gradient.
  • The Laplacian is a noise-sensitive second derivative with zero crossings at edges; Scharr is a more accurate 3x3 Sobel.

Next lesson: Canny Edge Detection.