Blur Operations On The Picture

In this article, we will talk about blurring operations on an image using Python and the openCv library.
Ezgisu Akat
2 minutes

Blur Operations On The Picture

We are developing, renewing, and even changing day by day, and of course, technological developments provide this development. In this article, I will briefly talk about image processing used in various fields such as medicine, military, security, agricultural applications, and its sub-branch, blurring operations. I hear you ask what is blurring? Image blur is created by applying a low-pass filter to the image. It actually removes high-frequency content from the image, such as noise and edges. The noise may be present for several reasons, such as a damaged or malfunctioning sensor used to capture the image, inadequate lighting, or other environmental variables. As you reduce the noise, the details also decrease. In this application, we will discuss simple average blurring, gaussian blurring, median blurring, and bilateral blurring.

If you are ready, we can start by making our installations. I used Python 3.10.5 version, OpenCV-python version, and Pycharm as IDE.

Line 2: First, we added our OpenCV package to our project, and now we can read our image.

Line 5: We can read and upload the image using the imread function. We have given the path to the file containing our image as a parameter to the imread function. 

Line 6: We display our picture on the screen with the imshow function. In the first parameter of the imshow function, we name the window that allows the image to be displayed on the screen. In the second parameter, we write the image we uploaded. 

Line 7: The waitKey function keeps the picture on the screen until we press a key. We give the value 0 as the parameter.

Line 10: We made a copy of our picture with the copy function. 

Line 11: Here, we set the value of our kernel_size variable. If I had to explain what a kernel is briefly, a kernel could be used to implement a filter on an image. Also, In this application, a kernel refers to a small matrix mixed with the image using the mathematical operation known as convolution. Different impacts are produced by kernels of various sizes, shapes, and contents.

Line 14:Using the blur function, we did the simple average blurring on our image. The first parameter of the blur function gives the picture we want to blur. We enter the kernel size value as a tuple in the second parameter. To briefly mention the simple average blurring, the simple average blurring is also known as a Box filter and meanfilter. An average filter averages all the pixels in the region around the central pixel, then replaces the original pixel with the average. As the size of the kernel increases, so will the amount in which the image is blurred.

Line 19: Since the return value of the imread function is NumPy ndarray, we can slice the image. Thanks to the slicing process, we can blur the part of the picture we want. Here, we chose the height value of our image between 250 - 600 values and the width value between 200 - 850. You can give the values you want while trying it yourself. Using the blur function, we blurred this part of our picture.

Line 24: We used another type of blurring, gaussian blurring. In the GaussianBlur function, we enter the picture we want to find the first parameter and the kernel size value as a tuple into the second parameter. In the GaussianBlur function, we enter the picture we want to find as the first parameter, the kernel size value as a tuple to the second parameter, sigmaX for the third parameter, and sigmaY for the fourth parameter. In the GaussianBlur function, we enter the picture we want to find as the first parameter and the kernel size value as a tuple to the second parameter. SigmaX and SigmaY are standard deviations in the x and y direction. We are asking OpenCV to automatically calculate sigma depending on our kernel size by setting these values to 0. Similar to average blurring, gaussian blurring uses a weighted mean instead of a single mean, with pixels in the neighborhood of the central pixel contributing more "weight" to the average.

Line 29: We give the kernel size value to the 2nd parameter of the picture we want to blur to the 1st parameter of the median blur function. You could take the salt and pepper out of your image using the median blur technique. The central element of the image is replaced by the median of all the pixels in the kernel area. The kernel size for the median must be square.

Line 34: We give the bilateral filter function the 1st parameter, the picture we want to blur, the 2nd parameter d, the 3rd parameter sigmaColor, and the 4th parameter sigmaSpace.The d defines the diameter of our pixel neighborhood; the higher this diameter, the more pixels will be included in the computation for blurring. The third argument is our standard color deviation. Increasing the value of sigma When calculating the blur, color signifies that more nearby colors will also be considered. sigmaSpace is the Standard deviation that controls the influence of distant pixels. Besides, a bilateral filter is known as a non-linear, edge-preserving, and noise-reduction smoothing filter for images. It replaces each pixel's intensity for a weighted average of intensity values from adjacent pixels.

And the blurring operations on the picture ended here. I hope you enjoyed reading my article and learned better by making the application like me.

Start Free NOW!

Start now for free!

No contracts, no credit card.
Free up-to 100 frames
Free hands-on onboarding & support
Hundreds of applications wait for you