Sunday, June 14, 2015

Working with Trackbar in OpenCV

Posted by Md.Hanif Ali on Sunday, June 14, 2015 in | No comments
Thresholding of image using trackbar:

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:
Thresholding of image using trackbar

0 comments:

Post a Comment