Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 54 ยท Image Processing

Kernels and filter2D

Apply your own kernels with cv2.filter2D: convolution versus correlation, sharpening and emboss kernels, border types, and a 2D filter written in numpy.

Blurring, sharpening, edge detection and embossing look like different tools, but under the hood they share one operation: slide a small grid of numbers, a kernel, over the image, multiply the kernel with the pixels beneath it, add up the products, and write the sum into the output. Only the numbers in the kernel change. OpenCV exposes this general operation as cv2.filter2D, and once you understand it, many named functions stop being mysterious.

As in the rest of this track, cv2 itself cannot run on this site. The OpenCV calls appear in read-only blocks, and the live examples implement the same arithmetic in numpy so you can run and modify it.

The operation, and one filter2D call

Python
import cv2
import numpy as np

img = cv2.imread("photo.png", cv2.IMREAD_GRAYSCALE)

sharpen = np.array([[ 0, -1,  0],
                    [-1,  5, -1],
                    [ 0, -1,  0]], dtype=np.float32)

out = cv2.filter2D(img, -1, sharpen)

The second argument is the output depth. -1 means "same as the source", so an 8-bit image gives an 8-bit result. That detail matters: the sum is computed in higher precision, then rounded and saturated into 0..255, so negative results become 0 and large ones become 255. Pass cv2.CV_32F instead if you need the raw values, for example when the kernel produces negative numbers you want to keep.

Optional arguments are anchor (the kernel position that corresponds to the output pixel, by default the center), delta (a number added to every result) and borderType (what to do near the edges, covered below). Kernels should be odd in both dimensions so the center is clear. See Blurring and Noise Reduction for why.

Convolution versus correlation

Strictly, convolution flips the kernel (both left-right and up-down) before sliding it, while correlation does not. The OpenCV documentation is explicit that filter2D computes correlation. For a symmetric kernel such as a box or Gaussian blur, the two are identical and nobody notices. For an asymmetric kernel such as emboss or a directional edge detector, the difference is a flipped result. If you need true convolution, flip the kernel yourself with cv2.flip(kernel, -1) before calling filter2D.

The clearest way to see it is to filter a single bright pixel, called an impulse. With true convolution the output reproduces the kernel as written. With correlation it reproduces the kernel rotated by 180 degrees.

Python Correlation or convolution?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The correlation output is the kernel rotated by 180 degrees (it starts with 9 and ends with 1), and the convolution output matches the kernel as written. For a symmetric box kernel the two functions agree everywhere.

Common kernels

One operation, many looks

Blur Positive weights that sum to 1.
Sharpen A big center, negative neighbors.
Emboss Negative on one side, positive on the other.
Edges Weights that sum to 0, so flat areas vanish.

The sum of the weights tells you a lot about the effect.

1 / 2

Look at the sum of a kernel before you use it. A sum of 1 keeps overall brightness (blur, sharpen). A sum of 0 removes flat regions and leaves only changes (edge kernels, which turn flat areas black). A sum above 1 brightens, below 1 darkens.

The sharpen kernel above has a sum of 1: it keeps the center pixel and subtracts the neighbors' average, so any difference between a pixel and its neighbors gets amplified. The emboss kernel [[-2,-1,0],[-1,1,1],[0,1,2]] also sums to 1, but its weights run diagonally from negative to positive, so edges facing one direction look raised. Emboss results are often shifted with delta=128 so that "no change" appears mid-gray.

Because the sums can go beyond the 8-bit range, saturation shows up right away at strong edges. The example computes the raw values and the saturated ones side by side.

Python Sharpening and embossing with saturation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The emboss rows show the other use: after removing the original brightness and adding 128, flat areas become mid-gray (128) and the edge shows up as a lighter band (188) on both sides of the step, a "raised" look.

Notice the overshoot in the sharpen row: just left of the edge, the output goes to -50 (below the original 50), and just right of it to 250 (far above 150). Saturation clips the -50 to 0 (and would clip anything above 255), which produces the thin dark and bright halos you see around edges in oversharpened photos. For big edges, a milder kernel or cv2.addWeighted with the original image gives a gentler result (see Arithmetic, Bitwise Ops and Masking).

Borders and padding

At the image edge, part of the kernel hangs outside. OpenCV conceptually extends the image with a border type, and the default for filter2D is BORDER_REFLECT_101. The numpy equivalent of each type is given in the table.

OpenCV border Extends abcdefgh as np.pad mode
BORDER_CONSTANT 000|abcdefgh|000 constant
BORDER_REPLICATE aaa|abcdefgh|hhh edge
BORDER_REFLECT cba|abcdefgh|hgf symmetric
BORDER_REFLECT_101 (default) dcb|abcdefgh|gfe reflect
BORDER_WRAP fgh|abcdefgh|abc wrap

The names are confusing: BORDER_REFLECT repeats the edge pixel, while BORDER_REFLECT_101 mirrors around it and does not repeat it. Zero padding darkens the edges of blurred images, which is why it is rarely the right choice for photos. The next example blurs a small image with different borders and shows how only the outermost pixels differ.

Python How border types change the edges

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A flat image should stay flat after blurring. Reflect and replicate keep the corner at 90.0, while constant (zero) padding drops it to 40.0 because five of the nine window values are padded zeros. That darkening is the visible "vignette" you get from badly chosen borders.

Writing your own filter, and when to use filter2D

The loop-over-kernel-offsets pattern in the examples is a good model: for a kernel of size k, you do k * k vectorized array operations instead of looping over every pixel. It is still slower than OpenCV, which uses optimized code and, for large kernels, FFT-based convolution. Use filter2D whenever you have a custom kernel, and prefer the dedicated functions when one exists: GaussianBlur, Sobel and Laplacian (later in this track) are separable or specialized and run faster than a general kernel. Separable kernels, such as the Gaussian, can use cv2.sepFilter2D with two 1D kernels, turning k * k multiplications per pixel into 2 * k.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does cv2.filter2D actually compute?

    filter2D slides the kernel without flipping it, which is correlation. For symmetric kernels the result is identical to convolution.
  2. 2. An 8-bit image is filtered with filter2D and ddepth=-1. A computed value of -37 becomes...

    With depth -1 the output is 8-bit, so values are saturated to the 0..255 range. Negatives become 0.
  3. 3. A kernel's weights sum to 0. What is the typical effect on a flat gray region?

    In a flat region, every neighbor has the same value, so the weighted sum is value times zero. Only places where values change produce nonzero output.
  4. 4. Which np.pad mode matches OpenCV's default border for filter2D, BORDER_REFLECT_101?

    BORDER_REFLECT_101 mirrors around the edge pixel without repeating it, which is numpy's reflect mode. numpy's symmetric mode corresponds to BORDER_REFLECT.

Summary

  • A kernel filter multiplies a small grid of weights with each neighborhood and sums the result; cv2.filter2D(img, -1, kernel) does it for any kernel.
  • filter2D computes correlation. Flip the kernel with cv2.flip(kernel, -1) for true convolution; symmetric kernels do not care.
  • Kernel sums matter: 1 keeps brightness (blur, sharpen), 0 keeps only changes (edges).
  • With 8-bit output, results are rounded and saturated, which creates halos at strong edges.
  • The default border is BORDER_REFLECT_101 (numpy reflect); zero padding darkens edges.

Next up: Morphological Operations.