Basic Image Filtering

A) IMAGE as Source Data

Before we are going to start Image Filtering, we have to understand the main data: “Image”.

In fact, the image is a matrix having dimensions m x n (i.e. height x width).

Generally, the Image can be thought in the following formats;

a. Binary image (BW): Every elements (pixels) of 2D matrix is true or false (0 or 1). We can give an example as;

Figure 1: Binary image

b. Gray Scale image (I): Every pixel value of 2D matrix in generally is in the range of [0,255], [0,65535] or [0,1]. As mathematical data type, every pixel in the range of [0,255] is “uint8” format (unsigned integer 8 bit); every pixel in the range of [0,65535] is “uint16” format. If the range is [0,1], this pixel format is in double/float precision. We can give an example as;

Figure 2: Gray Image [pixels are in range of [0,1]]

c. Color Image (Rgb, Indexed, HSV, HSI,…): Image has composed of 3 x 2D matrix.

RGB Image: The pixels of every matrix R, G and B has in uint8 format. R,G and B are named as “channels”.

Figure 3: RGB Color Image


Indexed Image: There is one 2D matrix for Indexes (Image Data) and one 2D matrix named as Color Map where the indexes indicates real color values.

Figure 4: Indexed Color Image


HSV (or similar for HSI) Image: The Image composed again with 3 x 2D matrix. One for H Channel (Hue), one for S Channel (Saturation) and the other one is V Channel (Value) [or in HSI, I Channel for Illumination]. Generally, HSI/HSV, every pixels of channels is in the range of [0,255].

In order to understand the details about the Image Data content, you should check the textbooks in [1,2]

B) FILTERING the IMAGE with Pyhton

Filtering of Image can be done on any type of image mentioned in Section A (Image as Source Data). Filtering is also known in literature as “kernel application”, “kernel filtering”, “masking”, “convolution operator” or “correlation operator”.

Convolution and correlation is the same in manner but in electronics “convolution” is preferred one. The only difference is application of mask. In convolution the mask is applied in reversal order (180 degree rotated). If the mask (filter) has symmetrical pattern itself, then correlation and convolution gives the same result.

Figure 5: Simple explanation of filtering (masking, convolving, correlating…) (Fig.Ref: [1])

The application of masking is in common sense made by 3×3 kernel (It can be 5×5, 7×7, or 9×9 and etc). The equation can be given as in equ.1;


In this equation, f is Image matrix, h is mask. “k and l” are limited with height and width of image whereas “i and j” are limited with the mask size (i.e. 3×3) [In equ.1 result of the (i-k) should be considered in its absolute value]

Why we want to apply filtering ?

Since the real world photography deals with the “Noise”, the taken images contain some level of noise. High noisy images are not acceptable one (No one wants to share this photos on facebook !). If the photo that you have noise and there is no chance to take another image ? Or the image is the old one !

Filtering is our helper..

Look at the Lena (well known Girl or grandma (?) in our domain) In (a) original Lena (only bust of the image is known 🙂 ) (b) The Noisy Lena (disordered with generated Gaussian Noise).Fig.6.

Fig.6: Original and Noisy Images (Fig.Ref:

Main Basic Filters:

a. Mean Filter: Also known as Blurring Filter or Average Filter. The aim is replacing the image pixel values with the average value of its neighbors. So the mask size blocks gives the same result by visually suppressing the noise.

Gaussian Averaging Filter is more accurate and near masking method suitable for human vision.

b. Median Filter: Also known as Bluring (??) but mainly Sharpening Filter (i.e. Laplacian is the correct one for sharpening but also this is known as sharpener), Retouching filter.

How we can write the code for applying filter to image?

First import needed libraries for our Python code (Line 1 and 2 below).

Read the image (here we use “png” format to get the gray image directly as matrix format) (Line 5)

Use 5×5 Mean Filter as kernel. (The image pixel value located at the center of mask is replaced by the average of 5×5 (25) neighbors) (Line 7).

Apply the Mean Filter to image (Line 8) and save the resultant image as “result” [3].

Through the Line 10-15, display the original image and the Mean Filtered image.

  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. img = cv2.imread(‘lena.png’)
  5. kernel = np.ones((5,5),np.float32)/25
  6. result = cv2.filter2D(img,-1,kernel)
  7. # You can also use  “result = cv2.blur(img,(5,5))” for same.
  8. # You can also use  “result = cv2.GaussianBlur(img,(5,5),0)” for Gaussian Bluring (more accurate for human vision)
  9. plt.subplot(121),plt.imshow(img),plt.title(‘Original’)
  10. plt.xticks([]), plt.yticks([])
  11. plt.subplot(122),plt.imshow(result),plt.title(‘Mean Filtered’)
  12. plt.xticks([]), plt.yticks([])

If we want to use Median filtering just replace the line 8 with the following [3];

8 result = median = cv2.medianBlur(img,5)

I have prepared this quick tutorial for you. I wish it will be helpful for you..

Assoc.Prof.Dr. Muhammed Cinsdikici – 01.01.2018

CopyRight: GPL License. You can use this quick tutorial with referencing to me and my web site


[1] Digital Image Processing 3rd Edition (DIP/3e), Rafael C. Gonzalez, Richard E. Woods, Prentice Hall, ISBN:978-0135052679, 2008.

[2]: Practical Image and Video Processing, Oge Marques, Wiley, ISBN: 978-0470048153 ,2011