Sub topic 9
The Laplacian and Zero-Crossing Edges
Assume you know basic Python, NumPy arrays, grayscale images, and how to read images with OpenCV. You should also understand that a pixel stores an intensity value. This chapter uses those ideas to detect edges with the Laplacian, a second-derivative filter.
What the Laplacian measures
The first derivative measures how quickly intensity changes in one direction. Sobel filters estimate these horizontal and vertical changes:
G_x = \frac{\partial I}{\partial x}, \qquad G_y = \frac{\partial I}{\partial y}The Laplacian combines second derivatives:
\nabla^2 I =
\frac{\partial^2 I}{\partial x^2}+
\frac{\partial^2 I}{\partial y^2}It responds to places where the intensity change itself changes quickly. A bright-to-dark transition and a dark-to-bright transition produce opposite Laplacian signs.
OpenCV approximates this operation with cv2.Laplacian:
laplacian = cv2.Laplacian(image, depth, ksize=3)The ksize argument selects the derivative kernel size. A larger kernel considers a wider neighborhood but can remove fine detail.
| Method | Derivative order | Typical use |
|---|---|---|
| Sobel | First | Directional gradients and gradient magnitude |
| Laplacian | Second | Fast response to rapid intensity changes |
| Canny | Multiple stages | A cleaner, connected edge map |
Preserve the sign
A Laplacian contains positive and negative values. Do not calculate it directly into an unsigned 8-bit array. Negative values would be clipped or wrapped, destroying information needed for zero-crossing detection.
Use cv2.CV_32F or another signed depth. You can create a display image separately:
display = cv2.convertScaleAbs(laplacian)A zero crossing is a location where the Laplacian changes sign between neighboring pixels. These sign changes often mark the center of an edge. Because noise also creates rapid sign changes, blur the image first and ignore crossings whose response is too small.
import cv2
import numpy as np
image = cv2.imread("input.jpg", cv2.IMREAD_GRAYSCALE)
if image is None:
raise FileNotFoundError("input.jpg could not be read")
blurred = cv2.GaussianBlur(image, (5, 5), 0)
laplacian = cv2.Laplacian(blurred, cv2.CV_32F, ksize=3)
center = laplacian[1:-1, 1:-1]
neighbors = [
laplacian[:-2, 1:-1],
laplacian[2:, 1:-1],
laplacian[1:-1, :-2],
laplacian[1:-1, 2:],
]
has_positive = np.logical_or.reduce([value > 0 for value in neighbors])
has_negative = np.logical_or.reduce([value < 0 for value in neighbors])
crossing = ((center > 0) & has_negative) | ((center < 0) & has_positive)
crossing &= np.abs(center) > 5
zero_edges = np.zeros_like(image)
zero_edges[1:-1, 1:-1] = crossing.astype(np.uint8) * 255
cv2.imwrite("laplacian_display.png", cv2.convertScaleAbs(laplacian))
cv2.imwrite("zero_crossings.png", zero_edges)The value 5 is a response threshold, not an intensity threshold. Raising it suppresses weak crossings. Lowering it detects more detail but also more noise.
Exercise
Why should a Laplacian be computed with cv2.CV_32F instead of directly with an unsigned 8-bit output?
Show answer
The Laplacian has positive and negative values. A signed or floating-point output preserves those signs so zero crossings can be detected.