How To Blur a Picture Using A Trackbar In OpenCV [C++]
Trackbars are such a useful tools when we talk about projects with graphic user Interface. In this brief tutorial we will learn together how to create trackbars in OpenCV and how to use them to blur an image using box filter method. So let's start coding :D !
Step 1 : importing HPP files (Headers):
#include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> #include
- OpenCV has some good GUI utilities for us (highgui.hpp).
- We need imgproc.hpp to have access to plenty of functions to edit our pictures like blur for example.
Step 2 : creating namespaces :
using namespace cv; using namespace std;
Using namespaces will let us save some time and effort. For example, writing certain terms like cv every time before calling an OpenCV function will not be necessary.
Step 3 : Creating a Trackbar Callback Function and declaring its necessary variables :
Mat image,output; void onChange(int sliderValue, void* userData) { if (sliderValue <= 0) return; blur(image, output, Size(sliderValue, sliderValue));
imshow("BlurWindow", output); }
- We are here creating empty matrices. The Input picture will be stored in the matrix named image and the Output picture in the matrix named output.
- onChange presents a pointer to the function to be called every time the slider position has been changed. So we don't need to have a loop to check every time the slider state.
- blur is a predefined function in OpenCV which blurs a picture using box filter method.
- imshow let us display the window.
- To avoid an exception in case sliderValue <=0,we need an if statement which returns nothing.
Step 4 : Writing the main function :
int main() { // Code Here return 0 ; }
If you don't know what a main function is, it's simply a function that presents the entry point of the program that we are going to run.
Step 5: Declaring the rest of the necessary variable inside the main function :
int blurAmount = 0;
- 0 is the starting value of the slider
String imagefile = "C:\\mouse.jpg";
- The path of the local picture will be stored as string in the variable imagefile.
- Be aware to use
\\
instead of \
Step 6: Loading the local image :
image = imread(imagefile);
Step 7: Creating a Display Window :
namedWindow("BlurWindow");
BlurWindow will be the window name.
Step 8 : Creating the trackbar inside the main function:
createTrackbar("Blur Value", "BlurWindow", &blurAmount, 100, onChange);
- 1st parameter : name of the trackbar
- 2nd parameter: name of the window where the trackbar must be attached
- 3rd parameter: starting value of the slider
- 4th parameter: maximum value of the slider
- 5th parameter: trackbar callback function name
Step 9 : Displaying the processed image:
imshow("Display window", image);
Step 10 : Setting up display time of the output window:
waitKey(0); //exit on any key pressed
- waitKey(0) will display the window infinitely until you press any key(it is suitable for image display not for video display).
Code :
#include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> #includeusing namespace cv; using namespace std; Mat image, output; void onChange(int sliderValue, void* userData) { if (sliderValue <= 0) return; // 1st param =input image ;2nd param =output image ,3rd param =blur size blur(image, output, Size(sliderValue, sliderValue)); imshow("BlurWindow", output); } int main(){ int blurAmount = 0; namedWindow("BlurWindow"); String imagefile = "C:\\mouse.jpg"; image = imread(imagefile); createTrackbar("Blur Value", "BlurWindow", &blurAmount, 100, onChange); imshow("BlurWindow", image); waitKey(0); }