Life2Coding
Working with Trackbar in OpenCV

Thresholding of image using trackbar:

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.

Code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
int value;
Mat src;
const int max_Value=255;

void on_trackbar(int p,void*)
{
Mat gray,thresh;
cvtColor(src,gray,CV_BGR2GRAY);
threshold(gray,thresh,p,max_Value,CV_THRESH_BINARY);
namedWindow("Thresh");
imshow("Thresh",thresh);

}

int main( int argc, char** argv )
{

src = imread("c:/me1.jpg", CV_LOAD_IMAGE_COLOR); // open colour image

if(! src.data ) // Check input image
{
std::cout << "No image found" << std::endl ;
return -1;
}

namedWindow( "Window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Window", src ); // Show our image inside it.
createTrackbar("Threshold","Window",&value,max_Value,on_trackbar);
waitKey(0); // Waiting for a keystroke to keep the screen visible
return 0;
}

Output:

Using%2BThreshold%2BFunctionality Working with Trackbar in OpenCV
Thresholding of image using trackbar
life2coding_icon [] Working with 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.