What You Need In A Website Design

Tameera Corporal is a professional website designer, graphics creator, business administration trainer, and published author. She loves to write code, articles, and books and is a lifetime…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Image Convolution From Scratch

Mathematical operation on two functions that produces a third function representing how the shape of one is modified by the other.

Image convolution — kernel filtering

The idea behind convolution is to study how one function when mapped with another function brings out a new modified function. When the same is applied to signals it is called convolution 1d, to images — convolution 2d, and to videos — convolution 3d. This article focuses mainly on convolution 2d.

We can think of an image as a 2Dimensional matrix containing pixel color values in the range of 0 to 255. Mathematically we can manipulate this matrix by applying various matrix operations.

We use imread() object to read the image. By default cv2.imread() reads the image in the format of Blue, Green, and Red. We need to convert it into Red, Blue, and Green format, that makes sense.

Lena original image (Google)

We take matrix values of a GRAY scale image where each pixel contains values in between 0 and 255. The problem with the color image is that each pixel value is a combination of 3 values probably the form of [R, G, B] or [B, G, R] which can make the computation complicated. So, to keep things simple we take a GRAY scale image.

If we view the matrix, we see that it contains pixel values in the range of 0 to 255.

Let’s transpose the above matrix and see if the image gets transposed.

Do you see the difference between the original matrix and the transposed matrix? Now save the matrix as an image using imwrite() method — which reads the matrix and numbers and writes as an image.

Let’s see the difference and get to know.

Lena — original vs transposed

We get an image that is totally transposed and it’s because of the transposed matrix that we performed earlier.

For convolution, we require a separate kernel filter which is operated to the entire image resulting in a completely modified image.

In image processing; kernel, convolution matrix, or mask is a small matrix used for blurring, sharpening, embossing, edge detection, and more. This is accomplished by doing a convolution between a kernel and an image.

The above function returns a 2Dimentional NumPy array containing the pixel values.

The above function returns a giant matrix containing sub-matrices of the size kernel which will again be used later. The resultant matrix can also be called a sampled matrix.

The giant matrix or the sampled matrix is passed as the argument along with the kernel filter in the above function to perform the convolution.

The above function is a plotting function that compares the original image with the transformed image after convolution.

We have several types of convolution operations that can be applied to an image. Few of them are

Lena — original vs identical

From the above result, it is clear that there is no difference between the original and the transformed image.

From the above result, we can say that the edges are being highlighted by white and the rest all is black. The algorithm was able to identify the edges of specific details like eyes and hair. However, there are other kinds of edge detecting algorithms.

Lena — original vs sharpen

From the above result, it is clear that the transformed image persists some sort of noise and we also see that the brighter areas got even brighter and also the darker areas got even darker.

From the result, we notice that the transformed image is slightly smooth compared with the original image. As we now know that when the kernel is operated with the sub-matrices the sum result is immediately averaged and thus leaving it with a normalized value.

Lena — original vs gaussina3

The gaussian algorithm works well to reduce the image noise and represents the image in a more beautiful way. The transformed image actually seems smoother than the original.

My custom convolution by random tweaking in the kernel matrix.

PS: Although convolution is the concept that deals with image manipulation, it was good to be able to implement and understand the mathematics of it. In the next article, I will explain why I am using the default kernels for achieving a certain transformation.

Add a comment

Related posts:

The Web Design Process

The volume and value of sales now being performed online globally continues to grow at a rapid rate, with no indication of slowing down. Your customers shop online more than ever before, and they are…

Cortisone injections for hip and knee pain are more dangerous than was thought

Cortisone injections for hip and knee pain lead to more complications than previously thought, research has found. The anti-inflammatory jabs are used by athletes to mask pain, and to treat symptoms…

5 acts of kindness

Kindness is a gift we give not other but ourselves so that we get to feel good about ourselves. Each act of kindness brings us joy that cannot be bought. My five acts of kindness within one day are…