5
Filters and Convolution
Overview
At this point, we have all of the basics at our disposal. We understand the structure of the library as well as
the basic data structures it uses to represent images. We understand the HighGUI interface and can actually
run a program and display our results on the screen. Now that we understand these primitive methods
required to manipulate image structures, we are ready to learn some more sophisticated operations.
We will now move on to higher-level methods that treat the images as images, and not just as arrays of
colored (or grayscale) values. When we say “image processing,” we mean just that: using higher-level
operators that are defined on image structures in order to accomplish tasks whose meaning is naturally
defined in the context of graphical, visual images.
Before We Begin
There are a couple of important concepts we will need throughout this chapter, so it is worth taking a
moment to review these ideas before we dig into the specific image processing functions that make up the
bulk of this chapter. We will need to learn two main concepts: first, we’ll need to understand filters (also
called kernels) and how they are handled in OpenCV. Next, we’ll take a look at how boundaries are
handled, and what happens when OpenCV needs to compute something that is a function of the area around
a pixel if that area spills off of the edge of the image.
Filters, Kernels, and Convolution
Most of the functions we will discuss in this chapter are special cases of a general concept called image
filtering. A filter is any algorithm that starts with some image 𝐼(𝑥, 𝑦 ) and computes a new image 𝐼’( 𝑥, 𝑦) by
computing for each pixel location 𝑥, 𝑦 in 𝐼’ some function of the pixels in 𝐼 that are in some area around
that point. The template that defines both this area’s shape, as well as how the elements of that area are
combined, is called a filter or a kernel.
1
In this chapter, many of the important kernels we encounter will be
1
These two terms can be considered essentially interchangeable for our purposes. The signal processing community
typically prefers the word filter, while the mathematical community tends to prefer kernel.
Get Learning OpenCV, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.