Book Image

Learning OpenCV 3 Computer Vision with Python (Update)

Book Image

Learning OpenCV 3 Computer Vision with Python (Update)

Overview of this book

Table of Contents (16 chapters)
Learning OpenCV 3 Computer Vision with Python Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
6
Retrieving Images and Searching Using Image Descriptors
Index

Custom kernels – getting convoluted


As we have just seen, many of OpenCV's predefined filters use a kernel. Remember that a kernel is a set of weights, which determine how each output pixel is calculated from a neighborhood of input pixels. Another term for a kernel is a convolution matrix. It mixes up or convolves the pixels in a region. Similarly, a kernel-based filter may be called a convolution filter.

OpenCV provides a very versatile filter2D() function, which applies any kernel or convolution matrix that we specify. To understand how to use this function, let's first learn the format of a convolution matrix. It is a 2D array with an odd number of rows and columns. The central element corresponds to a pixel of interest and the other elements correspond to the neighbors of this pixel. Each element contains an integer or floating point value, which is a weight that gets applied to an input pixel's value. Consider this example:

kernel = numpy.array([[-1, -1, -1],
                      [...