Sub topic 3
What Happens at the Image Border
Assume you know basic Python, NumPy array indexing, grayscale images, and how to run OpenCV. This chapter explains how cv2.filter2D handles pixels that fall outside an image.
Why borders need a rule
A convolution applies a kernel around every output pixel. For a 3×3 kernel, the center pixel needs one neighbor in every direction. A pixel at the top-left corner does not have all those neighbors. OpenCV must create values outside the image before it can calculate the result.
These invented values are called the border extension. The choice affects smoothing, sharpening, and gradient measurements. A kernel with radius r can affect up to r pixels near each border.
Common border modes
OpenCV exposes border modes through the borderType argument. The default for filter2D is cv2.BORDER_DEFAULT, which is the same as cv2.BORDER_REFLECT_101.
| Border mode | What it does | Typical effect |
|---|---|---|
| `BORDER_CONSTANT` | Uses a fixed value, usually zero | Can create artificial edges |
| `BORDER_REPLICATE` | Repeats the nearest edge pixel | Avoids new values but creates flat edges |
| `BORDER_REFLECT` | Mirrors the image and repeats the edge pixel | Smooth continuation with a duplicated edge |
| `BORDER_REFLECT_101` | Mirrors without repeating the edge pixel | OpenCV’s usual balanced default |
For a one-dimensional row [10, 20, 30], a three-value averaging kernel needs one value before 10. BORDER_REPLICATE supplies 10; BORDER_REFLECT_101 supplies 20; BORDER_CONSTANT with value zero supplies 0. Therefore, the first output value differs even though the original image is unchanged.
import cv2
import numpy as np
row = np.array([[10, 20, 30]], dtype=np.float32)
kernel = np.array([[1, 1, 1]], dtype=np.float32) / 3
for mode in (
cv2.BORDER_CONSTANT,
cv2.BORDER_REPLICATE,
cv2.BORDER_REFLECT,
cv2.BORDER_REFLECT_101,
):
result = cv2.filter2D(
row, -1, kernel, borderType=mode, borderValue=0
)
print(mode, result.ravel())borderValue matters only for BORDER_CONSTANT. The other modes calculate their missing values from nearby image pixels.
Choosing a mode
Reflection usually works well for natural images because it continues nearby intensity patterns without introducing a black or white frame. Replication can be suitable when the scene naturally continues with a flat region. Constant padding is useful when the outside of the image has a known meaning, but zero padding is risky for gradients and sharpening.
The same issue appears with cv2.Sobel. Constant padding can produce a strong gradient at the image boundary. That edge may be an artifact of padding rather than a feature in the scene.
Practical checks
Use the same border mode throughout a pipeline when possible. Inspect a thin strip along each edge when debugging. If border artifacts matter, crop away the kernel radius after filtering or pad the image deliberately before processing.
Exercise
Which border mode is a reasonable first choice for smoothing a natural photograph, and why?
Show answer
Use cv2.BORDERREFLECT101, OpenCV's default for filter2D. It mirrors nearby pixels without adding a sharp constant boundary.