3-Dimensional Convolution
This blog is a sequel to the previous one based on 2-Dimensional Convolution.
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.
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!
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!
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.
I would like to give an insight into my approach :
- The red, blue, and green pixels are separated into different arrays.
- Employ the process of convolution individually to each of the streams.
- This results in 3 arrays to which convolution has been applied.
- Combine the three arrays and convert the RGB values into an image.
This is the Plan Of Action we will follow
Here, I have made use of the same kernel in all three streams, it is possible to use different filters for different streams.
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.
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