The idea behind image processing with kernel convolution is to use a special matricial operation called convolution which takes as input two matrices: a matrix of weights, which is the kernel, and the matrix of the RGB values of our image (in the range [0, 255]). The output will be a new image with pixel values modified depending on the kernel used.
For instance, supposing we use a 3x3 kernel matrix, the pixel RGB value at position [2,2] in the modified image will be calculates as
Probably the most evident example of this technique is blurring. To achieve this effect, we use a Kernel matrix given by
which means that every pixel is the calibrated mean of the 8 neighbours surrounding it.The gaussian blurring effect is obtained by employing a matrix where the values are not constant as in the previous example, but are chosen according to the Gaussian distribution with regard to the vertical and horizontal distance between each surrounding pixel and the central one. In this example we consider a 13x13 kernel matrix, thus the image appears more "smoothed" than the preceeding one.
When performing edge detection, we want to highlight those pixels whose neighbours are not regular. Or, equivalently, we want to dampen those pixels who are equals in value to the immediately close ones. Thus we consider a Kernel matrix given by
Notice that by doing so, if the central pixel and the 8 surrounding ones have all the same value, the resulting pixel will have value 0, hence it will be black.Sharpening effects are applied by enhancing the constrast with pixels on the sides, thus we use a kernel matrix given by
We apply this effect on a fractured tibia x-ray image and on a brain tumor MRI. Both images where found on Google.
Embossing is a particular processing used to give a 2D image an idea of tridimensionality. The kernel matrix
ensures that a border separanting two differently colored areas (top-left to bottom-right) will be particularly highlighted depending on the pixels. For instance, when we go from a top darker area to a bottom lighter area, the border is highlighted in white, while the opposite occurs if you invert the colors. Thus we obtain a 3D effect in a given direction.Download the files image_convolution.py
and kernel_application.cc
in the same folder where you also have the image you want to process.
At line 7 of image_convolution.py
insert the name of your image and choose one of the kernel at lines 22-28.
Then save your changes, open a terminal and run the command
python3 image_convolution.py