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
3 x 3 Gaussian Kernel


5 x 5 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)
{
    // 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;
    }

    //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
Original Image

Image blurred with 3 x 3 Gaussian Kernel
Image blurred with 3 x 3 Gaussian Kernel


Image blurred with 5 x 5 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;
        bool bSuccess = cap.read(frame); // read a new frame from video 
        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.


No comments:

Post a Comment