3-Dimensional Convolution

NamyaLG
TheLeanProgrammer
Published in
3 min readJun 13, 2020

This blog is a sequel to the previous one based on 2-Dimensional Convolution.

Link: https://medium.com/@namyalg/2-dimensional-convolution-189abb174d92?sk=f1eb064826c03065c6f7a375961e2139

Give it a read!

I would recommend that you try this out first

I demonstrated the implementation of convolution on grayscale images previously, now let’s add a little color, Shall we?

This blog is a guide to implement convolution to colored images.

Image source: https://www.pythoninformer.com/python-libraries/numpy/numpy-and-images/

Colored images are a stream of RGB values. Each pixel has a corresponding RGB value. Red, Green, and Blue are primary colors. Mixing them in different proportions should be able to produce any color in the universe!

Image Source: https://www.google.com/url?sa=i&url=https%3A%2F%2Fmedium.com%2F%40RaghavPrabhu%2Funderstanding-of-convolutional-neural-network-cnn-deep-learning-

The process of applying filters slightly differs here, the main reason being that there are THREE streams. This would involve performing convolution over the volume.

Let’s look at the code then!

Imports and dependencies
Image dimensions
Colored Image

The image is read and displayed using the OpenCV framework.

Word Of Caution: The imread function reads the pixel's values as (b,g,r) as opposed to the (r,g,b) notation.

Obtaining pixels and separating them into 3 streams

I would like to give an insight into my approach :

  1. The red, blue, and green pixels are separated into different arrays.
  2. Employ the process of convolution individually to each of the streams.
  3. This results in 3 arrays to which convolution has been applied.
  4. Combine the three arrays and convert the RGB values into an image.
Image Source: https://miro.medium.com/max/679/1*-KbwNtZYpJcUQs8yoRGgkQ.jpeg

This is the Plan Of Action we will follow

A Box Blur kernel

Here, I have made use of the same kernel in all three streams, it is possible to use different filters for different streams.

A simple function to perform convolution
Applying convolution to the streams individually

The sizes of r_convol, g_convol, and b_convol are not the same as the image's dimensions. This is because the edges are neglected.

Combining the 3 streams
The blurred image

The Python Imaging Library allows the conversion of arrays or pixels to images. The Image.fromarray() function is used to perform the same.

I would strongly recommend the readers to go through the documentation of the above-used libraries to better understand the code.

Hope this was helpful !!

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

--

--

NamyaLG
TheLeanProgrammer

Tech-enthusiast | Runner_for_life | #NoHumanIsLimited