Filter Images and Videos

The image filtering is a neighborhood operation in which the value of any given pixel in the output image is determined by applying a certain algorithm to the pixel values ​​in the vicinity of the corresponding input pixel. This technique is commonly used to smooth, sharpen and detect edges of images and videos.

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


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 5 x 5 Normalized Box filter. This kernel is also used in the homogeneous smoothing. In the same way, you can create a Normalized Box filter with any size to be used in the homogeneous smoothing .

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

This is a 5 x 5 Gaussian kernel used in Gaussian smoothing (blurring). In the same way, you may create a Gaussian kernel with any size. The value distribution of the kernel should be calculated using the 2-D Gaussian function.
2-D Gaussian Function
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 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.


C22 = (K11 x A11 + K12 x A12 + K13 x A13) + (K21 x A21 + K22 x A22 + K23 x A23) + (K31 x A31 + K32 x A32 + K33 x A33)
C23 = (K11 x A12 + K12 x A13 + K13 x A14) + (K21 x A22 + K22 x A23 + K23 x A24) + (K31 x A32 + K32 x A33 + K33 x A34)
C24 = (K11 x A13 + K12 x A14 + K13 x A15) + (K21 x A23 + K22 x A24 + K23 x A25) + (K31 x A33 + K32 x A34 + K33 x A35)

C32 = (K11 x A21 + K12 x A22 + K13 x A23) + (K21 x A31 + K22 x A32 + K23 x A33) + (K31 x A41 + K32 x A42 + K33 x A43)
C33 = (K11 x A22 + K12 x A23 + K13 x A24) + (K21 x A32 + K22 x A33 + K23 x A34) + (K31 x A42 + K32 x A43 + K33 x A44)

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.


  1. Thanx for excellent tutorial!

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

    1. This comment has been removed by the author.

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

  3. Choose 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.
    When 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.

  4. 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.

  5. amazing thoughts love the way you've write keep it up

    take my online exam check assignment help.

  6. The perks of hiring someone to do my online exam now are numerous. I mean what can go wrong if you have an extra helping hand, might I say a helping mind.

  7. I needed to thank you for this incredible read!! Delhi Call Lady I certainly partaking in every single piece of it I have you bookmarked to look at new stuff you post.

  8. koop rijbewijs online geregistreerd in de gemeente binnen 5 tot 7 werkdagen tegen een goedkope prijs zonder examens
    Rijbewijs Kopen België

    Rijbewijs Kopen

    Rijbewijs b Kopen

    Rijbewijs Online Kopen

  9. Comprar carnet de conducir en nuestra web inscrito en el Dgt en un plazo de 5 a 7 días hábiles sin exámenes y podrá conducir libremente en España y otros países sin ningún problema señor

    Comprar Licencia de Conducir

    Comprar Carnet de Conducir

    Comprar Licencia Conducir Legal

    Comprar carnet de conducir

    comprar licencias de conducir

    comprar carnet conducir dgt

    comprar licencia de conducir