Life2Coding
How to Use Trackbar in OpenCV

Trackbar Option in OpenCV

Trackbars are very useful on lots of occasions. It enables users to change various parameters while the OpenCV application is running.

Simple Use of Trackbars

Whenever you change the position of a trackbar, the value of an integer variable is changed. Using that value, we can change a property of an image or a video. In this example, we will learn the usage of trackbar in OpenCV. We can implement any type of value change functionality with this code.

In this tutorial, you will learn how to use trackbar in OpenCV.

Code:

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
// Global variables

const int slider_max = 2;
int slider;
Mat img;
// Callback function for trackbar event

void on_trackbar(int pos, void *)
{
Mat img_converted;
if (pos == 1)
{
cvtColor(img, img_converted, CV_RGB2HSV);
}
else if (pos==2)
{
cvtColor(img, img_converted, CV_RGB2GRAY);
}
else
img_converted = img;

imshow("Trackbar app", img_converted);
}

int main()
{
img = imread("c://test//image1.jpg");
namedWindow("Trackbar app");
imshow("Trackbar app", img);

slider = 0;

//create the trackbar GUI for user option
createTrackbar("Color", "Trackbar app", &slider, slider_max, on_trackbar);

while (char(waitKey(1)) != 'q') 
{

}
return 0;
}

Output:

hhht-500x184 How to Use Trackbar in OpenCV
Track bar option in OpenCV
life2coding_icon [] How to Use Trackbar in OpenCV

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.