Code
Vertical Edge Detection Code Implementation
Vertical edge detection can be implemented programmatically by applying a two-dimensional cross-correlation operation to an image tensor. First, we construct a image tensor X where the middle columns are black () and the outer columns are white (). Next, we define a kernel K with values . We then perform the cross-correlation operation corr2d(X, K) to detect the vertical edges. Finally, applying the same kernel to the transposed image X.t() results in an output of zeros, verifying that the kernel only detects vertical edges.
X = torch.ones((6, 8)) X[:, 2:6] = 0 K = torch.tensor([[1.0, -1.0]]) Y = corr2d(X, K) # Apply the kernel to the transposed image (horizontal edges) corr2d(X.t(), K)
0
1
Updated 2026-05-09
Tags
D2L
Dive into Deep Learning @ D2L