Tutorial: Spatial Filter
Last updated
Last updated
Deep Learning Image Processing.
Updated. 2024.3
PDF version: click here to download
In this tutorial, you will learn how to design various spatial filters in OpenCV. You will learn which filters to apply for Gaussian and impulse noises and how to combine various filters to enhance the noise-corrupted images. You are required to create a camera application that allows the user to select the type and size of the image filter.
The correlation of spatial filtering by kernel w(s,t) on the image f(x,y) to obtain the output image g(x,y) is represented as
We will learn how the filter convolution works by programming in MATLAB.
Then, we will learn how to use filter functions in OpenCV.
An example of convolution with arbitrary values : No Padding with stride 1
Exercise
Download the example code and test images.
Write a simple program that applies a filter mask w(s,t) to image f(x,y) to result the output image g(x,y).
Note that the index of an array in a program starts from ‘0’ (C/C++) or ‘1 (MATLAB)
You need to increase the size of the source image around the border by applying zero padding.
Depending on the type of the filter mask w(s,t), you can obtain smoothing or sharpening filter.
First, read the OpenCV 4.9.0 documentation link
In this tutorial, you will learn how to apply diverse linear filters to smooth images using OpenCV functions such as: blur, GaussianBlur, medianBlur
OpenCV offers the function blur to perform smoothing with the normalized box filter.
We specify arguments :
src: Source image
dst: Destination image
Size( w,h ): Defines the size of the kernel to be used ( of width w pixels and height h pixels)
Point(-1, -1): Indicates where the anchor point (the pixel evaluated) is located with respect to the neighborhood. If there is a negative value, then the center of the kernel is considered the anchor point.
OpenCV offers the function filter2D()
for Filter Kernel Convolution.
It is performed by the function GaussianBlur :
Size(w, h): The size of the kernel to be used (the neighbors to be considered). w and h have to be odd and positive numbers.
sigmaX= The standard deviation in x. Writing 0 implies that it is calculated using kernel
sigmaY=The standard deviation in y. Writing 0 implies that it is calculated using kernel
This filter is provided by the medianBlur function:
i: The size of the kernel (there is only one because we use a square window). It must be odd.
Provided by OpenCV function bilateralFilter
d: The diameter of each pixel neighborhood.
sigmaColor: Standard deviation in the color space.
sigmaSpace: Standard deviation in the coordinate space (in pixel terms)
Calculates the Laplacian of an image using Laplacian()
ddepth: Depth of the destination image. Since our input is CV_8U we define ddepth = CV_16S to avoid overflow
kernel_size: The kernel size of the Sobel operator to be applied internally. We use 3 in this example.
scale, delta and BORDER_DEFAULT: We leave them as default values.
Create a new C++ project in Visual Studio Community
Project Name: DLIP_Tutorial_Filter_Image
Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\
Source File: DLIP_Tutorial_Filter_Image.cpp
Download the example code and test images.
Using the provided sample code, apply the following filters to all test images.
Choose appropriate filter to each image and explain why.
blur()
GaussianBlur()
medianBlur()
filter2D() : Design a normalized box filter kernel 5 by 5
Laplacian()
Show the result images to TA
Create a camera(webcam) application that has filtering function.
Create a new C++ project in Visual Studio Community
Project Name: DLIP_Tutorial_Filter_Vid
Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\
Source File: DLIP_Tutorial_Filter_Vid.cpp
Download the exercise code
You should make a keyboard input menu to let the user choose the type and size of the filter.
Filter options: Gaussian, Laplacian(3x3), Median, others..
Example: 'B' for blur with Gaussian, 'L' for Laplacian, 'M' for Median
Let the user also select the size of Gaussian Filter(bluriness)
Example: As 'UP' key is pressed, filter kernel_size is increased from 3, 5, 7, 9 , ....
Show the result images to TA.
An example code of Spatial Filter with OpenCV-C++
An example code of Spatial Filter with OpenCV-Python