Skip to main content

Image Filtering and Edge Detection

Inspect Gaussian smoothing and Sobel filters as local weighted sums, understand signed gradients and Canny edges, and connect fixed filters to CNNs.

A pixel value alone says little about local structure. A dark pixel could lie inside a flat region or next to a sharp boundary. An image filter examines a neighborhood and combines its values, producing a response that answers a more useful question: how smooth is this patch, or how quickly does brightness change across it?

Follow one weighted sum

A kernel is a small array of weights. Place it over an image patch, multiply corresponding entries, and add the products. Slide it to compute the next output. The explorer performs stride-one cross-correlation: it uses the kernel as displayed, without reversing it.

Switch between blur and Sobel, then move the selected patch. At a vertical boundary, Sobel x responds strongly while Sobel y stays near zero. The direction names describe the derivative, not the orientation of the boundary itself.

A filter is a local weighted sum
Filter
Edge direction
Input · selected 3 × 3 patch
Output · valid 5 × 5
2
2

Input values

002550025500255

Kernel weights

-101-202-101
Weighted sum
1020.00

Calculated cross-correlation with no padding. Sobel display maps −1020 to black, 0 to mid-gray and +1020 to white; the signed readout keeps the actual result. The blur uses its ordinary 0–255 scale.

With a 7 × 7 image and a 3 × 3 kernel, there are 5 × 5 valid placements. The output is smaller because no artificial pixels are added beyond the boundary. Padding could preserve its size, but the chosen border rule would then affect responses near the edges.

Smoothing averages; derivatives compare

The blur kernel here is a normalized, Gaussian-like binomial kernel:

K=116[121242121]K = \frac{1}{16} \begin{bmatrix}1&2&1\\2&4&2\\1&2&1\end{bmatrix}

Its weights sum to one. A constant patch therefore keeps the same value, while an isolated bright sample spreads its contribution across nearby outputs. Smoothing can reduce fine noise, but it also removes small details. OpenCV documents averaging, Gaussian, median, and bilateral filters; these operations do not all make the same tradeoff. OpenCV smoothing

Sobel x instead compares the right and left sides of a patch:

Kx=[−101−202−101]K_x = \begin{bmatrix}-1&0&1\\-2&0&2\\-1&0&1\end{bmatrix}

For rows [0, 0, 255], the response is 255 + 510 + 255 = 1020. Reversing that contrast produces −1020. Storing either result directly in an unsigned 8-bit image would lose information. Compute in a suitable signed or floating-point type, and separately choose a display scale.

A gradient response is not an object boundary

Horizontal and vertical derivatives give a gradient magnitude and direction:

G=Gx2+Gy2,θ=atan2⁡(Gy,Gx)G = \sqrt{G_x^2 + G_y^2}, \qquad \theta = \operatorname{atan2}(G_y, G_x)

Texture, shadows, and sensor noise can all create large gradients. Conversely, an object's boundary may have weak contrast. Edge detection measures image structure; recognizing an object requires additional evidence.

Canny builds a thinner edge map by combining smoothing, gradient estimation, suppression of non-maximal responses along the gradient direction, and hysteresis thresholding. Strong responses seed edges, while connected weaker responses can be retained. Its suppression step operates on local gradient responses, not detector boxes. The explorer above shows only filtering, not the complete Canny algorithm. OpenCV Canny tutorial

From fixed filters to learned features

These kernels are authored to detect a particular signal. A CNN learns its filter weights from a training objective and combines responses across channels and layers. The local weighted sum remains recognizable, but deeper features need not correspond to a simple human-named edge.

Continue to convolution for channels, stride, and padding, then image classification to see how spatial responses become an image-level prediction.

If you found this explanation helpful, consider sharing it with others.

Mastodon