Saturday, 15 March 2014

#6. User Interface: Creating a Trackbar

Now, I will deviate a bit from our image processing techniques and algorithms.
So far, we have seen how we can use 2D Convolution to process images.
In this post, I will show you how create a trackbar for user interface.

A simple example that increases brightness of the picture.
-----------------------------------------------------------------------------
Input image:


#include<iostream>
#include<cv.h>
#include<highgui.h>
using namespace std;
using namespace cv;

Mat img;
//STOP. Refer to 'main' function first.

int slider=0, slider_max=100;

// the following call back function takes two arguments
//first: the position of trackbar
// second: Null
// Refer documentation

void on_trackbar(int pos, void *){
Mat img_c;
img_c=img+Scalar(pos,pos,pos);
//This command varies brightness according to the position of trackbar.
imshow("Image",img_c);
}

int main()
{
img=imread("images/image.jpg",CV_LOAD_IMAGE_COLOR);
namedWindow("Image");
createTrackbar("Brightness","Image",&slider,slider_max,on_trackbar);

//createTrackbar creates a trackbar
//first argument "Brightness": Title of the Trackbar
//second argument "Image": The window name in which trackbar has to be created.
//third: minimum value of trackbar position
//NOTE: it has to be passed by reference. Refer documentation.
//fourth: maximum value
//fifth: the function which has to be called when the user interacts(i.e. Trackbar position is changed)

while(char(waitKey(0))!='q'){}
return 0;

}
-----------------------------------------------------------------
Result:

You can't see the trackbar here, but you've to believe in my code:P


Note: In previous tutorials, if you remember, I had commented against a variable that the value can be varied. You can actually try varying the values by creating a trackbar!
Do this as an exercise. In this way you can also go through all the tutorials until now, and believe me, your concepts will be more clear. Do comment if you have any doubts.

No comments:

Post a Comment