Saturday, June 25, 2016

How to Use Trackbar in OpenCV

Posted by Md. Hanif Ali Sohag on Saturday, June 25, 2016 in | No comments
Trackbar Option in OpenCV
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:
Track bar option in OpenCV

0 comments:

Post a Comment