Convolutions with Open-CV and Python:

Convolution is one of the most critical and fundamental building block in computer vision. In simple words it’s an element wise multiplication of the matrices and then summed. In simple words

  • Multiply the two matrices element by element
  • Sum the elements together

In terms of computer vision suppose we have a 2 dimensional image with x number of rows and y number of columns. We have another matrix which may be smaller or tiny in size called as kernel matrix. It is normal to find hard defined kernel matrices for the various types of image processing. Kernel matrix being smaller in size moves from top to bottom and from left to right. Let’s consider the first point written above that is multiplication of the two matrices. So suppose another thing that we are that we have an image whose pixel by pixel values in the form of matrix is as follow.

 

Suppose another thing we have a kernel matrix. Kernel matrices are hard defined for various types of image processing techniques.

 

So after multiplication element by element we come to the equation in the form mentioned below.

 

So after sum it will be = 30.

So in the output image the value at the coordinate (1,1) is will be 30 while in the input image it was 9. It was just a short example how image filtering is done. Below is the python code for image filtering

############################################Python###################################

import cv2

import numpy as np

from matplotlib import pyplot as plt

 

img = cv2.imread(‘image.png’) # reading the image

kernel = np.ones((5,5), np.float32)/25

dst = cv2.filter2D(img, -1, kernel)

 

plt.subplot(121), plt.imshow(img), plt.title(‘Original’)

plt.xticks([]), plt.yticks([])

plt.subplot(122),plt.imshow(dst),plt.title(‘Averaging’)

plt.xticks([]), plt.yticks([])

 

plt.show()

Related posts

One Thought to “Convolutions with Open-CV and Python:”

  1. Great content! Super high-quality! Keep it up! 🙂

Leave a Comment