Let's learn the meaning of some terms used when discussing image filtering techniques, kernel and convolution.

### Kernel

Kernel is a matrix with an odd height and an odd width. It is also known as convolution matrix, mask or filter. Kernels are named based on their value distribution. Kernel is used to define a neighborhood of a pixel in a image filtering operation. Some of the popular kernels are Normalized box filter, Gaussian kernel, Laplacian kernel, edge detecting kernels etc. Kernels can be defined with different sizes. But large kernels result in a large processing time.

This is a 3 x 3 Normalized Box filter. This kernel is used in homogeneous smoothing (blurring).

This is a 3 x 3 Gaussian kernel used in Gaussian smoothing (blurring).

2-D Gaussian Function |

x, y is the coordinates of the kernel where the origin is placed at the center. (i.e. x = 0 and y = 0 at the center element of the kernel.)

**σ**is the standard deviation of the Gaussian distribution. For larger standard deviations, larger kernels are required in order to accurately perform the Gaussian smoothing. The standard deviation of the following 5 x 5 Gaussian kernel is 1.### Convolution

Convolution is a mathematical operation performed on images by sliding a kernel across the whole image and calculating new values for each pixels based on the value of the kernel and the value of overlapping pixels of original image.

C_{22}= (K_{11}x A_{11}+ K_{12 }x A_{12}+ K_{13}x A_{13}) + (K_{21}x A_{21 }+ K_{22}x A_{22}+ K_{23}x A_{23}) + (K_{31}x A_{31}+ K_{32}x A_{32}+ K_{33}x A_{33}) C_{23}= (K_{11}x A_{12}+ K_{12}x A_{13}+ K_{13}x A_{14}) + (K_{21}x A_{22}+ K_{22}x A_{23}+ K_{23}x A_{24}) + (K_{31}x A_{32}+ K_{32}x A_{33}+ K_{33}x A_{34}) C_{24}= (K_{11}x A_{13}+ K_{12}x A_{14}+ K_{13}x A_{15}) + (K_{21}x A_{23}+ K_{22}x A_{24}+ K_{23}x A_{25}) + (K_{31}x A_{33}+ K_{32}x A_{34}+ K_{33}x A_{35}) C_{32}= (K_{11}x A_{21}+ K_{12}x A_{22}+ K_{13}x A_{23}) + (K_{21}x A_{31}+ K_{22}x A_{32}+ K_{23}x A_{33}) + (K_{31}x A_{41}+ K_{32}x A_{42}+ K_{33}x A_{43}) C_{33}= (K_{11}x A_{22}+ K_{12}x A_{23}+ K_{13}x A_{24}) + (K_{21}x A_{32}+ K_{22}x A_{33}+ K_{23}x A_{34}) + (K_{31}x A_{42}+ K_{32}x A_{43}+ K_{33}x A_{44})

In the same way C34, can be calculated in the convolved image. But other values in the boundary of the convolved image (e.g C11, C12, etc) cannot be calculated in the same way because the kernel is partially overlapped with the original image at the boundary. Therefore non-overlapped non-existing pixel values should be calculated in order to determine the pixel values at the boundaries of the convolved image. There are various techniques to handle this boundary values, but I'm not going to discuss it in this tutorial.

After learning bit of theory of image filtering, let's learn some use cases of image filtering techniques with OpenCV C++ examples.

- Smooth / Blur Images

When an image is acquired by a camera or other method, the image may be corrupted by random dots and noises. Smoothing/blurring is one of image processing techniques to eliminate such imperfections and improve the image. Image smoothing techniques can be applied even for each frames of a video to eliminate imperfections and improve the video.

- Homogeneous Blur
- Gaussian Blur
- Median Smoothing
- Bilateral Smoothing

Thanx for excellent tutorial!

ReplyDeleteBut, could you give a link, how are the boundary pixels counted? Just for better undrstanding

This comment has been removed by the author.

DeleteHi, Thanks for the tutorial, I have a question here, how those pixel values of 5*5 matrix calculated, or those are standard

ReplyDeleteChoose your favourite image from your device gallery or capture the real picture with your smartphone camera to create a drawing photo sketch & pencil sketch photo editor & set some stylish colour drawing effect and sparkle effect.

ReplyDeleteWhen you make a beautiful pencil sketch of your image then you feel it’s the best photo sketch - convert photo to sketch app and sparkle effect in the world.

Well, I was looking for Law Essay Writing Services for my essay when I landed on your post. However, I know it is not helping me out in my essay but I totally love the way you have described it because during my whole academic career, I could not learn matrices and this was the reason I always used to hate maths.

ReplyDelete