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:
Place the kernel anchor on top of a determined pixel, with the rest of the kernel overlaying the corresponding local pixels in the image.
Multiply the kernel coefficients by the corresponding image pixel values and sum the result.
Place the result to the location of the anchor in the input image.
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:
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() :
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
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:
Generated on Tue Oct 16 2018 05:17:46 for OpenCV by 1.8.12