### Gaussian Blur

Gaussian blur/smoothing is the most commonly used smoothing technique to eliminate noises in images and videos. In this technique, an image should be convolved with a Gaussian kernel to produce the smoothed image.

You may define the size of the kernel according to your requirement. But the standard deviation of the Gaussian distribution in X and Y direction should be chosen carefully considering the size of the kernel such that the edges of the kernel is close to zero. Here I have shown the 3 x 3 and 5 x 5 Gaussian kernels.

 3 x 3 Gaussian Kernel

 5 x 5 Gaussian Kernel

You have to choose a right size of the kernel to define the neighborhood of each pixel. If it is too large, small features of the image may be disappeared and the image will look blurred. If it is too small, you cannot eliminate noises in the image.

### Gaussian Blur on Images with OpenCV

OpenCV has an in-built function to perform Gaussian blur/smoothing on images easily. All you have to specify is the size of the Gaussian kernel with which your image should be convolved.

Here is a simple program demonstrating how to smooth an image with a Gaussian kernel with OpenCV.

```//Uncomment the following line if you are compiling this code in Visual Studio
//#include "stdafx.h"

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
Mat image = imread("D:/My OpenCV Website/Lotus.jpeg");

// Check for failure
if (image.empty())
{
cout << "Could not open or find the image" << endl;
cin.get(); //wait for any key press
return -1;
}

//Blur the image with 3x3 Gaussian kernel
Mat image_blurred_with_3x3_kernel;
GaussianBlur(image, image_blurred_with_3x3_kernel, Size(3, 3), 0);

//Blur the image with 5x5 Gaussian kernel
Mat image_blurred_with_5x5_kernel;
GaussianBlur(image, image_blurred_with_5x5_kernel, Size(5, 5), 0);

//Define names of the windows
String window_name = "Lotus";
String window_name_blurred_with_3x3_kernel = "Lotus Blurred with 3 x 3 Gaussian Kernel";
String window_name_blurred_with_5x5_kernel = "Lotus Blurred with 5 x 5 Gaussian Kernel";

// Create windows with above names
namedWindow(window_name);
namedWindow(window_name_blurred_with_3x3_kernel);
namedWindow(window_name_blurred_with_5x5_kernel);

// Show our images inside the created windows.
imshow(window_name, image);
imshow(window_name_blurred_with_3x3_kernel, image_blurred_with_3x3_kernel);
imshow(window_name_blurred_with_5x5_kernel, image_blurred_with_5x5_kernel);

waitKey(0); // Wait for any keystroke in the window

destroyAllWindows(); //destroy all opened windows

return 0;
}
```

Copy and paste the above code snippet into your IDE and run it. Please note that you have to replace "D:/My OpenCV Website/Lotus.jpeg" in the code with a valid location to an image in your computer. Then you should see set of images like the below.

 Original Image

 Image blurred with 3 x 3 Gaussian Kernel

 Image blurred with 5 x 5 Gaussian Kernel

#### Explanation

Let's go through the above OpenCV example program line by line.

```// Read the image file
Mat image = imread("D:/My OpenCV Website/Lotus.jpeg");

// Check for failure
if (image.empty())
{
cout << "Could not open or find the image" << endl;
cin.get(); //wait for any key press
return -1;
}
```
This code segment loads an image from the file "D:/My OpenCV Website/Lotus.jpeg" and returns it as a Mat object.
If the returned Mat object is empty, exit the program by returning from the main function. This is an important validation because calling imshow() on empty Mat object might crash your program.

```//Blur the image with 3x3 Gaussian kernel
Mat image_blurred_with_3x3_kernel;
GaussianBlur(image, image_blurred_with_3x3_kernel, Size(3, 3), 0);
```
The above function performs the Gaussian blur/smoothing operation with a 3 x 3 Gaussian filter on the original image and stores the smoothed image in the image_blurred_with_3x3_kernel Mat object. Each channel in the original image is processed independently. The width and height of the kernel should be odd. The standard deviation in the X direction and the Y direction of the Gaussian distribution will be calculated based on the size of the kernel.

```//Blur the image with 5x5 Gaussian kernel
Mat image_blurred_with_5x5_kernel;
GaussianBlur(image, image_blurred_with_5x5_kernel, Size(5, 5), 0);
```
The above function performs the Gaussian blur/smoothing operation with a 5 x 5 Gaussian filter on the original image and stores the smoothed image in the image_blurred_with_5x5_kernel Mat object. Each channel in the original image is processed independently. The width and height of the kernel should be odd. The standard deviation in the X direction and the Y direction of the Gaussian distribution will be calculated based on the size of the kernel.

```//Define names of the windows
String window_name = "Lotus";
String window_name_blurred_with_3x3_kernel = "Lotus Blurred with 3 x 3 Gaussian Kernel";
String window_name_blurred_with_5x5_kernel = "Lotus Blurred with 5 x 5 Gaussian Kernel";

// Create windows with above names
namedWindow(window_name);
namedWindow(window_name_blurred_with_3x3_kernel);
namedWindow(window_name_blurred_with_5x5_kernel);

// Show our images inside the created windows.
imshow(window_name, image);
imshow(window_name_blurred_with_3x3_kernel, image_blurred_with_3x3_kernel);
imshow(window_name_blurred_with_5x5_kernel, image_blurred_with_5x5_kernel);
```
The above code segment creates windows and shows images in them.

```waitKey(0); // Wait for any keystroke in the window

destroyAllWindows(); //destroy all opened windows
```
The program waits for any keystroke. After any key is pressed, all opened windows will be destroyed.

#### Summary

In the above section, you have learned,
• How to load an image from a file
• How to perform the Gaussian smoothing/blur operation on images with a Gaussian filter.
• How to create windows and display images
• How to wait without exiting the program until the user presses a key
• How to destroy created windows

### Gaussian Blur on Videos with OpenCV

Now I am going to show you how to perform Gaussian blur/smoothing on a video using an OpenCV C++ example. This is pretty much similar to the previous example.

It is recommended to go through the Play Video from File or Camera first in order to understand the following example better.

```//Uncomment the following line if you are compiling this code in Visual Studio
//#include "stdafx.h"

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
//open the video file for reading
VideoCapture cap("D:/My OpenCV Website/A Herd of Deer Running.mp4");

// if not success, exit program
if (cap.isOpened() == false)
{
cout << "Cannot open the video file" << endl;
cin.get(); //wait for any key press
return -1;
}

//Define names of the window
String window_name_of_original_video = "Original Video";
String window_name_of_video_blurred_with_5x5_kernel = "Video Blurred with 5 x 5 Gaussian Kernel";

// Create a window with above names
namedWindow(window_name_of_original_video, WINDOW_NORMAL);
namedWindow(window_name_of_video_blurred_with_5x5_kernel, WINDOW_NORMAL);

while (true)
{
Mat frame;
if (bSuccess == false)
{
cout << "Found the end of the video" << endl;
break;
}

//Blur the frame with 5x5 Gaussian kernel
Mat frame_blurred_with_5x5_kernel;
GaussianBlur(frame, frame_blurred_with_5x5_kernel, Size(5, 5), 0);

//show the frames in the created windows
imshow(window_name_of_original_video, frame);
imshow(window_name_of_video_blurred_with_5x5_kernel, frame_blurred_with_5x5_kernel);

//wait for for 10 ms until any key is pressed.
//If the 'Esc' key is pressed, break the while loop.
//If the any other key is pressed, continue the loop
//If any key is not pressed withing 10 ms, continue the loop
if (waitKey(10) == 27)
{
cout << "Esc key is pressed by user. Stoppig the video" << endl;
break;
}
}

return 0;

}
```
Copy and paste the above code snippet into your IDE and run it. Please note that you have to replace "D:/My OpenCV Website/A Herd of Deer Running.mp4" in the code with a valid location to a video in your computer. Then you should see a smoothed/blurred video along with the original video.

1. César BetancurthJuly 11, 2019 at 9:07 PM

Excellent explanation,it is important understood why we use a specific size of Kernel, thanks for your help.

2. I used this code but it was not work in my system show some cx4435687t error please help me I also need Digital Marketing Assignment help if I don't complete this assignment so it'll my last day in the university. Please help me in this too.

3. I have followed all the procedures you have mentioned, but the output result is very blurred. Can I reduce the pixel size to reduce fading? Buy Essay Online UK

4. I tried this code, but it didn't function in my system, giving me a cx4435687t problem. Could you possibly assist me? If I don't finish this task, it will be my final day at university, thus I'll require Thesis Editing And Proofreading Service. Please assist me as well.

5. This is it guys! If some of you really necessity assistance with essay adn you dont wish to proposal for phd waste your money adn time you can this pretty service for essayadn homewrok! This guys really know how to do it so check this essay writer and be the best! Excellent luck!

6. Great Topic!I have been reading this topic since last night.It was really helpful and informative for me and all the readers.If you are looking to buy an online Secondary Data Collection Company at an affordable and reasonable price.

7. Watch and Download world's famous Turkish action drama Kurulus Osman Season 3 in English on link below
👇
Kurulus Osman Season 3

Kurulus Osman Season 3 Episode 1
Kurulus Osman Season 3 Episode 1

Crypto quantum leap

Tube Mastery and Monetization by matt

Best product for tooth pain ,
Cavity ,
Tooth decay ,
And other oral issues
Need of every home
With discount
And digistore money back guarantee
Steel Bite Pro

8. koop rijbewijs online geregistreerd in de gemeente binnen 5 tot 7 werkdagen tegen een goedkope prijs zonder examens
Rijbewijs Kopen België

Rijbewijs Kopen

Rijbewijs b Kopen

Rijbewijs Online Kopen

9. Comprar carnet de conducir en nuestra web inscrito en el Dgt en un plazo de 5 a 7 días hábiles sin exámenes y podrá conducir libremente en España y otros países sin ningún problema señor

Comprar Licencia de Conducir

Comprar Carnet de Conducir

Comprar Licencia Conducir Legal

Comprar carnet de conducir

comprar licencias de conducir

comprar carnet conducir dgt

comprar licencia de conducir

10. This comment has been removed by the author.

11. This is it guys! If some of you really necessity assistance with master essay you dont wish to waste your money adn time you can this pretty service for master essay homewrok! https://masteressaywriters.co.uk/law-essay This guys really know how to do it so check this essay writer and be the best! Excellent luck!

12. Our day-to-day activities are rapidly shifting online. When our regular tasks are achievable through digital platforms, then why not our business? Considering the latest figures; approx 175 websites are created daily. Considering this shift of the tech era, a Website Builder comes in handy and transforms your business digitally. Now you have two places to increase your current revenue.