Adding Trackbar

Trackbar helps the user to play with image property dynamically, when your app is running. You can change brightness, contrast with the use of trackbar on the go.

#include "stdafx.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/contrib/contrib.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/core.hpp"

using namespace cv;
#include <iostream>

using namespace std;

int main()
{
Mat img = imread("scenary2.png", CV_LOAD_IMAGE_COLOR);

int brightValue = 50;
int contrastValue = 50;
namedWindow("Image", CV_WINDOW_AUTOSIZE);

createTrackbar("brightness", "Image", &brightValue, 100);
createTrackbar("contrast", "Image", &contrastValue, 100);

while (1)
{
Mat dest;
double brightVal = brightValue - 50;
double contrastVal = contrastValue / 50.0;

img.convertTo(dest, -1, contrastVal, brightVal);

imshow("Image", dest);

if (waitKey(30) == 27)
{
break;
}
}

return 0;
}

Just run the application and play with the trackbars 😉

  • int createTrackbar(const string& trackbarname, const string& winname, int* value, int count, TrackbarCallback onChange = 0, void* userdata = 0)
This OpenCV function creates a trackbar and attached that trackbar to a specified window
    • trackbarname – The name of the trackbar
    • winname – The name of the window to which the trackbar is attached
    • value – This integer, pointed by this pointer, holds the value associated with the position of the trackbar
    • count – The maximum value of the trackbar. The minimum value is always zero.
    • onChange – This function will be called everytime the position of the trackbar is changed. The prototype of this function should be “FunctionName(int, void*)“. The “int” value is the value associate with the position of the trackbar. And “void*” is any pointer value which you pass as the “userdata” (See the next parameter).
    • userdata – This pointer variable will be passed as the second parameter of the above function

 

Now lets use the 5th and 6th parameter of the createTrackBar function to avoid running in infinite loop.

#include "stdafx.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
//#include "opencv2/contrib/contrib.hpp"
#include "opencv2/core/core_c.h"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/core/core.hpp"

using namespace cv;
#include <iostream>
using namespace std;

Mat img;

void CallBackBrightNess(int brightVal, void* userData)
{
Mat src;
int cvalue = *(static_cast<int*>(userData));
double cFactor = (double)cvalue / 50.0;
img.convertTo(src, -1, cFactor, brightVal - 50);
imshow("Image", src);
}

void CallBackContrast(int contrastVal, void* userData)
{
Mat src;
double cFactor = (double)contrastVal / 50.0;
img.convertTo(src, -1, contrastVal / 50.0, *(static_cast<int*>(userData)) - 50);
imshow("Image", src);
}

int main()
{
img = imread("scenary2.png", CV_LOAD_IMAGE_COLOR);

int brightValue = 50;
int contrastValue = 50;
namedWindow("Image", CV_WINDOW_AUTOSIZE);

createTrackbar("brightness", "Image", &brightValue, 100, CallBackBrightNess, &contrastValue);
createTrackbar("contrast", "Image", &contrastValue, 100, CallBackContrast, &brightValue);

imshow("Image", img);

waitKey(0);

return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *