Kernels used in the homogeneous blur is called normalized box filter. You may define any size for this kernel according to your requirement. But it is preferable to define square kernels with a size of odd width and height. In the following images, I have shown 3 x 3 and 5 x 5 normalized box filters.
3x3 Normalized box filter |
5 x 5 Normalized box filter |
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.
Homogeneous Blur on Images with OpenCV
This is how you blur/smooth an image with OpenCV. You may choose the size of the kernel according to your requirement. I have used 3 x 3 and 5 x 5 kernel for this example program.
//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/Lady with a Guitar.jpg"); // 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 kernel Mat image_blurred_with_3x3_kernel; blur(image, image_blurred_with_3x3_kernel, Size(3, 3)); //Blur the image with 5x5 kernel Mat image_blurred_with_5x5_kernel; blur(image, image_blurred_with_5x5_kernel, Size(5, 5)); //Define names of the windows String window_name = "The Guitar"; String window_name_blurred_with_3x3_kernel = "The Guitar Blurred with 3 x 3 Kernel"; String window_name_blurred_with_5x5_kernel = "The Guitar Blurred with 5 x 5 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/Lady with a Guitar.jpg" in the code with a valid location to an image in your computer. Then you should see a set of images like the below.
Original Image |
Image blurred with 3 x 3 normalized box filter |
Image blurred with 5 x 5 normalized box filter |
Explanation
Let's go through the above OpenCV program line by line.
// Read the image file Mat image = imread("D:/My OpenCV Website/Lady with a Guitar.jpg"); // 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/Lady with a Guitar.jpg" 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 check because calling imshow() on empty Mat object might crash your program.
In the above section, you have learned,
Now I am going to show you how to blur/smooth 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.
//Blur the image with 3x3 kernel Mat image_blurred_with_3x3_kernel; blur(image, image_blurred_with_3x3_kernel, Size(3, 3));The above function performs the homogeneous smoothing/blur operation with a 3 x 3 normalized box 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.
//Blur the image with 5x5 kernel Mat image_blurred_with_5x5_kernel; blur(image, image_blurred_with_5x5_kernel, Size(5, 5));The above function performs the homogeneous smoothing/blur operation with a 5 x 5 normalized box 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.
//Define names of the window String window_name = "The Guitar"; String window_name_blurred_with_3x3_kernel = "The Guitar Blurred with 3 x 3 Kernel"; String window_name_blurred_with_5x5_kernel = "The Guitar Blurred with 5 x 5 Kernel"; // Create a window 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 windowsThe 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 homogeneous smoothing/blur operation on images with a normalized box 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
Homogeneous Blur on Videos with OpenCV
Now I am going to show you how to blur/smooth 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 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 kernel Mat frame_blurred_with_5x5_kernel; blur(frame, frame_blurred_with_5x5_kernel, Size(5, 5)); //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.