Let’s continue the study of computer vision, which we started in the last lesson. Let me briefly recall what was there:
· Stages of processing and analysis of images.
· A simple OpenCV program to display a picture in a window.
· Change the size of the picture.
· Convert from color to black and white.
As I wrote in the first part, in order to remove various noises from the image, image blur is used. For example, like this:
import cv2 my_photo = cv2.imread('MyPhoto.jpg') average_image = cv2.blur(my_photo,(7,7)) cv2.imshow('MyPhoto', average_image) cv2.waitKey(0) cv2.destroyAllWindows()
Here is the effect of applying this filter:
But this is a simple filter, it just averages. Gaussian filter is considered more advanced:
import cv2 my_photo = cv2.imread('MyPhoto.jpg') gaussian_image = cv2.GaussianBlur(my_photo,(7,7),0) cv2.imshow('MyPhoto', gaussian_image ) cv2.waitKey(0) cv2.destroyAllWindows()
But we do not believe, let’s check. Here is the result of applying the Gaussian filter:
It would seem, what’s the difference? The second one just blurred the image less.
Let’s check the filter in action. Let’s spoil the image on purpose:
Let’s pass through the first filter:
As you can see, the defects have not disappeared anywhere, they just also become blurry.
And the second filter:
As a matter of fact, the same. Yes, because the blur does not remove such defects. There are other filters for this, which I will cover in future tutorials. But what, then, is blurring used for? It is believed to remove Gaussian noise. Let’s check that too.
Now let’s spoil the photo by adding Gaussian noise, this can be done, for example, in Photoshop:
Let’s see how the first filter works:
Noise is visible.
And the second filter:
The noise is visible, but the blur is less.
We can increase the window size, for example, do not 7, but 11:
And in fact, we got the first picture. In other words, Gaussian blur with a larger window actually smears the image less. If we apply averaging with the same window (11 pixels), we get this:
Thus, the Gaussian filter works really better.
So let’s recap.
We learned how to apply a blur filter to an image in order to remove Gaussian noise, and compared two filters: an averaging filter and a Gaussian filter.