OpenCV  4.2.0
Open Source Computer Vision
Making your own linear filters!

.2.0+dfsg_doc_tutorials_imgproc_imgtrans_filter_2d_filter_2d

Prev Tutorial: Thresholding Operations using inRange
Next Tutorial: Adding borders to your images

Goal

In this tutorial you will learn how to:

  • Use the OpenCV function filter2D() to create your own linear filters.

Theory

Note
The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler.

Correlation

In a very general sense, correlation is an operation between every part of an image and an operator (kernel).

What is a kernel?

A kernel is essentially a fixed size array of numerical coefficients along with an anchor point in that array, which is typically located at the center.

How does correlation with a kernel work?

Assume you want to know the resulting value of a particular location in the image. The value of the correlation is calculated in the following way:

  1. Place the kernel anchor on top of a determined pixel, with the rest of the kernel overlaying the corresponding local pixels in the image.
  2. Multiply the kernel coefficients by the corresponding image pixel values and sum the result.
  3. Place the result to the location of the anchor in the input image.
  4. Repeat the process for all pixels by scanning the kernel over the entire image.

Expressing the procedure above in the form of an equation we would have:

\[H(x,y) = \sum_{i=0}^{M_{i} - 1} \sum_{j=0}^{M_{j}-1} I(x+i - a_{i}, y + j - a_{j})K(i,j)\]

Fortunately, OpenCV provides you with the function filter2D() so you do not have to code all these operations.

What does this program do?

  • Loads an image
  • Performs a normalized box filter. For instance, for a kernel of size \(size = 3\), the kernel would be:

    \[K = \dfrac{1}{3 \cdot 3} \begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix}\]

The program will perform the filter operation with kernels of sizes 3, 5, 7, 9 and 11.

  • The filter output (with each kernel) will be shown during 500 milliseconds

Code

The tutorial code's is shown in the lines below.

Explanation

Load an image

Initialize the arguments

Loop

Perform an infinite loop updating the kernel size and applying our linear filter to the input image. Let's analyze that more in detail:

  • First we define the kernel our filter is going to use. Here it is:

The first line is to update the kernel_size to odd values in the range: \([3,11]\). The second line actually builds the kernel by setting its value to a matrix filled with \(1's\) and normalizing it by dividing it between the number of elements.

  • After setting the kernel, we can generate the filter by using the function filter2D() :
  • The arguments denote:
    • src: Source image
    • dst: Destination image
    • ddepth: The depth of dst. A negative value (such as \(-1\)) indicates that the depth is the same as the source.
    • kernel: The kernel to be scanned through the image
    • anchor: The position of the anchor relative to its kernel. The location Point(-1, -1) indicates the center by default.
    • delta: A value to be added to each pixel during the correlation. By default it is \(0\)
    • BORDER_DEFAULT: We let this value by default (more details in the following tutorial)
  • Our program will effectuate a while loop, each 500 ms the kernel size of our filter will be updated in the range indicated.

Results

  1. After compiling the code above, you can execute it giving as argument the path of an image. The result should be a window that shows an image blurred by a normalized filter. Each 0.5 seconds the kernel size should change, as can be seen in the series of snapshots below:

cv::String
std::string String
Definition: cvstd.hpp:150
cv::Point_< int >
cv::imread
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
cv::IMREAD_COLOR
@ IMREAD_COLOR
If set, always convert image to the 3 channel BGR color image.
Definition: imgcodecs.hpp:67
cv::filter2D
void filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel, Point anchor=Point(-1,-1), double delta=0, int borderType=BORDER_DEFAULT)
Convolves an image with the kernel.
cv::samples::findFile
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
cv::BORDER_DEFAULT
@ BORDER_DEFAULT
same as BORDER_REFLECT_101
Definition: base.hpp:277
highgui.hpp
CV_32F
#define CV_32F
Definition: interface.h:78
cv::Mat::empty
bool empty() const
Returns true if the array has no elements.
imgcodecs.hpp
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::Scalar
Scalar_< double > Scalar
Definition: types.hpp:669
cv::Point
Point2i Point
Definition: types.hpp:194
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:792
cv::imshow
void imshow(const String &winname, const ogl::Texture2D &tex)
Displays OpenGL 2D texture in the specified window.
cv::hal::filter2D
void filter2D(int stype, int dtype, int kernel_type, uchar *src_data, size_t src_step, uchar *dst_data, size_t dst_step, int width, int height, int full_width, int full_height, int offset_x, int offset_y, uchar *kernel_data, size_t kernel_step, int kernel_width, int kernel_height, int anchor_x, int anchor_y, double delta, int borderType, bool isSubmatrix)
cv
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:52
imgproc.hpp
cv::Mat::ones
static MatExpr ones(int rows, int cols, int type)
Returns an array of all 1's of the specified size and type.