A Demonstration of Filters in Image Processing

Photo by Jacob Owens on Unsplash

Images are Numpy arrays

A Numpy array is a grid of values, all of the same type. These values contain information about each pixel of the image. It is the primary information stored in the pixels and determines the intensity of light from each point of the image. Because images are Numpy arrays, arithmetic operations can be performed on them like any other array.

Figure 1. Randomly Generated Numpy Array
  • np.set_printoptions(threshold=sys.maxsize): This function determines the way arrays and other Numpy objects are displayed. The objective of the code is to print all the values of the Numpy array. The default setting truncates the printed array.
  • np.random.randint(0, 50, (15,15)): This function returns random integers from “low” (inclusive) to “high” (exclusive). The objective of the code is to return random integers from 0(inclusive) to 50(exclusive), with 15 rows and 15 columns.
Figure 2. Image of Generated Numpy Array
Figure 3. Sliced Portion after being replaced with zero
Figure 4. Sliced Portion after being replaced with 255

As it has been illustrated above, images are simply Numpy arrays and can be manipulated like any other array.

Filters are operations performed on images.

Just as we sliced a portion of the image array to produce certain color effects, filters are produced in like manner. Filters can be used to blur images, sharpen images, detect the edges in images, and several others. Basically, filters enhance features in images and can also reduce noise in them.


Kernels are matrices used to produce effects (blurring, sharpening, outlining) in images. They are mostly 2-dimensional arrays and are often used interchangeably with filters. There are 1-dimensional kernels, 3-dimensional kernels, etc. In 3D however, you are likely to hear more of filters than kernels.

Figure 5. Examples of Kernels


Convolution is a mathematical operation that multiplies two arrays of the same dimensionality to produce a new array of the same dimensionality. This is achieved by running or sliding one of the arrays (kernel) across the other array (image array). For every pixel of the image, we slide or map the kernel over it and then multiply each pixel value of the image with the corresponding value of the kernel. Afterward, we take the sum of the product values which are used to replace pixel values of the image. Let’s illustrate;

Figure 6. Convolution Operation
Figure 7. Mathematical Formular for Convolution
Figure 8. Image & Kernel Matrices
Figure 9. Convolution Computation
Figure 10. Convolution Computation
Figure 11. Output Matrix from Scipy’s Convolution Function
  • ndi.convolve(img_matrix, kernel_matrix, mode=”constant”, cval=0): This function performs a multi-dimensional convolution. The objective of the code is to convolve the img_matrix with the kernel_matrix. The mode=” constant” pads/extends the img_matrix by filling all the values beyond the edge with the same constant value(0), defined by the “cval” parameter.

Next, we will observe the various effects different filters/kernels produce.

Mean Kernel

Figure 12. Blurring Effect of the Mean Kernel
Figure 13. Mean and Gaussian Kernel Effects
  • filters.gaussian(image): This function performs multi-dimensional Gaussian filtering. The objective of the code is to apply the Gaussian filter to the image.

Identity Kernel

Figure 14.Identity Kernel Effect

Edge-detection Kernel

Figure 15. Edge-detection Kernel Effect

Sharpen Kernel

Figure 16. Sharpen Kernel Effect

Gaussian Kernel

Figure 17. Gaussian Kernel Effect
Figure 18. Tweaked Identity Kernel Effect


Data Science enthusiast - Interest in image processing