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 6×86 \times 8 image tensor X where the middle columns are black (00) and the outer columns are white (11). Next, we define a 1×21 \times 2 kernel K with values [1.0,1.0][1.0, -1.0]. 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

Contributors are:

Who are from:

Tags

D2L

Dive into Deep Learning @ D2L